[英]How to connect php and nginx containers together
我正在嘗試創建一個簡單的 docker 項目並連接 PHP 和 Nginx 容器進行測試。 但我收到了這個錯誤:
Building php
Sending build context to Docker daemon 2.048kB
Step 1/1 : FROM php:latest
---> 52cdb5f30a05
Successfully built 52cdb5f30a05
Successfully tagged test_php:latest
WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building nginx
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM nginx:latest
---> 55f4b40fe486
Step 2/2 : ADD default.conf /etc/nginx/conf.d/default.conf
---> 20190910ffec
Successfully built 20190910ffec
Successfully tagged test_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating php ... done
Creating nginx ... done
Attaching to php, nginx
php | Interactive shell
php |
nginx | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
php | php > nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
php exited with code 0
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx | 2022/07/10 05:34:07 [emerg] 1#1: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:14
nginx | nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:14
nginx exited with code 1
這是項目的完整目錄結構:
- docker
-- nginx
-- default.conf
-- Dockerfile
-- php
-- Dockerfile
- src
-- index.php
docker-compose.yml
這是我使用的所有文件及其內容:
# docker/nginx/default.conf
server {
listen 80;
index index.php index.htm index.html;
root /var/www/html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
# docke/nginx/Dockerfile
FROM nginx:latest
ADD default.conf /etc/nginx/conf.d/default.conf
# docker/php/Dockerfile
FROM php:latest
# src/index.php
<?php
echo phpinfo();
# docker-compose.yml
version: "3.8"
services:
nginx:
container_name: nginx
build: ./docker/nginx
command: nginx -g "daemon off;"
links:
- php
ports:
- "80:80"
volumes:
- ./src:/var/www/html
php:
container_name: php
build: ./docker/php
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
主要問題發生在我想將 PHP 容器連接到項目並且沒有 PHP 時,Nginx 將正常工作。
您可以嘗試在您的nginx
服務中添加depends_on: php
以至少嘗試確保nginx
服務在php
服務正在Running
之前不會啟動。 可能依賴項是在需要它的主要服務之后啟動的。 我認為這是一個競爭條件問題。
我有 3 個節點,其中 nginx 和 php 容器位於不同的節點上。
在嘗試了各種方法后,例如:
都沒有工作...
原因實際上是在一個封閉的端口: https://docs.docker.com/engine/swarm/networking/#firewall-considerations
參與 swarm 的 Docker 守護進程需要能夠通過以下端口相互通信:
Port 7946 TCP/UDP for container network discovery.
Port 4789 UDP for the container overlay network.
在我將所做的所有更改(網絡、解析器、上游定義)恢復為原始的簡單設置並打開內部節點通信的端口后 - 服務發現開始按預期工作。
Docker 20.10
幾個問題:
總結一下,建議您:
因此,內部 docker 名稱解析似乎是您的問題,並且手動定義網絡似乎是最佳實踐。 在我的情況下,我在給 docker-compose 文件一個特定的網絡名稱並將容器附加到該網絡之前徘徊了太久。 如果容器位於同一個 docker-compose 文件中,它們應該位於為您的組合服務自動生成的相同 yourserver_default 網絡中。
看看https://blog.devsense.com/2019/php-nginx-docker ,他們實際上手動定義了該網絡。
如果你還沒有解決這個問題,最終從頭開始重做所有事情。 否則一切順利!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.