Docker deploys nginx php application
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file
version: '3'
services:
nginx:
container_name: myweb
image: nginx
ports:
- '80:80'
volumes:
- '/etc/nginx/:/etc/nginx' # 配置文件
- '/var/log/nginx/:/var/log/nginx/' # 日志
- '/data/php/yxyy/html:/data/php/yxyy/html' # 项目目录
php:
container_name: myphp
image: php:7.2-fpm
volumes:
- '/data/php/yxyy/html/:/data/php/yxyy/html/'
- '/usr/local/php7.2/:/usr/local/etc/'
The following is the entire process of the build. The installation of dockers will not be recorded. The following records the process of building nginx+php
Deploy nginx
First install a temporary nginx container
$ docker run --name tmpweb -d -p 80:80 nginx
Enter 127.0.0.1 in the browser to display the familiar interface and enter the tmpweb container
$ docker exec -it tmpweb /bin/bash
Search Configuration File Location
$ find / -name nginx.conf
# 在 /ect/nginx 中找到配置文件
Copy the nginx configuration in the container to the host machine/ect/nginx
$ docker cp tmpweb:/ect/nginx /ect/nginx
Modify the nginx configuration, such as the site root directory /etc/nginx/conf.d/default.conf. My project is in /data/php/yxyy/html
Delete the tmpweb container and start a container to mount the custom configuration file
docker run --name myweb -d -p 80:80 \
-v /ect/nginx:/ect/nginx \
-v /var/log/nginx/:/var/log/nginx/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ nginx
Enter 127.0.0.1 in the browser to verify whether it is normal
Deploy PHP
The installation process of php is similar to that of nginx. The image I use is php:7.2-fpm. Here is my installation process:
$ docker run --name tmpphp -d -p 80:80 php:7.2-fpm # 安装临时php:7.2-fpm镜像
$ docker exec -it tmpphp /bin/bash # 进入容器
$ find / -name php-fpm.conf
#在 /usr/local/etc/ 中找到配置文件
$ docker cp tmpphp:/usr/local/etc/ /usr/local/php7.2/ # 拷贝一份容器内的配置到宿主机
$ docker run --name myphp -d -p 9000:9000 \
-v /usr/local/php7.2/:/usr/local/etc/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ php:7.2-fpm
At this point, I think it's OK. The browser accesses it and the result shows 502
Solving 502
Check the nginx log as follows:
*19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
There is a connection problem. Check whether the PHP container is started:
$ docker ps
# 结果显示php容器是启动的
View the PHP container log:
$ docker logs myphp
# 也没有错误
View the host process:
$ ps -ef | grep php
# php 主进程和worker 进程都在运行
Now that these processes are already there, the problem will only be in the communication between them. Telnet on the host:
$ telnet 127.0.0.1 9000
# 可以正常的链接
Can't containers communicate with each other? I found that they can't communicate, but I want to verify it:
# 首先进入myweb容器telnet 127.0.0.1 9000,没有telnet
# OK,然后安装。 安装后,telnet 127.0.0.1 9000 拒绝。 来这里确认无法直接通信
Rerun the nginx container and add --link myphp
$ docker run --name myweb -d -p 80:80 --link myphp \
-v /ect/nginx:/ect/nginx \
-v /var/log/nginx/:/var/log/nginx/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ nginx
Ideals are beautiful, but reality is cruel and we still cannot communicate. According to the data, link will be abandoned. The docker I installed is relatively new. I think this may be the reason. Use docker compose
cluster management to start it. This is the docker-compose.yml configuration:
version: '3'
services:
nginx:
container_name: myweb
image: nginx
ports:
- '80:80'
volumes:
- '/etc/nginx/:/etc/nginx' # 配置文件
- '/var/log/nginx/:/var/log/nginx/' # 日志
- '/data/php/yxyy/html:/data/php/yxyy/html' # 项目目录
php:
container_name: myphp
image: php:7.2-fpm
volumes:
- '/data/php/yxyy/html/:/data/php/yxyy/html/'
- '/usr/local/php7.2/:/usr/local/etc/'
After a lot of hard work, I still can't see the result of 250. At this time, I want to smash the computer and enter the container:
$ telnet 127.0.0.1 9000
# 还是连接不上? 如果将 127.0.0.1 更改为容器名称会发生什么?
$ telnet myphp 9000
# 没想到,就在这个时候,一种幸福的感觉从容不迫的袭来。 幸福来得太突然
Modify nginx configuration, change 127.0.0.1 to myphp (container name), restart, the highlight moment has arrived. However, in fact, I thought too much. 502 is solved, 404 appears unexpectedly.
Solving 404
File not found
When I saw the 404 error, my first reaction was that the path was wrong. After repeated confirmation, the path was OK. I think it can only be a permission problem. Check the nginx log:
*32 FastCGI sent in stderr: "Primary script unknown" while reading response header
ls -l
You really don't have permission to see it. You smile when you see that you don't have permission. That's kind of funny. Give me 777 permissions. Unfortunately, I still can't. Execute with root user.
- Modify the configuration www.conf of php FMP: user=root, group=root;
- Modify nginx.conf: user=root. Restart the container to view the result.
View the container logs docker logs phpxxx display
ERROR: [pool www] please specify user and group other than root
php cannot be executed by root. I changed it to nginx. But the result is still not good.
cat /etc/passwd Check that there is no such user, only the WWW data user of PHP. Modify the configuration of nginx and php, change to the WWW data user, and restart.
Finally! 😊
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/opersys_10102.html
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:100 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:164 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:139 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application
Copy files from host to Docker container
Publish Date:2025/03/25 Views:127 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:99 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:123 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories