【问题标题】:How to connect php and nginx containers together如何将php和nginx容器连接在一起
【发布时间】:2022-08-14 01:36:36
【问题描述】:

我正在尝试创建一个简单的 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 将正常工作。

  • 您的 PHP 容器正在退出;你在 Nginx 错误之前看到php exited with code 0

标签: php docker nginx docker-compose dockerfile


【解决方案1】:

您可以尝试在您的nginx 服务中添加depends_on: php,以至少尝试确保nginx 服务在php 服务为Running 之前不会启动。可能依赖项是在需要它的主要服务之后启动的。我认为这是一个竞争条件问题。

【讨论】:

  • 不,我知道了:ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for services.nginx: 'depend_on'
  • 哦,对不起,应该是depends_on。我现在编辑了答案。
  • 谢谢,但仍有问题:nginx exited with code 1
  • 还在说host not found in upstream吗?
  • 实际上是的,nginx | 2022/07/10 09:18:21 [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
【解决方案2】:

我有 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.

在我将所做的所有更改(网络、解析器、上游定义)恢复为原始的简单设置并打开内部节点通信的端口后 - 服务发现开始按预期工作。

码头工人 20.10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2017-02-13
    • 2016-12-27
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多