0

I have two services defined for docker-compose

version: '3'

services:
    celery:
        build:
            context: .
            dockerfile: ./docker/celery/Dockerfile
        command: celery -A api.tasks worker -l info
    rabbitmq:
        image: "rabbitmq:3-management"
        ports:
            - "5672:5672"
            - "15672:15672"
        hostname: "0.0.0.0"

I can start the first service

docker-compose run --service-ports rabbitmq

And everything works well. I can ping and connect to port 5672 for communication from host os.

$ curl 0.0.0.0:5672
AMQP

However, the second service cannot see that port. The following command errors because it cannot connect to 0.0.0.0:5672.

docker-compose run --service-ports celery

How do I setup two docker containers, such that they can see each other?

Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62

2 Answers2

0

From inside the Docker container, the loopback address 0.0.0.0 refers to the container itself. Use your host's IP address to reach the other container.

Here's an extensive explanation on how to reach your host from inside a container and various network modes that Docker offers: https://stackoverflow.com/a/24326540/417866

Another approach would be to create a Docker Network, connect both your containers to it, and then they'll be able to resolve each other on that network. Details here: https://docs.docker.com/engine/reference/commandline/network_create/

Arash Motamedi
  • 9,284
  • 5
  • 34
  • 43
0

So the easy answer is to refer to each other by name. In your compose file you reference two services:

  • rabbitmq
  • celery

if you use docker-compose up -d (or just docker-compose up) it will create the new containers on a newly created network they share. Docker compose then registers both services to the DNS service for that network via an automatic alias.

So from celery, you could ping rabbitmq via:

ping rabbitmq and on rabbitmq you could ping celery via ping celery

This applies to all network communications as it's just name resolution. You can accomplish this all manually by creating a new network, assigning them to the hosts, and then registering aliases, but docker-compose does all the hard work.

Dockstar
  • 1,005
  • 11
  • 15