SoFunction
Updated on 2025-03-03

Implementation steps for docker to build lnmp environment

I have had nothing to do recently, so I used the company's computer to deploy an environment to record the deployment and the pitfalls I stepped on.

Nginx

1. Pull the image and create a container using the image.

You can set the Nginx version you pull by yourself, I use nginx:latest

// Pull the mirrordocker pull nginx:1.18.0

1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete 
0b20d28b5eb3: Pull complete 
1576642c9776: Pull complete 
c12a848bad84: Pull complete 
03f221d9cf00: Pull complete 
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
/library/nginx:1.18.0

// After the installation is completed, you can use the command to view which images are installeddocker images

REPOSITORY        TAG       IMAGE ID       CREATED       SIZE
nginx             latest    605c77e624dd   2 years ago   141MB

2. The host creates the mapping file required by Nginx

This needs to be created in advance, because if you create a container directly using the docker command, you will report an error without a local mapped file

First create the required directory of nginx

[root@localhost ~]# mkdir -pv /lnmp/nginx/{conf/,data,logs,html}
mkdir: created directory ‘/lnmp'
mkdir: created directory ‘/lnmp/nginx'
mkdir: created directory ‘/lnmp/nginx/conf'
mkdir: created directory ‘/lnmp/nginx/conf/'
mkdir: created directory ‘/lnmp/nginx/data'
mkdir: created directory ‘/lnmp/nginx/logs'
mkdir: created directory ‘/lnmp/nginx/html'

[root@localhost ~]# ls /lnmp/nginx/
conf  data  logs html
[root@localhost ~]# 

Create in conf and you also need one of them. These are used after mapping. After creating the nginx configuration file, open it and save the default configuration, because during mapping, this local file will overwrite the files in the container (of course, you can also create the container first and copy the container)

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/ warn;
pid        /var/run/;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx//*.conf;
	server {
		listen 80;
		server_name localhost;
		root /usr/share/nginx/html;
		index   ;

		location / {
			try_files $uri $uri/ /?$query_string;
			autoindex on;
		}
		
		location ~ \.php$ {
            // This is a big pit I stepped on            // If you do not specify the php-fpm running directory, it will be empty, causing nginx to fail to parse the php file            // So here you must specify the run root directory!  !  !			root           /var/www/html;
			fastcgi_pass   php81-fpm:9000;
			fastcgi_index  ;
			fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
			include        fastcgi_params;
		}


		error_log /var/log/nginx/;
		access_log /var/log/nginx/;
	}

}

3. Create a container

There is a point to note here. Before creating, we need to create a Docker network so that Nginx, MySQL and PHP containers can communicate with each other.

docker network create lnmp-network

After that, create the container. After the creation is successful, you can use the ps command to see which containers are there.

docker run -it --name nginx --network lnmp-network -p 80:80 -v D:/lnmp/nginx/conf/:/etc/nginx/ -v D:/lnmp/nginx/conf/:/etc/nginx/ -v D:/lnmp/nginx/html:/usr/share/nginx/html -v D:/lnmp/nginx/logs:/var/log/nginx -d nginx:latest


docker ps
CONTAINER ID   IMAGE                    COMMAND                   CREATED        STATUS             PORTS
                NAMES
a9af314321d6   nginx:latest             "/docker-entrypoint.…"   25 hours ago   Up 57 minutes      0.0.0.0:80->80/tcp                  nginx

The mirror name and address in the command need to be changed to my own. I only mapped port 80 here, and port 443 can also be mapped together. If necessary, add it.

Mysql

There is nothing to say, mysql still has the same process, and it is talented

#Pull Mysql image, use version 5.7 here[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        1.18.0    c2c45d506085   12 months ago   133MB
[root@localhost ~]# docker pull  mysql:5.7.32           
5.7.32: Pulling from library/mysql
a076a628af6f: Pull complete 
f6c208f3f991: Pull complete 
88a9455a9165: Pull complete 
406c9b8427c6: Pull complete 
7c88599c0b25: Pull complete 
25b5c6debdaf: Pull complete 
43a5816f1617: Pull complete 
7065aaa2655f: Pull complete 
b4bc531db40f: Pull complete 
8c3e9d7c9815: Pull complete 
fadfb9734ed2: Pull complete 
Digest: sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b
Status: Downloaded newer image for mysql:5.7.32
/library/mysql:5.7.32
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        1.18.0    c2c45d506085   12 months ago   133MB
mysql        5.7.32    cc8775c0fe94   15 months ago   449MB
[root@localhost ~]# 

#Create a data directory[root@localhost ~]# mkdir -pv /lnmp/mysql/{conf,data,logs}
mkdir: Directory created "/lnmp/mysql"
mkdir: Directory created "/lnmp/mysql/conf"
mkdir: Directory created "/lnmp/mysql/data"
mkdir: Directory created "/lnmp/mysql/logs"
[root@localhost ~]# ls /lnmp/mysql/
conf  data  logs
[root@localhost ~]# 

#Start the mysql container[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
d4c36a62dde4   nginx:1.18.0   "/docker-entrypoint.…"   18 minutes ago   Up 12 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
[root@localhost ~]# docker run -d -p 3306:3306 --name mysql \
> -v /lnmp/mysql/conf:/etc/mysql/ \
> -v /lnmp/mysql/logs:/logs \
> -v /lnmp/mysql/data:/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> mysql:5.7.32
55cc5694695d26327abee0580af2cbcaf28c49f33da8da24aa50c5b049946e01
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
55cc5694695d   mysql:5.7.32   "…"   5 seconds ago    Up 3 seconds    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
d4c36a62dde4   nginx:1.18.0   "/docker-entrypoint.…"   18 minutes ago   Up 12 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp                      nginx
[root@localhost ~]# 

PHP

Actually, it's the same, pulling the mirror

docker pull nfqlt/php81-fpm:latest

Create mapping directories and files

cd /lnmp/php-fpm
mkdir logs
touch 
touch 

Of course, I will never get this configuration file, so I created a temporary container and CP it out haha. If you are interested, you can also operate it like this. I posted the command

// Start the container (not mapped files)docker run -d --name php81-fpm --network lnmp-network -v D:/lnmp/nginx/html:/var/www/html -v D:/lnmp/php-fpm/logs:/var/log/php-fpm nfqlt/php81-fpm:latest

// Copy files in the container to the localdocker cp php81-fpm:/etc/php/8.1/fpm/ D:/lnmp/php-fpm/

// Stop and delete the original containerdocker stop php81-fpm
docker rm php81-fpm

Then, familiar links, create containers

docker run -d --name php81-fpm --network lnmp-network -p 9000:9000 -v D:/lnmp/nginx/html:/var/www/html -v D:/lnmp/php-fpm/:/etc/php/8.1/fpm/ -v D:/lnmp/php-fpm/:/etc/php/8.1/fpm/ -v D:/lnmp/php-fpm/logs/:/tmp/log/ nfqlt/php81-fpm:latest

Note: When mapping this, D:/lnmp/nginx/html:/var/www/html is used to map the address of the running directory (D:/lnmp/nginx/html) that maps the nginx. /var/www/html is the big pit we mentioned above. You need to set the root directory of the php run. If you make a mistake, it will be impossible to parse the php file! !

This is the article about the implementation steps of docker building lnmp environment. For more related content on docker building lnmp environment, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!