【问题标题】:How can I connect a PHP container with nginx container by using docker-compose?如何使用 docker-compose 将 PHP 容器与 nginx 容器连接起来?
【发布时间】:2017-06-20 21:56:04
【问题描述】:

我正在尝试在 Docker 环境中创建一个简单的 LAMP 堆栈。它通过运行第三方容器 phpdockerio/php71-fpm:latest 来工作,但我目前想要一个安装了 XDebug 的自定义 PHP 容器。

我的问题是,如果我执行docker-compose up,PHP 容器会在启动后退出,然后我的网络服务器容器才能使用它。如何成功告诉 PHP 容器等待我的 nginx 容器连接?


命令行输出

PS C:\playground> docker-compose.exe up
Starting playground_php_1
Starting playground_web_1
Attaching to playground_php_1, playground_web_1
playground_php_1 exited with code 0
playground_web_1 exited with code 1

Dockerfile

FROM php:latest

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

ENTRYPOINT ["docker-php-entrypoint"]

CMD ["php", "-a"]

docker-compose.yml

version: '2'
services:
  php:
    build:
      context: ./etc/php/
      dockerfile: Dockerfile
    volumes:
      - './src:/usr/share/nginx/html'

  web:
    image: nginx:latest
    ports:
      - 8080:80
    volumes:
      - './etc/nginx:/etc/nginx/conf.d'
      - './src:/usr/share/nginx/html'
    depends_on:
      - php

nginx 配置

...
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
...

【问题讨论】:

  • 任何时候你的容器化进程退出,容器都被认为是停止的。如果您的任何一个进程在退出之前在 stdout/stderr 上输出任何内容,您可以通过执行 docker logs <container_name_or_id> 查看该输出
  • @programmerq 谢谢你的提示。 :)

标签: php nginx docker docker-compose


【解决方案1】:

我再次使用我的自定义容器运行它。我将基本图像从php 更改为php-fpm

接下来我必须清除正在运行的容器并删除我机器上已经创建的图像。否则docker-compose 会再次使用错误/旧容器。

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2020-02-13
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多