Docker notes 2 — save your own LNMP

Time:2021-1-17

As a server developer, it is inevitable that we need to configure our own server environment in our daily study and work, and even different projects need different custom environments. The birth of docker technology can package the server environment we need well, so as to avoid various server errors caused by repeated configuration or configuration errors. In this chapter, we will implement and record: How to package the customized docker container as an image to realize environment migration and reuse.

Note: if you don’t know much about docker, or if you need to explain some basic commands, please take a look at docker note 1 – Introduction (first knowledge + build + use)Blog: docker note 1 – Introduction (first knowledge + build + use)

Virtual machine environment: centos7

1、 Enter docker container

//Install docker
$ yum -y install docker 
//Start the service
$ systemctl start docker
//Pull centos7 image from docker image library
$ docker pull centos:centos8.2.2004
//View image
$ docker images
//Run container
$ docker run -itd --name centos-one centos:centos8.2.2004
//View containers
$ docker ps
//Enter the container
$docker exec - it [container ID: 57cb4276a2f9] / bin / Sh

Docker notes 2 -- save your own LNMP
PS: if you get stuck before this, please refer to: docker note 1 – Introduction (first knowledge + build + use)::Blog: docker note 1 – Introduction (first knowledge + build + use)

2、 Building LNMP in container

2.1 install and start nginx

//Install nginx
$ yum install nginx
//Start nginx. If you can't find your own nginx startup script, you can use find / - name nginx
$ ./usr/sbin/nginx 
//Test nginx
$ curl 127.0.0.1

Docker notes 2 -- save your own LNMP

Docker notes 2 -- save your own LNMP

2.2. Install PHP

//Install PHP
$ yum install php

Docker notes 2 -- save your own LNMP

2.3 prepare a PHP file for verification

$ cd /usr/share/nginx/html
$ echo "<?php phpinfo();" >> index.php

2.4 start FPM

//Find FPM
$ find / -name php-fpm
//Start FPM
$ /usr/sbin/php-fpm
//View process
$ ps -aux | grep php-fpm

Docker notes 2 -- save your own LNMP

2.5 configure nginx associated FPM

//Found nginx.conf
$ find / -name nginx.conf
location ~ \.php(.*)$ {
    fastcgi_pass   127.0.0.1:9000;
}

Docker notes 2 -- save your own LNMP

//Restart nginx
$ ./usr/sbin/nginx -s reload
//Test it
$ curl 127.0.0.1:80/index.php

Docker notes 2 -- save your own LNMP

3、 Storage container

//Exit container
$ exit;
//View containers
$ docker ps
//Generate image based on container ID (- M image name - a author name [container ID] [new container label])
$ docker commit -m 'my_lnmp' -a 'guoqf' 57cb4276a2f9 centos:guoqf_lnmp
//Save the image with the Save command
$ docker save centos:guoqf_lnmp > /root/guoqf_lnmp.tar

Docker notes 2 -- save your own LNMP

Docker notes 2 -- save your own LNMP

4、 Test image

4.1. Delete the image and restart docker

//Delete image
$ docker rmi 
//Restart docker service
$ systemctl restart docker

Docker notes 2 -- save your own LNMP

4.2. Operation container

//Import image
$ docker load < gqf_lnmp.tar
//Run container
$ docker run -itd --name centos-4 centos/guoqf_lnmp:latest
//Enter the container
$docker exec - it [container ID] / bin / Sh

4.3. Restart fast CGI and LNMP

4.4 view container IP address

$ ifconfig
$ exit

Docker notes 2 -- save your own LNMP

4.4 access services through host machine

Docker notes 2 -- save your own LNMP

4.5 access services through Internet IP

Docker notes 2 -- save your own LNMP

At this time, it is found that the docker service cannot be directly exposed. After some understanding, the network principle of docker is as follows:

Docker notes 2 -- save your own LNMP
After consulting the information, we found that we can do a port mapping (between the host and the container) when generating the container, that is, add – P after the run command

5、 Port mapping

Host:

$ docker run -itd --name centos-4 -p 80:80 centos/guoqf_lnmp:latest

Docker notes 2 -- save your own LNMP

Finally, try again to visit:

Docker notes 2 -- save your own LNMP

So far, we have learned how to use docker to build our own project running environment and save it for subsequent transplantation and rapid construction;
In this way, the image is stored in its own server, with relatively high privacy and security, and low single machine operation cost;
In the next issue, we will study how to use docker file to upload our own image, so as to cope with the rapid construction of server cluster environment! ~

Reference & Tool Library:

1)dockerHub
2)Teach you how to modify the port mapping of the running docker container
3) The whole process of docker installing an LNMP

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]