简体   繁体   English

如何设置两个 docker 容器,以便它们可以相互看到?

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

I have two services defined for docker-compose我为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.我可以 ping 并连接到端口 5672 以与主机 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.以下命令错误,因为它无法连接到 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?如何设置两个 docker 容器,以便它们可以相互看到?

From inside the Docker container, the loopback address 0.0.0.0 refers to the container itself. 从Docker容器内部,环回地址0.0.0.0引用容器本身。 Use your host's IP address to reach the other container. 使用主机的IP地址到达其他容器。

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 以下是有关如何从容器内部访问主机以及Docker提供的各种网络模式的详细说明: 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. 另一种方法是创建一个Docker网络,将两个容器连接到它,然后他们就能够在该网络上相互解析。 Details here: https://docs.docker.com/engine/reference/commandline/network_create/ 详情请访问: 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? @ArashMotamedi如果你是对的,那么他也无法从主机连接到rabbitmq ,不是吗?

@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 . @AndreiCioara我认为您的docker docker-compose很好,问题在于命令 - 因为默认情况下--service-ports <service> docker-compose run并不打开任何端口,除非您使用--service-ports <service>指定它们实际执行的操作,但仅限于对于一次一个服务,因此当您docker-compose run --service-ports celery时, rabbitmq端口关闭。

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 除非你有充分的理由使用run (例如覆盖命令或避免端口冲突),我建议你使用常规

docker-compose up

So the easy answer is to refer to each other by name. 所以简单的答案就是通过名字互相引用。 In your compose file you reference two services: 在您的撰写文件中,您引用了两个服务:

  • rabbitmq 的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 up -d(或只是docker-compose up),它将在他们共享的新创建的网络上创建新容器。 Docker compose then registers both services to the DNS service for that network via an automatic alias. Docker compose然后通过自动别名将两个服务注册到该网络的DNS服务。

So from celery, you could ping rabbitmq via: 所以从芹菜中,您可以通过以下方式ping rabbitmq:

ping rabbitmq and on rabbitmq you could ping celery via ping celery ping rabbitmq和rabbitmq你可以通过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. 您可以通过创建新网络,将它们分配给主机,然后注册别名来手动完成所有操作,但是docker-compose可以完成所有工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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