关于网络:多个docker-compose项目之间的通信

Communication between multiple docker-compose projects

我在两个不同的文件夹中有两个单独的docker-compose.yml文件:

  • ~/front/docker-compose.yml
  • ~/api/docker-compose.yml

如何确保front中的容器可以将请求发送到api中的容器?

我知道可以使用docker run为单个容器设置--default-gateway选项,以便可以为该容器分配特定的IP地址,但是当使用docker-compose时似乎没有此选项。

目前,我最终要执行docker inspect my_api_container_id并查看输出中的网关。 它可以工作,但问题是该IP是随机分配的,因此我不能依靠它。

因此,此问题的另一种形式可能是:

  • 我可以使用docker-compose将固定IP地址分配给特定容器吗?

但最后我要照顾的是:

  • 两个不同的docker-compose项目如何相互通信?


您只需要确保要互相交谈的容器在同一网络上即可。网络是一流的docker构造,并不特定于组合。

1
2
3
4
5
6
7
8
9
10
# front/docker-compose.yml
version: '2'
services:
  front:
    ...
    networks:
      - some-net
networks:
  some-net:
    driver: bridge

...

1
2
3
4
5
6
7
8
9
10
# api/docker-compose.yml
version: '2'
services:
  api:
    ...
    networks:
      - front_some-net
networks:
  front_some-net:
    external: true

Note: Your app’s network is given a name based on the"project name", which is based on the name of the directory it lives in, in this case a prefix front_ was added

然后,他们可以使用服务名称互相交谈。在front中,您可以执行ping api,反之亦然。


只不过是@ johnharris85的好答案,
当您运行docker compose文件时,将创建一个" default"网络
因此您可以将其作为外部网络添加到其他撰写文件中:

1
2
3
4
5
# front/docker-compose.yml
version: '2'
  services:  
    front_service:
    ...

...

1
2
3
4
5
6
7
8
9
10
# api/docker-compose.yml
version: '2'
services:
  api_service:
    ...
    networks:
      - front_default
networks:
  front_default:
    external: true

对我来说,这种方法更合适,因为我不拥有第一个docker-compose文件,而是想与之通信。


更新:从撰写文件版本3.5开始:

现在可以使用:

1
2
3
4
5
6
7
8
9
10
11
12
version:"3.5"
services:
  proxy:
    image: hello-world
    ports:
      -"80:80"
    networks:
      - proxynet

networks:
  proxynet:
    name: custom_network

docker-compose up -d将加入一个名为" custom_network"的网络。如果不存在,它将被创建!

1
2
3
root@ubuntu-s-1vcpu-1gb-tor1-01:~# docker-compose up -d
Creating network"custom_network" with the default driver
Creating root_proxy_1 ... done

现在,您可以执行以下操作:

1
2
3
4
5
6
7
8
9
10
version:"2"
services:
  web:
    image: hello-world
    networks:
      - my-proxy-net
networks:
  my-proxy-net:
    external:
      name: custom_network

这将创建一个将在外部网络上的容器。

我在文档中找不到任何参考,但是它有效!


来自api的所有容器都可以通过以下配置加入front默认网络:

1
2
3
4
5
6
7
8
# api/docker-compose.yml

...

networks:
  default:
    external:
      name: front_default

请参阅Docker Compose指南:使用现有网络(请参阅底部)


先前的发布信息是正确的,但是没有有关如何链接容器的详细信息,这些容器应以" external_links"的形式连接。

希望这个例子让您更清楚:

  • 假设您有带两个服务(svc11和svc12)的app1 / docker-compose.yml和带两个以上服务(svc21和svc22)的app2 / docker-compose.yml,并假设您需要以交叉方式连接:

  • svc11需要连接到svc22的容器

  • svc21需要连接到svc11的容器。

因此配置应如下所示:

这是app1 / docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2'
services:
    svc11:
        container_name: container11
        [..]
        networks:
            - default # this network
            - app2_default # external network
        external_links:
            - container22:container22
        [..]
    svc12:
       container_name: container12
       [..]

networks:
    default: # this network (app1)
        driver: bridge
    app2_default: # external network (app2)
        external: true

这是app2 / docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2'
services:
    svc21:
        container_name: container21
        [..]
        networks:
            - default # this network (app2)
            - app1_default # external network (app1)
        external_links:
            - container11:container11
        [..]
    svc22:
       container_name: container22
       [..]

networks:
    default: # this network (app2)
        driver: bridge
    app1_default: # external network (app1)
        external: true

从Compose 1.18(规范3.5)开始,您可以使用自己的自定义名称覆盖所有需要的Compose YAML文件的默认网络。只需向其添加以下内容即可:

1
2
3
networks:
  default:
    name: my-app

The above assumes you have version set to 3.5 (or above if they don't deprecate it in 4+).

其他答案也一样;这是一个简化的摘要。


我将使用以下命令将所有容器同时组合在一起,以确保将所有容器docker-compose都添加到同一网络中:

1
docker compose --file ~/front/docker-compose.yml --file ~/api/docker-compose.yml up -d


For using another docker-compose network you just do these(to share networks between docker-compose):

  • Run the first docker-compose project by up -d
  • Find the network name of the first docker-compose by: docker network ls(It contains the name of the root directory project)
  • Then use that name by this structure at below in the second docker-compose file.
  • 第二个docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    version: '3'
    services:
      service-on-second-compose:  # Define any names that you want.
        .
        .
        .
        networks:
          - <put it here(the network name that comes from"docker network ls")>

    networks:
      - <put it here(the network name that comes from"docker network ls")>:
        external: true

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    version: '2'
    services:
      bot:
        build: .
        volumes:
          - '.:/home/node'
          - /home/node/node_modules
        networks:
          - my-rede
        mem_limit: 100m
        memswap_limit: 100m
        cpu_quota: 25000
        container_name: 236948199393329152_585042339404185600_bot
        command: node index.js
        environment:
          NODE_ENV: production
    networks:
      my-rede:
        external:
          name: name_rede_externa