15

I have two containers in one docker-compose.yml. Both are on same on same network. I have added the dependency using "depends_on", I am able to ping the other container, but curl is not working.

version: "2.1"
services:
    web:
            restart: always
            build:
                    context: .
                    dockerfile: web.dockerfile
            ports:
                    - "9000:8080"
            expose:
                    - "8080"

            networks:
                    - test

    nginx_web:
            restart: always
            build:
                    context: .
                    dockerfile: nginx_web.dockerfile
            ports:
                    - "8100:80"
            expose:
                    - "80"
            depends_on:
                    - web
            networks:
                    - test
    networks:
            test:

When I am trying ping from nginx_web container to web, it is working fine. But the curl isn't working. I am getting

curl: (7) Failed to connect to 172.28.0.7 port 8080: Connection refused

And when I am doing curl from the host machine directly to web at port 9000, it is working fine.

10
  • Can you write the exact curl command you are typing? By the way when using networks port exposing is not necessary. Commented Jul 28, 2017 at 8:40
  • I am using : curl http://web:8080/ I know port is not necessary, but just to be doubly sure I am exposing it. Commented Jul 28, 2017 at 8:52
  • The servicename is translated to an IP correctly I don't know what the issue is. Commented Jul 28, 2017 at 9:01
  • Yes, the service name is translated correctly, because ping web works fine and I am getting response from 172.28.0.7 . Commented Jul 28, 2017 at 9:02
  • 2
    Iam sorry, but i must ask this question. On the web container does somebody listen port 8080 and can accept connection? Can you curl web container from web container? Commented Jul 28, 2017 at 9:05

2 Answers 2

12

In your django app, change it from listening on 127.0.0.1 to listen on all interfaces with 0.0.0.0. Docker containers get their own network namespace, so you can't access another container's loopback interface.

Also, your ports do not match. Django is responding on 9000, so from container to container, you need to use port 9000. The mapping to your published port only happens on the docker host. And exposing a port really doesn't do anything for this use case.

Lastly, from your host, you have the ports reversed. It's the port on the host followed by the port inside the container. So "8080:9000".

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

2 Comments

Yes, this was the issue. I figured out this later. I was running my django app at 127.0.0.1, but then I realised that it won't be allowed to curl from outside.
Had the exact same problem. Knowing that container to container communication is using internal ports is counter-intuitive IMHO... so THANKS !!
9

I had the same issue, and I had success when I tried to make the curl without http . You should try doing curl name_of_container:8080/path/to/web/method . That worked for me because inside the docker compose, it doesn't need SSL.

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.