多个码头工程项目之间的沟通 [英] Communication between multiple docker-compose projects

查看:102
本文介绍了多个码头工程项目之间的沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two separate docker-compose.yml files in two different folders:


  • 〜/ front / docker-compose.yml

  • 〜/ api / docker-compose.yml

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

How can I make sure that a container in front can send requests to a container in api?

我知道 - default-gateway 选项可以使用 docker run ,以便可以将特定的IP地址分配给此容器,但使用 docker-compose 。

I know that --default-gateway option can be set using docker run for an individual container, so that a specific IP address can be assigned to this container, but it seems that this option is not available when using docker-compose.

目前,我最终做了一个 docker检查my_api_container_id 并查看输出中的网关。它的工作原理,但问题是这个IP被随机归因,所以我不能依赖它。

Currently I end up doing a docker inspect my_api_container_id and look at the gateway in the output. It works but the problem is that this IP is randomly attributed, so I can't rely on it.

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

Another form of this question might thus be:


  • 我可以使用docker-compose将固定的IP地址归因于特定容器吗?

但是最终我正在寻找的是:

But in the end what I'm looking after is:


  • 两个不同的docker组合项目如何沟通

推荐答案

你只需要确保你想要的容器彼此在同一个网络上。网络是一流的码头构造,不是特定的。

You just need to make sure that the containers you want to talk to each other are on the same network. Networks are a first-class docker construct, and not specific to compose.

# front/docker-compose.yml
version: '2'
services:
  front:
    ...
    networks:
      - some-net
networks:
  some-net:
    driver: bridge

...

# api/docker-compose.yml
version: '2'
services:
  api:
    ...
    networks:
      - front_some-net
networks:
  front_some-net:
    external: true

然后他们可以使用服务名称彼此交谈。从前面可以执行 ping api ,反之亦然。

They can then talk to each other using the service name. From front you can do ping api and vice versa.

这篇关于多个码头工程项目之间的沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