Why container deployment

It is mainly to achieve elastic expansion and reduce the cost of basic resources. Previously, the forum application of the company was deployed on private cloud and applied for ECS with fixed configuration. The cost of basic resources was high and elastic scaling could not be achieved.

The implementation steps

  1. Basic knowledge of Docker.
  2. Choose the appropriate base image.
  3. Configuration and code copy based on the underlying image, and verification.
  4. Write Dockerfile to facilitate subsequent automatic deployment.

Basic knowledge of Docker

Docker is still the most mainstream and most widely used container, so DOCker is still selected to complete the containerization deployment. Get Started With Docker Take a moment to read it through and get a sense of it before moving on.

Choose the appropriate mirror image

Docker image repository provides many Linux images, such as CentOS, Alpine and so on. When we download, we can choose the Linux image to add various configurations, or we can choose the more mature image in the community. If you choose the community image, it is recommended to choose the one with the most downloads and more stars. The company’s PHP deployment environment is Nginx+php-fpm. For downloads and star numbers, I chose richarvey/nginx-php-fpm. Since the company’s PHP is 5.7, so the mirror version is PHP5, you can choose the latest version.

Configuration and code copy based on the underlying image, and verification

After selecting the image and version, we need a Linux machine as the deployment machine to make and verify the image, and install the Docker client according to CentOS Docker installation. You are advised to configure image acceleration; otherwise, the image download may be slow. Docker pull richarvey/nginx-php-fpm:php5

[root@VM-0-3-centos ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED       SIZE
richarvey/nginx-php-fpm   php5      557c302f7402   4 years ago   229MB
Copy the code

By looking at the image hierarchy of version PHP5, the default startup command is/start.shWhen the container is started, the start.sh script is executed:



For more configurations, see the documentationportalSince Nginx and PHP-FPM are integrated with this image by default, you are advised to read the documentation in detail. It’s a good habit to read the documentation before using open source, and you’ll save yourself a lot of detours.

Using the commanddocker run -itd richarvey/nginx-php-fpm:php5Start the container,docker psThe CONTAINER ID is used in the following commands:

[root@VM-0-3-centos ~]# docker ps
CONTAINER ID   IMAGE                          COMMAND       CREATED          STATUS          PORTS             NAMES
d897f6a22b3b   richarvey/nginx-php-fpm:php5   "/start.sh"   35 seconds ago   Up 34 seconds   80/tcp, 443/tcp   priceless_swirles
Copy the code

After the startup is complete, use the commanddocker exec -it d897f6a22b3b bin/bashDefault webroot is “/var/ WWW/HTML”.“/var/ WWW/HTML” uses VOLUME to share the persistent directory, so the commit image cannot be saved.



Copy the code file into webroot and use the docker copy file commanddocker cp /home/app/helloworld d897f6a22b3b:/home/app/helloworld(Replace the code directory). My code directory is just index. PHP, which looks like this:


      
echo "Vincent,hello world! \n";
? >
Copy the code

How to boot Nginx and PHP-FPM? The last daemon it is running on is the start.sh container it is running on.

# Start supervisord and services
/usr/bin/supervisord -n -c /etc/supervisord.conf
Copy the code

The daemon container is then designed to run Nginx and PHP-FPM. You can run the cat /etc/supervisord.conf command to view the detailed configuration file. The container container is running a script that is designed to send emails and is handled in a container container that is running on the container container, so the container is running on a thread that needs to be modified in the container container configuration file. The container container is running on vi /etc/supervisor. conf to change the container configuration file (ignore this if there is no PHP script running on it). If scheduled tasks exist in the project, modify vi /etc/crontabs/root to add customized scheduled tasks. If no scheduled tasks exist, skip this step. Docker commit -a “Vincent” -m “PHP demo” d897f6a22b3b demo/ PHP :1.0

[root@VM-0-3-centos ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE demo/ PHP 1.0 2c278fad2792 24 seconds ago 229MB Richarvey /nginx-php-fpm php5 REPOSITORY TAG IMAGE ID CREATED SIZE demo/ PHP 1.0 2c278fad2792 24 seconds ago 229MB Richarvey /nginx-php-fpm php5 557c302f7402 4 years ago 229MBCopy the code

This time we start with a mapped external port for easy test access in the browser. Docker run -itd -p 80:80 -e ‘WEBROOT=/home/app/ helloWorld ‘demo/ PHP :1.0:

[root@VM-0-3-centos ~]# docker run -itd -p 80:80 demo/ PHP :1.0
fe5b68eb007c858baf25a4f13009d46bca190ad572b39b235d5ecdc66b8b9c76
[root@VM-0-3-centos ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FE5B68EB007c Demo/PHP :1.0"/start.sh"8 seconds ago Up 7 seconds 0.0.0.0:80->80/ TCP, 443/ TCP jolly_hodgkin d897f6a22b3b richarvey/nginx-php-fpm:php5"/start.sh"   2 hours ago     Up 35 minutes   80/tcp, 443/tcp               priceless_swirles
Copy the code

You can enter the container and use the built-in curl 127.0.0.1 to verify that it is accessible:

[root@VM-0-3-centos app]# docker exec -it d897f6a22b3b bin/bashBash - 4.3 -# curl 127.0.0.1
Vincent,hello world!
Copy the code

Browser access:

The images completed above can be used directly for deployment, but because the system iterates, packaging images with commands like this every time is cumbersome and not in accordance with best practices. Let’s take a look at how Dockerfile does this.

Write Dockerfile to facilitate subsequent automatic deployment

Create a Dockerfile in the /home/app directory. The complete Dockerfile is as follows:

FROM    docker.io/richarvey/nginx-php-fpm:php5
MAINTAINER      Fisher "php demo"

# copy PHP code from current directory. Not required, because the image provides code to configure git automatic pull
COPY  helloworld /var/www/html/
The custom queue file path is added to the container. The scheduled task was added to crontab. Not a must
# RUN echo -e "[include] \nfiles = /var/www/html/commands/queue.ini" >> /etc/supervisord.conf \
# && cat /var/www/html/crontab/crontab-job.ini >> /etc/crontabs/root
Copy the code

Docker build /home/app -t demo/ PHP :1.1

[root@VM-0-3-centos ~]# docker build /home/app -t demo/ PHP :1.1Sending build context to Docker Daemon 16.38kB Step 1/3: FROM docker.io/richarvey/nginx-php-fpm:php5 ---> 557c302f7402 Step 2/3 : MAINTAINER Fisher"php demo"
 ---> Running in8e646f558832 Removing intermediate container 8e646f558832 ---> 7a7b268156d3 Step 3/3 : COPY helloWorld /var/www/html/ --> e7ac2bafe7a8 Successfully built e7ac2bafe7a8 Successfully tagged demo/ PHP :1.1Copy the code

To start the image, runDocker run -itd -p 80:80 Demo/PHP :1.1And then use your browser to access:

conclusion

This practice encountered some problems, mainly as follows:

  1. At the beginning, I did not check docker layers and directly used command when RUNNING/bin/bashStart. sh is not executed and the container is not initialized.
  2. Note that volume persisted webroot directory “/var/www/html”, container copy code file and commit code file are problematic.

So actually read the documentation, there are various default configurations are explained, so the implementation process will get twice the result with half the effort.