Docker - build and deploy application services separately (Nginx+Php+Mysql+Redis)

summary

Use docker to build a common example of php, redis, mysql and nginx. Students with a good foundation can skip the previous basic explanation and start the actual battle directly.

Basic knowledge

Mirror operation

1. Check docker version

docker-compose --version
docker version

2. Get image

Generally speaking, the latest tag of the image means that the content of the image will change by tracking the changes of the latest version, and the content is unstable. Therefore, in terms of stability, do not ignore the label information of the image or use the default latest marked image in the production environment.

docker pull php:7.4.25-fpm-buster

The options supported by the pull subcommand mainly include:

-a, --all-tags=true|false: Whether to obtain all images in the warehouse. The default value is No;
--disable-content-trust: Cancel the content verification of the image. The default value is true.

3. View the image

Use the command to list the basic information of the existing image on the local host.

➜  docker docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
mysql                          latest              b05128b000dd        5 days ago          516MB
php                            7.4.25-fpm-buster   918ac8229863        4 weeks ago         405MB
redis                          latest              7faaec683238        5 weeks ago         113MB
nginx                          latest              87a94228f133        6 weeks ago         133MB

TAG information is used to mark different images from the same warehouse. For example, there are multiple images in the ubuntu warehouse. The release versions are distinguished by TAG information, such as 18.04, 18.10, etc.

In addition, use the inspect command to view the details.

docker docker image inspect 192.168.0.177:5000/gzcp:1.3

4. Saving and exporting images

Export image:

docker save -o ~/gzcp.tar 192.168.0.177:5000/gzcp:1.3

Save image:

docker load -i gzcp.tar

If there is something unclear, you can execute the help command

docker image help

5. Delete image

docker rmi 15117995393/gzcp_dev:gzcp_dev

Container operation

1. Login container

bash: interactive mode in the form of bash

docker exec -it d1854ecb30df bash

2. Delete container

docker rm 

By default, the docker rm command can only delete containers that have been terminated or exited, but cannot delete containers that are still running.

3. Export and import of containers

Export mirror command

docker export -o ~/lnmp1.7.tar 804c161615c3

Import mirror command

docker import ~/lnmp1.7.tar -t lnmp

4. View container

View container details command

docker inspect stark_lnmp:v1.0

View the process in the container. b0b5a9371ce4 is the container ID

docker top b0b5a9371ce4

View docker status

docker stats

Docker data management

1. Data volume

Data Volumes is a special directory that can be used by the container. It maps the host operating system directory directly into the container, which is similar to the mount behavior in Linux.

docker volume create -d local test test

2. Bind data volume

In addition to using the volume subcommand to manage data volumes, you can also mount any local path of the host into the container as a data volume when creating a container. The data volume created in this form is called bound data volume.

-The mount option supports three types of data volumes, including:

  • -The mount option supports three types of data volumes, including:
  • Bind: bind the data volume and map it to the path specified by the host;
  • tmpfs: temporary data volume, which exists only in memory.

Next, create a Web container using the training/webapp image and mount a data volume to the / opt/webapp directory of the container:

docker run -d -P --name web --mount type=bind,source=/webapp, destination=/opt

The above command is equivalent to using the old - v flag to create a data volume in the container:

docker run -d -P --name web -v /webapp:/opt/webapp training/webapp bash

In addition, the path of the local directory must be an absolute path, and the path in the container can be a relative path. If the directory does not exist, Docker will automatically create it.

3. Data volume container

If users need to share some continuously updated data among multiple containers, the simplest way is to use the data volume container. The data volume container is also a container, but its purpose is to provide data volumes for other containers to mount.

Create a data volume container dbdata and mount a data volume in it to / dbdata:

docker run -it -v /dbdata --name dbdata alpine

Then, you can use -- volumes from in other containers to mount the data volumes in the dbdata container, for example, create two containers db1 and db2, and mount the data volumes from the dbdata container.

docker run -it --volumes-from dbdata --name db1 alpine

Note: the container on which the data volume is mounted using the -- volumes from parameter does not need to be kept running.

Port mapping and container interconnection

