如何使用cURL从自身连接到PHP容器? [英] How to connect to PHP container from itself using cURL?

查看:54
本文介绍了如何使用cURL从自身连接到PHP容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I try to connect to the container from itself using curl -v localhost, I get:

*   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

我提到我既不想从容器连接到主机,也不想从主机连接到容器(我只能找到有关这两个用例的信息),而不是从容器内部连接到容器.容器本身.

I mention that I don't want to connect neither from the container to the host, nor from the host to the container (I could only find information about these 2 use cases) but from inside the container to the container itself.

这是我的 docker-compose.yml :

services:
  php:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    ports:
      - '8000:80'
    volumes:
      - ./app:/var/www/html

以防万一,我提到我正在使用图像 php:7.3-fpm-alpine .

Just in case, I mention that I'm using the image php:7.3-fpm-alpine.

我还用 curl -v php (带有容器的名称)进行了测试,我得到了:

I also tested with curl -v php (with the name of the container), I got:

*   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-compose.yml 中添加 EXPOSE:80 进行了测试,得到了相同的结果.
我还使用 curl localhost:8000 进行了测试,使用端口8000得到了相同的结果.

I also tested by adding EXPOSE: 80 in docker-compose.yml, I got the same result.
I also tested with curl localhost:8000, I got the same result with port 8000.

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

I noticed with docker ps there is a default port set on 9000:

PORTS  
9000/tcp, 0.0.0.0:8000->80/tcp  

当我执行 curl localhost:9000 时,我得到:

curl: (56) Recv failure: Connection reset by peer

我无法从自身连接到容器的什么地方?

What am I missing to be able to connect to the container from itself?

推荐答案

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

You should be able to use localhost when accessing the container from itself, as the below shows:

Dockerfile:

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 或等同于说"any".

Try it with your image using the minimal setup possible, to pinpoint the problem. Also, verify that your image is accessible from the outside. If it is reachable from your host, and not reachable from within the container using localhost, then its server configuration bind or listen address is probably limiting it. Set it to 0.0.0.0 or the equivalent of saying "any".

这篇关于如何使用cURL从自身连接到PHP容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