1

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.

Here is my docker-compose.yml:

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

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

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

While ping php is OK:

PING php (172.18.0.5): 56 data bytes
64 bytes from 172.18.0.5: seq=0 ttl=64 time=0.262 ms

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.

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

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

When I execute curl localhost:9000 and I get:

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

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

2
  • How did you start the Container and from where did you start the curl? docker run/docker exec? Commented Apr 28, 2020 at 8:40
  • I start the container using docker-compose up. I enter in the container with docker-compose exec -u root php bash. Commented Apr 28, 2020 at 8:58

2 Answers 2

2

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

Dockerfile:

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

And then:

$ docker build -t temp .
$ docker run --detach --name temp temp
$ docker exec temp curl localhost
<html><body><h1>It works!</h1></body></html>

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".

Sign up to request clarification or add additional context in comments.

1 Comment

You made me on the right track with httpd. In fact PHP-FPM needs to be accessed via an Nginx container, we can't access it directly with cURL.
1

PHP-FPM needs to be accessed via an Nginx container, we can't access it directly like this with cURL.

So, in this case I had to use the Nginx container name in my cURL command from the PHP container to get the content:

curl nginx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.