有我的docker-compose.yml
version: '2'
services:
web:
image: nginx:latest
ports:
- "8018:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
- /private/var/log/nginx:/var/log/nginx
- /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
networks:
- code-network
php:
image: php:fpm
volumes:
- ./code:/code
- ./php-fpm.conf:/usr/local/etc/php-fpm.conf
- ./www.conf:/usr/local/etc/php-fpm.d/www.conf
- /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
networks:
- code-network
networks:
code-network:
driver: bridge
在site.conf中,我这样写fastcgi_pass unix:/var/run/php7-fpm.sock;
我还在www.conf中将监听地址更改为listen = /var/run/php7-fpm.sock
。
在我的MAC中,文件夹/ private / var / run中有一个名为php7-fpm.sock的文件,模式为666
我运行docker-compose up -d
后,容器运行成功。但是当我访问http://localhost:8018时,它返回502.在我检查了nginx错误日志后,我发现了这个
2017/11/01 13:08:39 [错误] 6#6:* 1连接()到unix:/var/run/php7-fpm.sock连接上游时失败(111:拒绝连接),客户端:172.18.0.1,服务器:localhost,请求:“GET / HTTP / 1.1”,上游:“fastcgi:// unix:/var/run/php7-fpm.sock:”,主机:“localhost:8018”< / p>
顺便说一句,在我尝试使用unix套接字模式之前。我成功用tcp / ip模式访问http://localhost:8018。
答案 0 :(得分:5)
检查How to set up Apache2 and PHP-FPM via unix socket?后,我将docker-compose.yml更改为
version: '2'
services:
web:
image: nginx:latest
ports:
- "8018:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
- /private/var/log/nginx:/var/log/nginx
- "phpsocket:/var/run"
networks:
- code-network
php:
image: php:fpm
volumes:
- ./code:/code
- ./php-fpm.conf:/usr/local/etc/php-fpm.conf
- ./www.conf:/usr/local/etc/php-fpm.d/www.conf
- ./zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
- "phpsocket:/var/run"
networks:
- code-network
networks:
code-network:
driver: bridge
volumes:
phpsocket:
将zz-docker.conf覆盖为
[global]
daemonize = no
[www]
listen = /var/run/php7-fpm.sock
listen.mode = 0666
最后当我访问http://localhost:8018时,phpinfo页面出现了!
答案 1 :(得分:0)
我认为问题在于您尝试共享包含php7-fpm.sock
删除/private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
而是从您的nginx主机配置文件中引用服务主机名。
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass php:9000;
}
特别是,通知php:9000
是指给予您的php服务的动态主机名上的端口9000,它将可解析为网络中的每个其他容器。