简体   繁体   中英

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

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?

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/

@ArashMotamedi if you were right, then he wouldnt be able to connect to the rabbitmq from the host either, would he?

@AndreiCioara I think your docker-compose is fine, the problem is with the command - since docker-compose run by default doesnt open any ports, unless youve specified them with --service-ports <service> what you actually did, but only for one service at a time, therefore the rabbitmq ports closed when you run docker-compose run --service-ports celery .

unless you have a very good reason to use run (such as override the command or avoid ports collision) i recommend you use the regular

docker-compose up

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM