docker nginx 和 php,php 无法连接到本地主机 [英] docker nginx and php, php cant connect to localhost
问题描述
我已经创建了一个对端口 8080:80 开放的 nginx 容器所以我可以从主机访问它.
I have created an nginx container that is open to port 8080:80 so I could access it from the host.
它连接到具有开放端口 9000:9000 的 php fpm 容器
it is connected to php fpm container that has an open port 9000:9000
nginx 与 php 一起成功运行.
nginx successfully runs with php.
我的问题是 php 尝试访问 localhost:8080但问题是php localhost:8080无效,需要连接nginx容器.
My problem is that php tries to access localhost:8080 but the problem is that the php localhost:8080 is not valid, it needs to connect to the nginx container.
这是我的 wordpress 网站上的错误:你可以看到那里有一些很时髦的东西......下面我将附上我的 docker-compose.yml
here is the error on my wordpress site: you can see that something is funky there... below I'll attach my docker-compose.yml
从 http://下载安装包/localhost:8080/wp-content/themes/realtyspace/plugins/advanced-custom-fields-pro.zip...
下载失败.cURL 错误 7:无法连接到本地主机端口 8080:连接被拒绝
Download failed. cURL error 7: Failed to connect to localhost port 8080: Connection refused
docker-compose.yml
docker-compose.yml
version: '2'
services:
my-nginx:
build: .
volumes:
- ./../:/var/www/html
ports:
- "8080:80"
links:
- my-php
my-php:
build:
context: .
dockerfile: Dockerfile.php-fpm
volumes:
- ./../:/var/www/html
ports:
- "9000:9000"
links:
- my-mysql
my-mysql:
image: mariadb:5.5
volumes:
- /var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp
MYSQL_DATABASE: wp
MYSQL_USER: wp
MYSQL_PASSWORD: wp
推荐答案
使用docker内部网络,配置php访问http://my-nginx:80
.
Use docker's internal networking and configure php to access http://my-nginx:80
.
localhost
将解析为 php 容器本身的独立 IP,而不是运行所有内容的 Docker 主机的 IP.并试图通过 http://dockerhost:8080
将导致不可移植的 docker-compose.yml 和 iptables 防火墙和 nat 规则的可能问题,这些问题比它们的价值更麻烦.使用 v2 compose 文件的价值在于,您可以在 Docker 内部获得一个隔离网络,其中每个容器的 DNS 解析都可以相互协作.
localhost
will resolve to the isolated IP of the php container itself, not that of the Docker host that's running everything. And trying to pass http://dockerhost:8080
will result in a non-portable docker-compose.yml and likely issues with iptables firewall and nat rules that are more trouble than they are worth. The value of using the v2 compose files is that you get an isolated network internal to Docker with DNS resolution of each of your containers to work with each other.
这篇关于docker nginx 和 php,php 无法连接到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!