Connecting to mysql in docker fails











up vote
0
down vote

favorite












I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database.



The Dockerfile depends on a prevoously built image and looks like this:



# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image

# Stage 1, start tests
FROM php:7.2-fpm

RUN curl -sS https://getcomposer.org/installer | php
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&
apt-get install -yq nodejs build-essential
git unzip
libfreetype6-dev
libjpeg62-turbo-dev
libmcrypt-dev
libpng-dev
subversion
&& curl -sL https://deb.nodesource.com/setup_8.x | bash -
&& pecl install mcrypt-1.0.1
&& docker-php-ext-enable mcrypt
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install -j$(nproc) gd
&& docker-php-ext-install -j$(nproc) mysqli

RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

RUN yes | pecl install xdebug
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN npm install -g npm

COPY --from=image /var/www/html/ /var/www/html/

WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh

RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]


I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got




Enter password: mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!




To run this I am running



docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .


The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit.



When I remove that RUN line my build fails when I'm trying to run a script that creates the database




mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'




What do I need to do in my Dockerfile to make it work?










share|improve this question






















  • if status is password right syntax should be mysqladmin -u root -pstatus
    – bato3
    Nov 22 at 11:02










  • I removed the password and it shows the output of the mysqladmin command...
    – dingo_d
    Nov 22 at 11:05










  • for future: don't remove passwords, just change it to **** or PASSWORD
    – bato3
    Nov 22 at 11:10















up vote
0
down vote

favorite












I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database.



The Dockerfile depends on a prevoously built image and looks like this:



# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image

# Stage 1, start tests
FROM php:7.2-fpm

RUN curl -sS https://getcomposer.org/installer | php
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&
apt-get install -yq nodejs build-essential
git unzip
libfreetype6-dev
libjpeg62-turbo-dev
libmcrypt-dev
libpng-dev
subversion
&& curl -sL https://deb.nodesource.com/setup_8.x | bash -
&& pecl install mcrypt-1.0.1
&& docker-php-ext-enable mcrypt
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install -j$(nproc) gd
&& docker-php-ext-install -j$(nproc) mysqli

RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

RUN yes | pecl install xdebug
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN npm install -g npm

COPY --from=image /var/www/html/ /var/www/html/

WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh

RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]


I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got




Enter password: mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!




To run this I am running



docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .


The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit.



When I remove that RUN line my build fails when I'm trying to run a script that creates the database




mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'




What do I need to do in my Dockerfile to make it work?










share|improve this question






















  • if status is password right syntax should be mysqladmin -u root -pstatus
    – bato3
    Nov 22 at 11:02










  • I removed the password and it shows the output of the mysqladmin command...
    – dingo_d
    Nov 22 at 11:05










  • for future: don't remove passwords, just change it to **** or PASSWORD
    – bato3
    Nov 22 at 11:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database.



The Dockerfile depends on a prevoously built image and looks like this:



# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image

# Stage 1, start tests
FROM php:7.2-fpm

RUN curl -sS https://getcomposer.org/installer | php
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&
apt-get install -yq nodejs build-essential
git unzip
libfreetype6-dev
libjpeg62-turbo-dev
libmcrypt-dev
libpng-dev
subversion
&& curl -sL https://deb.nodesource.com/setup_8.x | bash -
&& pecl install mcrypt-1.0.1
&& docker-php-ext-enable mcrypt
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install -j$(nproc) gd
&& docker-php-ext-install -j$(nproc) mysqli

RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

RUN yes | pecl install xdebug
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN npm install -g npm

COPY --from=image /var/www/html/ /var/www/html/

WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh

RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]


I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got




Enter password: mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!




To run this I am running



docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .


The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit.



When I remove that RUN line my build fails when I'm trying to run a script that creates the database




mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'




What do I need to do in my Dockerfile to make it work?










share|improve this question













I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database.



The Dockerfile depends on a prevoously built image and looks like this:



# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image

