在Docker Compose

时间:2017-11-06 22:06:00

标签: php docker nginx docker-compose alpine

我正在摆弄Docker,试图在单独的Alpine容器上运行Nginx和PHP-FPM来设置Docker组合。我的设置位于https://github.com/sparkbuzz/lemp_docker的GitHub上,我的 docker-compose.yml 如下所示:

version: '3'
services:
  alpine_nginx:
    build: ./nginx
    container_name: alpine_nginx
    links:
      - alpine_php
    ports:
      - "80:80"

  alpine_php:
    build: ./php
    container_name: alpine_php
    ports:
      - "9000:9000"

我能够成功构建图像,当我在浏览器中访问localhost时,我可以看到由Nginx提供的index.html。但是,在尝试访问phpinfo.php时,我收到 502 - Bad Gateway 错误

我可以docker exec -it ... /bin/ash进入两个正在运行的实例,似乎服务运行得很愉快,但是,端口9000上的明确的PHP-FPM从未被命中。

来自我的控制台的一些反馈:

Recreating alpine_php ...
Recreating alpine_php ... done
Recreating alpine_nginx ...
Recreating alpine_nginx ... done
Attaching to alpine_php, alpine_nginx
alpine_php      | [06-Nov-2017 21:46:39] NOTICE: fpm is running, pid 1
alpine_php      | [06-Nov-2017 21:46:39] NOTICE: ready to handle connections
alpine_nginx    | 2017/11/06 21:46:46 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.20.0.2:9000", host: "localhost"
alpine_nginx    | 172.20.0.1 - - [06/Nov/2017:21:46:46 +0000] "GET /index.php HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"```

我非常接近,但不确定Nginx为何对PHP上游感到满意。 Nginx配置如下:

server {
  listen 80;
  server_name localhost;

  location / {
    root /var/www/localhost/htdocs/;
    index index.html;
  }

  location ~* \.php$ {
    fastcgi_index index.php;
    fastcgi_pass alpine_php:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  }
}

如何让Nginx与PHP FPM交谈?

1 个答案:

答案 0 :(得分:2)

你在php-fpm配置中听127.0.0.1

将此添加到php/Dockerfile

RUN sed -i 's/127.0.0.1:9000/0.0.0.0:9000/g' /etc/php7/php-fpm.d/www.conf