当我尝试使用curl -v localhost从容器本身连接到容器时,我得到:
* Trying 127.0.0.1:80...
* TCP_NODELAY set
* connect to 127.0.0.1 port 80 failed: Connection refused
* Trying ::1:80...
* TCP_NODELAY set
* Immediate connect fail for ::1: Address not available
* Trying ::1:80...
* TCP_NODELAY set
* Immediate connect fail for ::1: Address not available
* Failed to connect to localhost port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 80: Connection refused我提到,我不想从容器连接到主机,也不想从主机连接到容器(我只能找到关于这两个用例的信息),但是从容器内部连接到容器本身。
这是我的船坞-Compose.yml
services:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
ports:
- '8000:80'
volumes:
- ./app:/var/www/html以防万一,我提到我使用的是图像php:7.3-FPM-高寒。
我还用curl -v php (容器的名称)进行了测试,得到了:
* Trying 172.18.0.5:80...
* TCP_NODELAY set
* connect to 172.18.0.5 port 80 failed: Connection refused
* Failed to connect to content_php port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to content_php port 80: Connection refused虽然ping php是可以的:
PING php (172.18.0.5): 56 data bytes
64 bytes from 172.18.0.5: seq=0 ttl=64 time=0.262 ms我还通过在docker-come.yml中添加EXPOSE: 80进行了测试,得到了相同的结果。
我还用curl localhost:8000进行了测试,在端口8000上得到了相同的结果。
我注意到在docker ps中有一个默认端口设置在9000:
PORTS
9000/tcp, 0.0.0.0:8000->80/tcp 当我执行curl localhost:9000时,我得到:
curl: (56) Recv failure: Connection reset by peer我错过了什么才能从自身连接到容器?
发布于 2020-04-28 09:37:18
在从容器本身访问容器时,您应该能够使用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>试着用你的图像使用尽可能小的设置,找出问题所在。另外,验证您的图像是可以从外部访问的。如果它可以从您的主机访问,并且不能从容器内使用localhost访问,那么它的服务器配置bind或listen地址可能限制了它。将其设置为0.0.0.0或相当于说“任何”。
发布于 2020-04-28 13:34:00
PHP需要通过Nginx容器访问,我们不能用cURL直接访问它。
因此,在本例中,我必须在来自cURL容器的命令中使用Nginx容器名来获取内容:
curl nginxhttps://stackoverflow.com/questions/61475342
复制相似问题