# Stage 1, start tests
FROM php:7.2-fpm

RUN curl -sS https://getcomposer.org/installer | php
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&
apt-get install -yq nodejs build-essential
git unzip
libfreetype6-dev
libjpeg62-turbo-dev
libmcrypt-dev
libpng-dev
subversion
&& curl -sL https://deb.nodesource.com/setup_8.x | bash -
&& pecl install mcrypt-1.0.1
&& docker-php-ext-enable mcrypt
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install -j$(nproc) gd
&& docker-php-ext-install -j$(nproc) mysqli

RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status

RUN yes | pecl install xdebug
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN npm install -g npm

COPY --from=image /var/www/html/ /var/www/html/

WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh

RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]


I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got




Enter password: mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!




To run this I am running



docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .


The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit.



When I remove that RUN line my build fails when I'm trying to run a script that creates the database




mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'




What do I need to do in my Dockerfile to make it work?







mysql docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 10:40









dingo_d

6,02994781




6,02994781












  • if status is password right syntax should be mysqladmin -u root -pstatus
    – bato3
    Nov 22 at 11:02










  • I removed the password and it shows the output of the mysqladmin command...
    – dingo_d
    Nov 22 at 11:05










  • for future: don't remove passwords, just change it to **** or PASSWORD
    – bato3
    Nov 22 at 11:10


















  • if status is password right syntax should be mysqladmin -u root -pstatus
    – bato3
    Nov 22 at 11:02










  • I removed the password and it shows the output of the mysqladmin command...
    – dingo_d
    Nov 22 at 11:05










  • for future: don't remove passwords, just change it to **** or PASSWORD
    – bato3
    Nov 22 at 11:10
















if status is password right syntax should be mysqladmin -u root -pstatus
– bato3
Nov 22 at 11:02




if status is password right syntax should be mysqladmin -u root -pstatus
– bato3
Nov 22 at 11:02












I removed the password and it shows the output of the mysqladmin command...
– dingo_d
Nov 22 at 11:05




I removed the password and it shows the output of the mysqladmin command...
– dingo_d
Nov 22 at 11:05












for future: don't remove passwords, just change it to **** or PASSWORD
– bato3
Nov 22 at 11:10




for future: don't remove passwords, just change it to **** or PASSWORD
– bato3
Nov 22 at 11:10












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Answer to your specific problem



This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.



So when you have the lines



RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status


The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.



To avoid this you could combine them into a single line like



RUN /etc/init.d/mysql start && mysqladmin -u root -p status


However you will need to do this every time you want to use mysql. Such as in your develop.sh.



Wider answer



It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.



You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.



I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.






share|improve this answer





















  • Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
    – dingo_d
    Nov 22 at 11:14










  • Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
    – Jacob Tomlinson
    Nov 22 at 11:17










  • Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
    – dingo_d
    Nov 22 at 11:26










  • Everything in your image will be available. But nothing will be running.
    – Jacob Tomlinson
    Nov 22 at 11:31






  • 1




    Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
    – Jacob Tomlinson
    Nov 22 at 11:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429091%2fconnecting-to-mysql-in-docker-fails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Answer to your specific problem



This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.



So when you have the lines



RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status


The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.



To avoid this you could combine them into a single line like



RUN /etc/init.d/mysql start && mysqladmin -u root -p status


However you will need to do this every time you want to use mysql. Such as in your develop.sh.



Wider answer



It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.



You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.



I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.






share|improve this answer





















  • Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
    – dingo_d
    Nov 22 at 11:14










  • Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
    – Jacob Tomlinson
    Nov 22 at 11:17










  • Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
    – dingo_d
    Nov 22 at 11:26










  • Everything in your image will be available. But nothing will be running.
    – Jacob Tomlinson
    Nov 22 at 11:31






  • 1




    Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
    – Jacob Tomlinson
    Nov 22 at 11:36















up vote
2
down vote













Answer to your specific problem



This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.



So when you have the lines



RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status


The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.