1. Access container application from outside

Add mapping port - p host port when starting container: container port

docker run -it -d -p 6379:6379 redis bash 

2. The interconnection mechanism enables convenient mutual visits

Using the -- link parameter enables safe interaction between containers.

docker run -it --link redis-test:74-fpm b6086f719fea bash

In actual combat, Mysql, Redis, PHP and Nginx applications are installed and deployed separately

Execute the command to view the local image, stark_lnmp:v1.0 is that I integrated lnmp1 7 image, students in need can get the image address from me

➜  Docker docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
stark_lnmp                     v1.0                279e576fa083        4 weeks ago         5.43GB

Execute command:

docker run -d -i -t --privileged=true --name lnmp -p 90:80 -p 453:443 -p 6389:6379 -p 11221:11211 -p 3316:3306 -v /Users/stark/ChangPei/Docker/lnmp/wwwroot:/home/wwwroot stark_lnmp:v1.0 bash

Log in to docker:

docker exec -it 21e94b048331 bash

Start lnmp

[root@7d63a5989e81 default]# lnmp
+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              https://lnmp.org             |
+-------------------------------------------+
Usage: lnmp {start|stop|reload|restart|kill|status}
Usage: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}
Usage: lnmp vhost {add|list|del}
Usage: lnmp database {add|list|edit|del}
Usage: lnmp ftp {add|list|edit|del|show}
Usage: lnmp ssl add
Usage: lnmp {dnsssl|dns} {cx|ali|cf|dp|he|gd|aws}
Usage: lnmp onlyssl {cx|ali|cf|dp|he|gd|aws}

mysql test:

$link = mysqli_connect('127.0.0.1', 'root', '12345678','test');

$sql = "select `id`,`page_name` from `t_page`  limit 0,5";

$result = mysqli_query($link,$sql);

while ( $row=mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
    echo "id:".$row['id'].",page_name:".$row['page_name'].PHP_EOL;
}

Execution result:

[root@7d63a5989e81 default]# php mysql.php
id:1,page_name:Company profile
id:2,page_name:Enterprise honor
id:3,page_name:development history
id:4,page_name:contact us
id:5,page_name:Talent recruitment

Installation services

There were many problems in the previous installation. I guess there is no network interconnection to the php program??!!

Mysql,

docker pull mysql:5.6

docker run -itd --name mysql1 -p 3326:3306 -e MYSQL_ROOT_PASSWORD=rootroot mysql:5.6

redis

docker pull redis:6.2.6

2. Modify the default configuration

bind 127.0.0.1 #Comment out this part, which restricts redis to local access only

protected-mode no #The default is yes. The protected mode is enabled and local access is restricted

daemonize no#By default, no is changed to yes, which means that it is started as a daemon and can be run in the background. Unless the kill process is changed to yes, redis will fail to start in the configuration file mode

databases 16 #Number of databases (optional). I modified this to check whether it takes effect..

dir  ./ #Enter the local redis database storage folder (optional)

appendonly yes #redis persistence (optional)

3. Start redis

docker run -p 6399:6379 --name redis1 -v /Users/stark/ChangPei/Docker/redis/redis1.conf:/etc/redis/redis.conf -v /Users/stark/ChangPei/Docker/redis/data:/data -d redis:6.2.6 redis-server /etc/redis/redis.conf --appendonly yes

php+Nginx

docker pull php:7.4-fpm

2. Operation

docker run --name  phpfpm1 -v /Users/stark/ChangPei/Docker/nginx/www:/www  -d php:7.4-fpm

3. Install Nginx

docker run --name runoob-php-nginx -p 8083:80 -d \
    -v /Users/stark/ChangPei/Docker/nginx/www:/usr/share/nginx/html:ro \
    -v /Users/stark/ChangPei/Docker/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
    --link phpfpm1:php \
    nginx:1.20

remaining problems

The redis service cannot be linked in the PHP script, but it can be used in the software?!?!?!?!

How to install the extensions required by PHP?!?!?!?!

Added by ttroy on Fri, 28 Jan 2022 12:33:18 +0200