search
HomeOperation and MaintenanceDockerAnalyze how docker builds lnmp environment (php7.4 + nginx)

This article will introduce you to the installation method of docker under Linux, and learn more about the steps of building lnmp environment (php7.4 nginx) with docker. I hope it will be helpful to you!

Analyze how docker builds lnmp environment (php7.4 + nginx)

1. Docker basics

1.1 Docker installation

The installation of docker is very simple, we can directly Use the yum command in the centos system to install:

yum install -y docker

After completion, check the docker version information:

[root@localhost docker]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Fri Mar 19 13:57:09 2021
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Fri Mar 19 13:57:09 2021
 OS/Arch:         linux/amd64
 Experimental:    false
[root@localhost docker]#

Prove that the installation is complete. The docker command is as follows:

systemctl start docker   # 启动docker
systemctl stop docker    # 停止docker
systemctl status docker  # 查看docker状态
systemctl restart docker # 重新启动docker

sudo systemctl enable docker #Set docker to start automatically at boot

Before starting, we need to modify the docker image source:

[root@localhost docker]# vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://ftnejmh3.mirror.aliyuncs.com"]
}

After completion, run systemctl start docker to start the docker service.

2. Docker builds lnmp environment

Introduction to environment software:

1. docker

2. nginx

3. mysql

4. php7.4 [Recommended learning: "PHP Video Tutorial"]

lnmp directory structure built by docker:

docker
│   └── nginx
│   │   └── default.conf #nginx配置文件
│   └── www
│       └── ...  #项目代码

2.1 Docker builds nginx

We can use the docker search nginx command to find the nginx image on Docker Hub. Here we directly pull the official image

[root@localhost ~]# docker pull nginx

After the download is completed, we can The image whose REPOSITORY is nginx is found in the local image list.

[root@localhost www]# docker images
REPOSITORY       TAG       IMAGE ID        CREATED        SIZE
docker.io/nginx  latest    62d49f9bab67    12 days ago    133 MB

Create nginx configuration

