I created a docker container from mysql:5.7 image.
sudo docker run --name mysqltest -e MYSQL_ROOT_PASSWORD=password -v mysql:/var/lib/mysql -d mysql:5.7
And I Created a php container that included phpunit
sudo docker run --rm -v $(pwd):/app -w /app phpunit/phpunit:8 phpunit --testdox file.php
in file.php I'm trying to connect mysql container, via mysql container ip as host:
sudo docker inspect mysqltest
but still I get "connection Refused", but I can connect to mysql container directly via :
sudo docker exec -it mysqltest mysql -ppassword
Please Help me, I'm really confused !
The connection is refused as you are not exposing MySQL ports, so it is not seen by the host and the other containers. The proper way of handling these cases is by using docker-compose and custom docker networks, however, the following changes can act as a quick-fix:
sudo docker run --name mysqltest -p 3306:3306 --network=host -e MYSQL_ROOT_PASSWORD=password -v mysql:/var/lib/mysql -d mysql:5.7
Followed by:
sudo docker run --network=host --rm -v $(pwd):/app -w /app phpunit/phpunit:8 phpunit --testdox file.php
-p 3306:3306 tells Docker to map default port of MySQL inside the container to port 3306 of the host. --network=host directs docker to use your local machine network stack. You can verify MySQL being accessible by trying to connect to it from your machine on port 3306 with any of its client applications.
Note that you need to update your application configurations to use the MySQL database on localhost.
Related
I have:
apache+php docker container (net=host)
mongo docker container (net=host)
mysql docker container (net=host)
In the first container, A php script is dumping a database from mysql and migrating processed data to mongodb.
It write to mongodb, but a mysqldump command called from the script is not working.
This is the mysqldump command
mysqldump -h 127.0.0.1 -u xxxxx -pxxxxxx
Got below error
mysqldump: Got error: 2003: Can't connect to MySQL server on
'127.0.0.1' (111) when trying to connect
Note that this script is working on production without docker.
I tried to add:
--protocol=TCP
--port=3307
Also tried to change 127.0.0.1 to mysql ( container name ) but maybe it dont works with (net=host), but it still doesn't work.
If i do mysql -h 127.0.0.1 -u xxxx -pxxxx from the php container it works, but no from php script.
Commands i use to launch containers:
docker run --restart=unless-stopped --net=host --name mysql -v /opt/mysql/mysql_config:/etc/mysql/conf.d -v /opt/mysql/mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxx -d mysql:5.6
docker run --restart=unless-stopped --net=host --name apachephp -d apachephp
The first option is better to use docker-compose networking. But providing a solution base on your current problem.
Using the Host network, the container does not get IP and linking does not work.
so the quick way to fix it can be remove --net host and add linking or you can create docker network as well.
You can publish port if you want to connect from host.
docker run --restart=unless-stopped --name mysql -v /opt/mysql/mysql_config:/etc/mysql/conf.d -v /opt/mysql/mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxx -d mysql:5.6
docker run --restart=unless-stopped --link mysql --name apachephp -d apachephp
Now you will be able to access MySQL using below command from PHP container.
mysql -h mysql -uroot -ppassword
My ultimate goal is to run different versions of PHP on my local computer for testing.
I believe Docker is the best way to accomplish this.
I have been able to get a container with Apache and PHP running via this tutorial: https://github.com/tutumcloud/apache-php
But the issue is that I cannot mount a volume so that I can edit local files and view them on the docker container.
Here are my steps in terminal running in the same directory as the docker file:
docker build -t tutum/apache-php .
docker run -d -p 8080:80 tutum/apache-php -v /Users/user-name-here/apache-php/sample:/app/
The error I get back is:
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"-v\": executable file not found in $PATH".
I'm on OSX - El Captain, just installed latest version of Docker and Docker tools.
The basic format of the docker run command is:
docker run [<options>] <image> [<command>]
In this case, your image is tutum/apache-php, and the run command is being parsed like this:
docker run -d -p 8080:80 tutum/apache-php -v /Users/user-name-here/apache-php/sample:/app/
docker run
options: -d -p 8080:80
image: tutum/apache-php
command: -v /Users/user-name-here/apache-php/sample:/app/
This is the source of your error.
exec: "-v": executable file not found in $PATH
Since the command begins with -v, it will try to locate and execute that command. Of course, it doesn't exist, so you will get this error.
The fix is simply to move the -v option and its argument to the proper place.
docker run -d -p 8080:80 -v /Users/user-name-here/apache-php/sample:/app/ tutum/apache-php
I have three Docker containers running on Mac OS sierra, namely web, mysql and mongo, and have linked both mongo and mysql into web, which is essentially a Ubuntu Xenail base, with Apache and PHP added.
I am currently mounting my local Symfony project into the web container, and that seems to be working fine, but when I try to interact with the DB in any way, I get:
An exception occured in driver: SQLSTATE[HY000] [2002] Connection
refused
I've tried almost every combination of parameter values, but keep getting the same result.
I suspect it might have something to do with the way that I am linking the containers?
I'm in the process of learning Docker, so please excuse my limited knowledge.
Thanks!
Web dockerfile:
FROM ubuntu:xenial
MAINTAINER Some Guy <someguy#domain.com>
RUN apt-get update && apt-get install -y \
apache2 \
vim \
php \
php-common \
php-cli \
php-curl \
php-mysql \
php-mongodb \
libapache2-mod-php \
php-gd
RUN mkdir -p /var/www/symfony.local/public_html
RUN chown -R $USER:$USER /var/www/symfony.local/public_html
RUN chmod -R 755 /var/www
COPY config/php/php.ini /usr/local/etc/php/
COPY config/apache/sites-available/*.conf /etc/apache2/sites-available/
RUN a2enmod rewrite
RUN a2dissite 000-default.conf
RUN a2ensite symfony.local.conf
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Mysql dockerfile:
FROM mysql:5.7
MAINTAINER Some Guy <someguy#domain.com>
# Set the root users password
ENV MYSQL_ROOT_PASSWORD password
# Copy over the DB dump to be run upon creation
COPY sql/ /docker-entrypoint-initdb.d
# Copy over the custom mysql config file
COPY config/ /etc/mysql/conf.d
EXPOSE 3306
Run commands:
docker run --name mongo -d mongo #Im making use of the official Mongo image
docker run --name mysql -v /usr/local/var/mysql:/var/lib/mysql -d someguy/local:mysql
docker run --name web -d -p 80:80 --link mysql:mysql --link mongo:mongo -v ~/Sites/symfony.local/:/var/www/symfony.local/public_html/ someguy/local:web
Symfony parameters.yml file:
parameters:
database_host: mysql
database_port: 3306
database_name: gorilla
database_user: root
database_password: password
UPDATE:
So I've moved over to using docker-compose, but am still receiving the same error.
docker-compose.yml file
version: "2"
services:
web:
build: ./web
ports:
- "80:80"
volumes:
- ~/Sites/symfony.local/:/var/www/symfony.local/public_html/
depends_on:
- db
- mongo
mongo:
image: mongo:latest
mysql:
image: mysql:latest
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
Means, it has nothing to do with your network per se - the links are just fine.
What you are lacking is the how the user has been created, if the user has been created https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L122 .. so actually without a host limitation per se.
The question in your case is, what is inside your "sql/" folder - those scripts are executed during the entrypoint.
Be sure to never use exitX in those scripts, they will interrupt the main script, see https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L151
Check your docker logs for mysql to ensure the script did not print you any warnings, use https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh as an reference.
And last but not least, please use docker-compose. If you have issues with the timings ( mysql starting to slow and your web-container freaks out ), use a "wait for mysql" entrypoint in web:
#!/bin/bash
# this script does only exist to wait for the database before we fire up tomcat / standalone
RET=1
echo "Waiting for database"
while [[ RET -ne 0 ]]; do
sleep 1;
if [ -z "${db_password}" ]; then
mysql -h $db_host -u $db_user -e "select 1" > /dev/null 2>&1; RET=$?
else
mysql -h $db_host -u $db_user -p$db_password -e "select 1" > /dev/null 2>&1; RET=$?
fi
done
Set db_host, $user, $pasword accordingly using ENV or whatever suits you.
I created a Docker container with Apache on it. Everything works, except that I can't link MySQL (https://hub.docker.com/_/mysql/) to my container, and I'm not sure what I did wrong.
Here are the steps I did:
I run mysql server container
docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=pass123 -d mysql/mysql-server:latest
Then, I run and link my Apache Docker:
docker run -it --link mysqlserver:mysql -v "$(pwd)":/var/www/html -p 80:80 -p 3306:3306 apachebash
But I cant access MySQL.
I'm trying to run php built-in server (php -S localhost:8080) via docker, I cannot access site from the host though - I always end up with Connection reset.
Here's a simple Dockerfile I build on:
FROM centos:centos6
RUN rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN yum --enablerepo=remi,remi-php55 install -y php php-opcache php-cli php-pear php-common && yum clean all
RUN php -r "readfile('https://getcomposer.org/installer');" | php
RUN echo "date.timezone = Europe/Prague" >> /etc/php.ini
RUN mv composer.phar /usr/bin/composer
RUN php -r "eval('?>'.file_get_contents('http://backend.bolt80.com/piecrust/install'));"
RUN mv piecrust.phar /usr/bin/chef
CMD ["/bin/bash"]
Is it even possible to run this server with docker? While trying to make it work, I found out that when nginx was installed and set to listen on this very port, it is accessible from the host. PHP built-in server seems to be hidden from the host, thus not able to serve any requests though.
Anyone was successful making this work?
If from within the docker container you start your webserver with php -S localhost:8080 then the webserver will only accept connections originating from the docker container itself.
To be able to communicate with your webserver from the docker host you need to make two changes:
in your Dockerfile, add EXPOSE 8080, or when running the container add -p 8080 to the docker run command line. This will tell the docker host that your container has a program which expects communication on port 8080
start the webserver with php -S 0.0.0.0:8080 so it also accepts connections from outside of the docker container itself
Once you have a container running with those changes, open up a new terminal on the docker host and use the docker ps command to see what port on the docker host is forwarded to port 8080 in the container. For instance:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbccf4058b07 test:latest "php -S 0.0.0.0:8080 4 minutes ago Up 4 minutes 0.0.0.0:49153->8080/tcp sad_hawking
In this example port 49153 of the docker host is to be used. Then query your webserver to validate you can communicate with it:
$ curl http://localhost:49153
For docker compose
services:
api:
build: ./api
command: php -S 0.0.0.0:80
ports:
- 80:80
with dockerfile, placed in ./api folder
FROM php:apline
WORKDIR /
COPY . .