Docker Mysql link to another container - php

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.

Related

How connect a php script on docker container with network host, with a mysql docker container also on network host

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

Can't connect to docker mysql container via PHP

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.

How to run a PHP script on docker?

I have index.php:
<?php
echo "Hello World";
?>
Dockerfile from the website: https://docs.docker.com/samples/library/php/
FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]
I build image and run container:
docker build -t my-php-app .
docker run -p 7000:80 --rm --name hello-world-test my-php-app
I see only text "Hello World" but my application doesn't work in http://localhost:7000/ why?
If you want to run some script "on the fly" with php-cli you can create the container and remove it immediately after the script execution.
Just go to the directory with your code and run:
Unix
docker container run --rm -v $(pwd):/app/ php:7.4-cli php /app/script.php
Windows - cmd
docker container run --rm -v %cd%:/app/ php:7.4-cli php /app/script.php
Windows - power shell
docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/script.php
--rm will remove the container after execution
-v $(pwd):/app/ will mount current directory
php:7.4-cli is the image
and php /app/script.php is the command which will be executed after the container is created
You can keep the same base image as you have php:7.2-cli:
FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]
...build the image:
docker build -t my-php-app .
...run it:
docker run --rm --name hello-world-test my-php-app
You will obtain:
Hello World
Everything you did was correct except the port mapping (-p 7000:80) which is not necessary because you aren't running a web server.
== EDIT
If you want to run it as a web server, use the following Dockerfile:
FROM php:7.2-apache
COPY . /var/www/html/
...build it:
docker build -t my-php-app .
...and run it:
docker run -p 8080:80 -d my-php-app
You will then have your PHP script running on 8080.
 1. Create simple php script:
echo '<?php echo "Working";' > my.php
 2. Run docker:
docker run -p 8080:8080 --rm -v $(pwd):$(pwd) php:7.4-cli php -S 0.0.0.0:8080 $(pwd)/my.php
 3. Open in browser:
http://localhost:8080/
Many answers suggest using Apache for this, but that is not required. You need to have your application in the container run continuously on a specific port. You can keep the php:7.2-cli image, but your CMD should be different:
CMD [ "php", "-S 0.0.0.0:80", "./index.php" ]
This will run the built-in PHP webserver and after that you can expose it with the docker run command you already had
Here is a quick and simple example with Docker on Windows 11, assuming you have a similar directory structure as the example below:
C:\Users\YourName\Workspace\MyProject\program.php
And program.php has the following content:
<?php echo "It works!"; ?>
Then, in the Command Prompt, navigate to the project directory:
cd C:\Users\YourName\Workspace\MyProject
Run with CLI
docker run --rm -p 8080:8080 -v %CD%:/cli php:7.4-cli php -S 0.0.0.0:8080 /cli/program.php
View: http://localhost:8080
Run with SERVER
docker run --rm -d -p 8081:80 -v %CD%:/server --mount type=bind,source="%CD%",target=/var/www/html php:apache
View: http://localhost:8081/program.php
Then feel free to modify program.php and refresh the page.
Environment
Docker version 20.10.16, build aa7e414
Windows 11 Home, Version 22H2, OS build 22622.436

Docker: What's the difference between --link tag:db and --link tag:mysql

I've been trying to migrate to docker with MySQL and PHP but am having trouble getting my head around linking containers. I've managed to get mysql:5.7 working with wordpress:latest and phpmyadmin to test it by running:
docker run --name testdb -d -e MYSQL_ROOT_PASSWORD=password mysql:5.7
docker run --name testadmin -d --link testdb:db -p8080:80 phpmyadmin/phpmyadmin
docker run --name testwp -d -p 80:80 -e WORDPRESS_DB_PASSWORD=password --link testdb:mysql wordpress
Using tag:db where I've used tag:mysql and vice versa brings up connection problems. I'm trying to understand the difference and whether using tag:db or tag:mysql determines the connection type i.e. sockets vs tcp
I've tried searching for documentation on specifics to do with linking but either there isn't much info about the different link types or my googlefu isn't up to snuff.
Im still drilling down through phpmyadmin's source as well as wordpress's source to work out what functions they're using but will take me some time to take it all in. Any info or pointers would be greatly appreciated.
MySql container looks correct for me, so keep using:
docker run --name testdb -d -e MYSQL_ROOT_PASSWORD=password mysql:5.7
But in phpadmin you have to use name testdb in link to MySql, like:
docker run --name testadmin -d --link testdb -p8080:80 phpmyadmin/phpmyadmin
Here you have clear link from this container to another testdb, hence inside phpmyadmin you can refer to MySql container.
For wordpress you have to do the same, and link to MySql container's name, like:
docker run --name testwp -d -p 80:80 --link testdb wordpress
And you don't have to provide MySql root password here, because this password intended for MySql container not for this.
This approach works for me, hope it'll help you!

How can I mount a volume to docker container in OSX?

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

Categories