Running PHP docker container with MYSQL container - php

I need to make my PHP docker container connect with my MYSQL container.
The steps I'm trying to execute are:
create a docker network:
docker network create network-pfa
run the mysql container:
docker run --name mysql --network=network-pfa -d -e MYSQL_ALLOW_EMPTY_PASSWORD=YES mysql:5.7
run the php container:
docker run --name php --network=network-pfa -it php bash
After these steps inside the php container bash I'm trying to connect to the mysql using the command:
mysql -u root
Bash error: bash: mysql: command not found
I have been studying docker for just a week and am trying to make a simple system using php, mysql and nginx.
My goal is to connect the three containers together and create a php file that reads some data in mysql and then using nginx to view my php result in my computer's browser (outside the containers).
Note: I am not using docker-compose yet.

I think it is missing to install the mysql-client in your Dockerfile
For alpine
RUN apk add mysql-client
For debian based:
RUN apt-get install -y mysql-client

Related

Is there anyway to start docker container without a service?

I have a php container which needs php-fpm to be started everytime I start the container . Now because of a wrong configuration in php-fpm config file , fpm does not gets started and so , container cannot start. Is there anyway that I can start the container without php-fpm so that I can fix the config file?
The container error is as follows :
[04-Sep-2020 13:47:30] ERROR: [/usr/local/etc/php-fpm.conf:7] value is NULL for a ZEND_INI_PARSER_ENTRY
[04-Sep-2020 13:47:30] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
[04-Sep-2020 13:47:30] ERROR: FPM initialization failed
There are two ways to fix the image. Since I can't find image digitalocean/php, I'll use php:7.4-fpm in my example.
First way:
Copy file from the container and use it to build your own image:
Create Dockerfile:
FROM php:7.4-fpm
COPY ./php-fpm.conf /usr/local/etc/php-fpm.conf
Then:
docker run --detach --name php php:7.4-fpm tail -f /dev/null
docker cp php:/usr/local/etc/php-fpm.conf php-fpm.conf
docker stop php
docker rm -v php
# Edit php-fpm.conf
docker build --tag myphp-fm .
docker run --detach --name php myphp-fpm
and you get running container based on the fixed image.
Second way:
Run a shell using the broken image, fix the file and create a new image using the shell container
docker run -it --name php php:7.4-fpm bash
# Edit /usr/local/etc/php-fpm.conf
# If you install any additional tools remember to remove them afterwards
# and clean any cache's
# Once you're done exit the shell, thus stopping the container
docker commit -a "you" -m "/usr/local/etc/php-fpm.conf fix" php myphp-fpm
docker stop php
docker rm -v php
docker run --detach --name php myphp-fpm
and again you get running container based on the fixed image.
Of course, you can run your new image in whatever way you run the original image in the beginning.
I recommend the first way as it's way easier to edit the file outside the container.

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

Docker mysql environment

I have a Dockerfile that is based FROM php:alpine and I'm trying to add mysql to the build.
FROM php:alpine
COPY test-data/ /var/www/
RUN apk add --update --no-cache \
mysql
# Composer
RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer
ENV COMPOSER_ALLOW_SUPERUSER=1
WORKDIR /var/www
My problem is after successfully building, I tried and run the container with the mysql environment overrides but I cant login to mysql within the container.
$ docker run -e MYSQL_DATABASE=homestead -e MYSQL_USER=homestead -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=secret -ti --rm idecardo /bin/sh
Testing mysql login fails
$ mysql -uroot -p # with password "secret"
Since you are newbie - always try to learn by copying ready working code over and breaking down what is being done in that code.
For Docker:
You can see the docker repository for mysql - https://hub.docker.com/_/mysql/
Under description section you will see links to various docker files for different MySQL versions.
Take one of them as a source for your inspiration, for example the link to 8.0/Dockerfile: https://github.com/docker-library/mysql/blob/fc3e856313423dc2d6a8d74cfd6b678582090fc7/8.0/Dockerfile
Notice that after mysql installation instructions in that dockerfile there is Entrypoint and CMD instructions.
In general:
Since you want php and mysql to work from docker - my advise is for you to see about docker-compose. Docker containers can be run in a variety of ways and docker-compose allows you to launch several docker containers, share some folders between them. In that scenario you would want to launch separate mysql container and separate php container, share host data folders between them and launch your code.
Also, watch some video tutorials online - they explain in details the basics of what docker is all about and how it works.

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

Running package manager inside the docker

I've built an image for the purpose of PHP development, and it became clear to me that I didn't really thought about how to access the tools that I need for every day development. For example: composer, package manager for PHP, I need it to run whenever composer.json updates. I thought it is worth installing those tools inside the same image, but then I don't have a way to access them. So, I can:
Create separate image for composer and run it in different container
Install composer on my host machine.
I'd like to avoid option 2), but then, does it have sense having a setup like 1) ? How did you guys solved this issue ?
Unless you have some quite specific requirements there is a third option:
Connect to the container using docker exec command:
docker exec -it CONTAINER-NAME/ID COMMAND [ARG...]
Here is the example:
1: Create your application:
echo "<?php phpinfo();" > index.php
2: Start container:
docker run -it --rm --name my-apache-php-app -p 80:80 -v "$PWD":/var/www/html php:5.6-apache
3: Open another terminal window and exec required commands inside running container:
docker exec -it my-apache-php-app curl -sS https://getcomposer.org/installer | php
docker exec -it my-apache-php-app ls
If you need shell inside running container - run:
docker exec -it my-apache-php-app bash
That's it!

Categories