Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用cURL从自身连接到PHP容器?_Php_Docker_Curl_Docker Compose - Fatal编程技术网

如何使用cURL从自身连接到PHP容器?

如何使用cURL从自身连接到PHP容器?,php,docker,curl,docker-compose,Php,Docker,Curl,Docker Compose,当我尝试使用curl-v localhost从容器本身连接到容器时,我得到: *正在尝试127.0.0.1:80。。。 *TCP_节点集 *连接到127.0.0.1端口80失败:连接被拒绝 *正在尝试:1:80。。。 *TCP_节点集 *即时连接失败::1:地址不可用 *正在尝试:1:80。。。 *TCP_节点集 *即时连接失败::1:地址不可用 *无法连接到本地主机端口80:连接被拒绝 *正在关闭连接0 curl:(7)无法连接到本地主机端口80:连接被拒绝 我提到我既不想从容器连接到主机,

当我尝试使用
curl-v localhost
从容器本身连接到容器时,我得到:

*正在尝试127.0.0.1:80。。。
*TCP_节点集
*连接到127.0.0.1端口80失败:连接被拒绝
*正在尝试:1:80。。。
*TCP_节点集
*即时连接失败::1:地址不可用
*正在尝试:1:80。。。
*TCP_节点集
*即时连接失败::1:地址不可用
*无法连接到本地主机端口80:连接被拒绝
*正在关闭连接0
curl:(7)无法连接到本地主机端口80:连接被拒绝
我提到我既不想从容器连接到主机,也不想从主机连接到容器(我只能找到关于这两个用例的信息),而是从容器内部连接到容器本身

这是我的docker-compose.yml:

服务:
php:
建造:
上下文:。
dockerfile:docker/php/dockerfile
端口:
- '8000:80'
卷数:
-/应用程序:/var/www/html
以防万一,我提到我使用的是php:7.3-fpm-alpine图像

我还使用
curl-vphp
(使用容器名称)进行了测试,得到:

*正在尝试172.18.0.5:80。。。
*TCP_节点集
*连接到172.18.0.5端口80失败:连接被拒绝
*无法连接到content_php端口80:连接被拒绝
*正在关闭连接0
curl:(7)无法连接到content_php端口80:连接被拒绝
ping php
正常时:

pingphp(172.18.0.5):56个数据字节
172.18.0.5中的64字节:seq=0 ttl=64时间=0.262毫秒
我还通过在docker-compose.yml中添加
EXPOSE:80进行了测试,得到了相同的结果。
我还使用
curl localhost:8000
进行了测试,使用端口8000得到了相同的结果

我注意到,
docker ps
在9000上设置了一个默认端口:

端口
9000/tcp,0.0.0.0:8000->80/tcp
当我执行curl localhost:9000时,我得到:

curl:(56)Recv故障:对等方重置连接

要从容器本身连接到容器,我缺少什么?

从容器本身访问容器时,您应该能够使用
localhost
,如下所示:

Dockerfile:

FROM httpd:2.4
RUN apt-get update && apt-get install curl
然后:

$ docker build -t temp .
$ docker run --detach --name temp temp
$ docker exec temp curl localhost
<html><body><h1>It works!</h1></body></html>
$docker build-t temp。
$docker运行--分离--名称临时
$docker exec temp curl localhost
它起作用了!

使用尽可能少的设置,对图像进行尝试,以查明问题所在。此外,请确认您的图像可以从外部访问。如果可以从您的主机访问它,但不能使用
localhost
从容器内访问它,则其服务器配置
bind
listen
地址可能会限制它。将其设置为
0.0.0.0
或相当于说“any”。

PHP-FPM需要通过Nginx容器访问,我们不能像使用cURL这样直接访问它

因此,在本例中,我必须在PHP容器的cURL命令中使用Nginx容器名称来获取内容:

curl nginx

你是如何开始容器的,从哪里开始卷曲的?docker run/docker exec?我使用
docker compose up
启动容器。我使用
docker compose exec-u root php bash
进入容器。您使用httpd使我走上了正确的道路。事实上,PHP-FPM需要通过Nginx容器进行访问,我们不能用cURL直接访问它。