[root@localhost ~]# cd /docker/nginx
[root@localhost ~]# touch default.conf
[root@localhost ~]# vim default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /docker/www/lmrs-2008/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /docker/www/lmrs-2008/public;
    }

    location ~ \.php$ {
        root /docker/www/lmrs-2008/public;
        fastcgi_pass   172.17.0.3:9000;    //容器的ip地址
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
fastcgi_pass   172.17.0.3:9000;

View ip information

[root@localhost docker]# docker inspect php | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

Use nginx image to open nginx application container

[root@localhost ~]# docker run -p 80:80 -d --name nginx -v /docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www  --privileged=true nginx

-p 80:80:将容器的80端口映射到主机的80端口
-d 后台运行(守护进程)
--name nginx:将容器命名为nginx
-v 将主机中当前目录下的www挂载到容器的www目录

#查看docker目前的容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
8781afd1bf13        nginx               "/docker-entrypoin..."   About an hour ago   Up 17 minutes       0.0.0.0:80->80/tcp                  nginx

2.2 docker installation php

Like nginx, we can first find the image through docker search php. Here we directly pull the official image, labeled 7.4-fpm. You can choose other versions by yourself

[root@localhost ~]# docker pull php:7.4-fpm

After the download is completed, we can find the image with the REPOSITORY of php and the label of 7.4-fpm in the local image list.

[root@localhost docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              62d49f9bab67        12 days ago         133 MB
docker.io/php       7.4-fpm             41b17b0f90e6        2 weeks ago         405 MB

Use php mirror to open the php-frm application container

[root@localhost docker]# docker run -p 9000:9000 -d --name php -v /docker/www:/docker/www --privileged=true php:7.4-fpm
-p 9000:9000 :将容器的9000端口映射到主机的9000端口
-d 后台运行(守护进程)
--name php:将容器命名为php
-v 将主机中当前目录下的www挂载到容器的www目录

Check the container startup status

[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
cbf73ca0f17c        php:7.4-fpm         "docker-php-entryp..."   About an hour ago   Up About an hour    0.0.0.0:9000->9000/tcp              php
8781afd1bf13        nginx               "/docker-entrypoin..."   About an hour ago   Up 23 minutes       0.0.0.0:80->80/tcp                  nginx

At this point, you can see that nginx and php are running successfully (STATUS is up Indicates that it is running)

Check whether the php-fpm service IP configured by nginx is consistent:

location ~ \.php$ {
    root /docker/www/lmrs-2008/public;
    fastcgi_pass   172.17.0.3:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

In the above configurationfastcgi_pass 172.17.0.3:9000 ;Needs to be consistent with the IP of the php container we are viewing.

After completing the above configuration and confirming that there is no problem starting the container, you can test it:

There may be directory permission problems when accessing laravel. The following is the solution:

[root@localhost docker]# cd /docker/www
[root@localhost www]# chmod -R 777  文件名

Recommended learning: "docker video tutorial"

The above is the detailed content of Analyze how docker builds lnmp environment (php7.4 + nginx). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Docker and Kubernetes: Building Scalable ApplicationsDocker and Kubernetes: Building Scalable ApplicationsApr 28, 2025 am 12:18 AM

Use Docker and Kubernetes to build scalable applications. 1) Create container images using Dockerfile, 2) Deployment and Service of Kubernetes through kubectl command, 3) Use HorizontalPodAutoscaler to achieve automatic scaling, thereby building an efficient and scalable application architecture.

Kubernetes and Docker: A Comparative AnalysisKubernetes and Docker: A Comparative AnalysisApr 27, 2025 am 12:05 AM

The main difference between Docker and Kubernetes is that Docker is used for containerization, while Kubernetes is used for container orchestration. 1.Docker provides a consistent environment to develop, test and deploy applications, and implement isolation and resource limitation through containers. 2. Kubernetes manages containerized applications, provides automated deployment, expansion and management functions, and supports load balancing and automatic scaling. The combination of the two can improve application deployment and management efficiency.

Running Docker on Linux: Installation and ConfigurationRunning Docker on Linux: Installation and ConfigurationApr 26, 2025 am 12:12 AM

Installing and configuring Docker on Linux requires ensuring that the system is 64-bit and kernel version 3.10 and above, use the command "sudoapt-getupdate" and install it with the command "sudoapt-getupdate" and verify it with "sudoapt-getupdate" and. Docker uses the namespace and control groups of the Linux kernel to achieve container isolation and resource limitation. The image is a read-only template, and the container can be modified. Examples of usage include running an Nginx server and creating images with custom Dockerfiles. common

Why Use Docker? Benefits and Advantages ExplainedWhy Use Docker? Benefits and Advantages ExplainedApr 25, 2025 am 12:05 AM

The reason for using Docker is that it provides an efficient, portable and consistent environment to package, distribute, and run applications. 1) Docker is a containerized platform that allows developers to package applications and their dependencies into lightweight, portable containers. 2) It is based on Linux container technology and joint file system to ensure fast startup and efficient operation. 3) Docker supports multi-stage construction, optimizes image size and deployment speed. 4) Using Docker can simplify development and deployment processes, improve efficiency and ensure consistency across environments.

Docker in Action: Real-World Examples and Use CasesDocker in Action: Real-World Examples and Use CasesApr 24, 2025 am 12:10 AM

Docker's application scenarios in actual projects include simplifying deployment, managing multi-container applications and performance optimization. 1.Docker simplifies application deployment, such as using Dockerfile to deploy Node.js applications. 2. DockerCompose manages multi-container applications, such as web and database services in microservice architecture. 3. Performance optimization uses multi-stage construction to reduce the image size and monitor the container status through health checks.

Docker vs. Kubernetes: Use Cases and ScenariosDocker vs. Kubernetes: Use Cases and ScenariosApr 23, 2025 am 12:11 AM

Select Docker in a small project or development environment, and Kubernetes in a large project or production environment. 1.Docker is suitable for rapid iteration and testing, 2. Kubernetes provides powerful container orchestration capabilities, suitable for managing and expanding large applications.

Docker on Linux: Containerization for Linux SystemsDocker on Linux: Containerization for Linux SystemsApr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Docker: The Containerization Tool, Kubernetes: The OrchestratorDocker: The Containerization Tool, Kubernetes: The OrchestratorApr 21, 2025 am 12:01 AM

Docker is a containerization tool, and Kubernetes is a container orchestration tool. 1. Docker packages applications and their dependencies into containers that can run in any Docker-enabled environment. 2. Kubernetes manages these containers, implementing automated deployment, scaling and management, and making applications run efficiently.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.