To avoid this you could combine them into a single line like



RUN /etc/init.d/mysql start && mysqladmin -u root -p status


However you will need to do this every time you want to use mysql. Such as in your develop.sh.



Wider answer



It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.



You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.



I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.






share|improve this answer





















  • Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
    – dingo_d
    Nov 22 at 11:14










  • Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
    – Jacob Tomlinson
    Nov 22 at 11:17










  • Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
    – dingo_d
    Nov 22 at 11:26










  • Everything in your image will be available. But nothing will be running.
    – Jacob Tomlinson
    Nov 22 at 11:31






  • 1




    Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
    – Jacob Tomlinson
    Nov 22 at 11:36













up vote
2
down vote










up vote
2
down vote









Answer to your specific problem



This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.



So when you have the lines



RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status


The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.



To avoid this you could combine them into a single line like



RUN /etc/init.d/mysql start && mysqladmin -u root -p status


However you will need to do this every time you want to use mysql. Such as in your develop.sh.



Wider answer



It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.



You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.



I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.






share|improve this answer












Answer to your specific problem



This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.



So when you have the lines



RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status


The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.



To avoid this you could combine them into a single line like



RUN /etc/init.d/mysql start && mysqladmin -u root -p status


However you will need to do this every time you want to use mysql. Such as in your develop.sh.



Wider answer



It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.



You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.



I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 11:08









Jacob Tomlinson

1,59221946




1,59221946












  • Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
    – dingo_d
    Nov 22 at 11:14










  • Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
    – Jacob Tomlinson
    Nov 22 at 11:17










  • Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
    – dingo_d
    Nov 22 at 11:26










  • Everything in your image will be available. But nothing will be running.
    – Jacob Tomlinson
    Nov 22 at 11:31






  • 1




    Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
    – Jacob Tomlinson
    Nov 22 at 11:36


















  • Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
    – dingo_d
    Nov 22 at 11:14










  • Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
    – Jacob Tomlinson
    Nov 22 at 11:17










  • Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
    – dingo_d
    Nov 22 at 11:26










  • Everything in your image will be available. But nothing will be running.
    – Jacob Tomlinson
    Nov 22 at 11:31






  • 1




    Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
    – Jacob Tomlinson
    Nov 22 at 11:36
















Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
– dingo_d
Nov 22 at 11:14




Yeah, I'm missing the point of docker (I'm a noob when it comes to docker). I'm using it this way instead of docker compose, because it's run as a stage in a Jenkins pipeline. I have one dockerfile for php, then one for nginx and now I wanted to do the same to automate running tests on the project. Plus I want to do it myself instead of nagging my devops so that I can learn more :D I'll see how I can fix this issue. Thanks for the advice!
– dingo_d
Nov 22 at 11:14












Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
– Jacob Tomlinson
Nov 22 at 11:17




Welcome to the wonderful world of docker! I would've thought that you could run two containers at the same time as part of your jenkins pipeline so you could urn mysql and php together.
– Jacob Tomlinson
Nov 22 at 11:17












Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
– dingo_d
Nov 22 at 11:26




Actually, that's the part that is confusing me a bit. I'm running this locally on my mac to avoid waiting for every part of Jenkins pipeline to finish (the build takes ~8 minutes), and I thought that all the things from my PHP image (mysql, composer, node, etc) would be available to me when I did the FROM ${PHP_IMAGE} as image, but they weren't. Again, I'm sure I'm doing something wrong XD
– dingo_d
Nov 22 at 11:26












Everything in your image will be available. But nothing will be running.
– Jacob Tomlinson
Nov 22 at 11:31




Everything in your image will be available. But nothing will be running.
– Jacob Tomlinson
Nov 22 at 11:31




1




1




Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
– Jacob Tomlinson
Nov 22 at 11:36




Ah I missed that you were using staged builds. You will only have access to resources from the last stage.
– Jacob Tomlinson
Nov 22 at 11:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429091%2fconnecting-to-mysql-in-docker-fails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)