1. Preparing the environment
Installation of docker can be referred to Docker introduction and installation details Not here!
Case Requirements:
(1) Each container needs to achieve data persistence;
(2) Assign a fixed IP address to the container to prevent the IP address from changing after the container is rebuilt, causing unnecessary troubles;
Case context:
Install on a docker host!Preset environment as shown in Figure:
2. Case Implementation
(1) Create a network card to address fixed IP addresses
[root@docker ~]# docker network create -d bridge --subnet 200.0.0.0/24 --gateway 200.0.0.1 lnmp //Create network card lnmp, specify segment 200.0.0.0/24
(2) Solving directory problems in nginx containers
[root@docker ~]# docker run -itd --name test nginx //Run a container at will to generate the required configuration files in nginx [root@docker ~]# mkdir /data /wwwroot //Create a directory for mounting nginx containers [root@docker ~]# docker cp test:/etc/nginx /data //Copy the nginx home directory from the nginx container locally [root@docker ~]# docker cp test:/usr/share/nginx/html /wwwroot //Copy the page root directory from the nginx container locally
(3) Run Nginx container tests
[root@docker ~]# docker run -itd --name nginx -v /data/nginx:/etc/nginx -v/wwwroot/html:/usr/share/nginx/html -p 80:80 --network lnmp --ip 200.0.0.10 nginx:latest //Run a container named nginx, mount the appropriate directory, map ports, specify IP addresses [root@docker ~]# echo "hello world" > /wwwroot/html/index.html [root@docker ~]# curl 127.0.0.1 hello world //No problem testing nginx web page root directory
(4) Run mysql container
[root@docker ~]# docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 --network lnmp --ip 200.0.0.20 mysql:5.7 //Run a container named mysql and specify the mapping port and IP address //Configuring environment variables in containers with the'-e'option is equivalent to ENV in dockerfile [root@docker ~]# Yum-y install mysql //download mysql client tool locally for testing [root@docker ~]# mysql -u root -p123456 -h 127.0.0.1 -P 3306 MySQL [(none)]> create database lzj; //Log in to the database and create the database for testing
(5) Run PHP containers
[root@docker ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 200.0.0.30 php:7.2-fpm //Run a container named phpfpm, map ports, mount directories, and specify IP addresses //Looking at the mounted directory is the same as the nginx home directory because the configuration file of the nginx container needs to be modified [root@docker ~]# echo "<?php" >> /wwwroot/html/test.php [root@docker ~]# echo "phpinfo();" >> /wwwroot/html/test.php [root@docker ~]# echo "?>" >> /wwwroot/html/test.php //Create pages for nginx and php connection tests
(6) Edit the configuration file of Nginx so that Nginx can connect to php
[root@docker ~]# vim /data/nginx/conf.d/default.conf 8 location / { 9 root /usr/share/nginx/html; 10 index index.php index.html index.htm; //Add index.php 11 } 30 location ~ \.php$ { 31 root /usr/share/nginx/html; //Modify its page root location 32 fastcgi_pass 200.0.0.30:9000; //Change to IP address of PHP container 33 fastcgi_index index.php; 34 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //Add $document_root variable 35 include fastcgi_params; 36 } [root@docker ~]# docker restart nginx //restart nginx container [root@docker ~]# docker ps | grep nginx //Ensure nginx container is functioning properly e000ccd5c883 nginx:latest "nginx -g 'daemon of..." 27 minutes ago Up 31 seconds 0.0.0.0:80->80/tcp nginx
Access Test:
(7) Test the coordination between PHP container and Mysql container
We use phpMyadmin, a database management tool.
Download phpMyadmin:
[root@docker ~]# wget https://files.phpmyadmin.net/phpMyAdmin/4.9.1/phpMyAdmin-4.9.1-all-languages.zip [root@docker ~]# unzip phpMyAdmin-4.9.1-all-languages.zip [root@docker ~]# mv phpMyAdmin-4.9.1-all-languages /wwwroot/html/phpmyadmin //Move it to the root directory of the page and rename it [root@docker ~]# vim /data/nginx/conf.d/default.conf ........................ //Omit some content and add the following location /phpmyadmin { root /usr/share/nginx/html; index index.html index.htm index.php; } location ~ /phpmyadmin/(?<after_ali>(.*)\.(php|php5)?$) { root /usr/share/nginx/html; fastcgi_pass 200.0.0.30:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } [root@docker ~]# docker restart nginx //restart nginx container [root@docker ~]# docker ps | grep nginx Ensure nginx container is working properly e000ccd5c883 nginx:latest "nginx -g 'daemon of..." About an hour ago Up 9 seconds 0.0.0.0:80->80/tcp nginx
Access Test:
Error is due to the fact that when PHP is compiled and installed normally, you need to add some related options such as "--with-mysql...". When you see this page, it is clear that the PHP container we are running does not add options about the database.
(8) Resolving PHP containers does not support associating Mysql databases
Sign in Docker website As shown in Fig.
Now that you've seen the answer on Docker's website, write a Dockerfile based on the original php image and generate a new image to support this feature!
[root@docker ~]# vim Dockerfile FROM php:7.2-fpm RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-install -j$(nproc) iconv \ && 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 mysqli pdo pdo_mysql //Add this line to support the ability to connect mysql [root@docker ~]# docker build -t phpmysql . //Generate a new image from a Dockerfile file [root@docker ~]# docker rm phpfpm -f //Remove the original phpfpm container [root@docker ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 200.0.0.30 phpmysql //Or the command to create the phpfpm container, just change the mirror to the one you just made [root@docker ~]# cd /wwwroot/html/phpmyadmin/ [root@docker phpmyadmin]# mv config.sample.inc.php config.inc.php [root@docker phpmyadmin]# vim config.inc.php //Write a configuration file for PHP 31 $cfg['Servers'][$i]['host'] = '200.0.0.20'; //Change it to the IP address of the mysql container [root@docker ~]# docker restart phpfpm //Restart php container [root@docker ~]# docker ps | grep phpfpm //Ensure php container is functioning properly 367e73bc70bd phpmysql "docker-php-entrypoi..." 6 minutes ago Up 10 seconds 0.0.0.0:9000->9000/tcp phpfpm
Access Test:
This concludes this post. Thank you for reading it.