簡體   English   中英

如何將 php 和 nginx 容器連接在一起

[英]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 容器位於不同的節點上。

在嘗試了各種方法后,例如:

  • 為 docker-compose 內部的服務定義專用網絡
  • 在 nginx 配置中使用上游定義而不是服務的直接名稱
  • 將 docker 的 127.0.0.11 解析器顯式添加到 nginx

都沒有工作...

原因實際上是在一個封閉的端口: 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

幾個問題:

  • 看來您有看不到其他容器的容器。
  • 似乎容器退出/失敗,這肯定不是因為第一個問題; nginx 仍然可以工作 php-fpm 套接字不可用,它可能會崩潰,但它應該很好地管理這種不可用
  • 確保 php.ini 確實在端口 9000 上打開了 fpm 套接字。
  • 你 index.php 文件腳本沒有用“?>”關閉[但這沒關系]

總結一下,建議您:

  • 考慮 docker swarm 網絡配置 [但您似乎沒有使用 docker swarm]
  • 使用 depends_on 可以幫助 docker 決定首先開始什么,但在你的情況下這不應該是一個問題,nginx 可以等待。 它將僅在 web 用戶請求時使用套接字。

因此,內部 docker 名稱解析似乎是您的問題,並且手動定義網絡似乎是最佳實踐。 在我的情況下,我在給 docker-compose 文件一個特定的網絡名稱並將容器附加到該網絡之前徘徊了太久。 如果容器位於同一個 docker-compose 文件中,它們應該位於為您的組合服務自動生成的相同 yourserver_default 網絡中。

看看https://blog.devsense.com/2019/php-nginx-docker ,他們實際上手動定義了該網絡。

如果你還沒有解決這個問題,最終從頭開始重做所有事情。 否則一切順利!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM