Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to curl from one container to another

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.

like image 715
reetesh11 Avatar asked Jul 28 '17 08:07

reetesh11


People also ask

Can two containers talk to each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Can multiple containers use the same volume?

Share Data with Volumes. Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.


1 Answers

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

like image 98
BMitch Avatar answered Sep 23 '22 22:09

BMitch