如何使用 docker-compose 将 docker 容器相互链接 [英] how to link docker container to each other with docker-compose

查看:23
本文介绍了如何使用 docker-compose 将 docker 容器相互链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 docker-compose 设置一个 mongo 副本集.对于副本集,容器必须相互了解.

I have to setup a mongo replica set with docker-compose. For the replica set the containers have to know each other.

我在 docker-compose.yml

    dbreplicasetpart1:
      image: mongo:2.6.8
      expose:
        - '27018'
      links:
        - replicasetpart2
        - replicasetpart3
      cap_add:
        - NET_ADMIN

    dbreplicasetpart2:
      image: mongo:2.6.8
      links:
        - replicasetpart1
        - replicasetpart3
      expose:
        - '27019'
      cap_add:
        - NET_ADMIN
...

我收到一条循环导入消息.但是,如果我删除到 dbreplicasetpart1 的反向链接,我将无法从 dbreplicasetpart2 ping 到 dbreplicasetpart1.有什么解决办法?

I get an circular import message. But if I remove the back-link to dbreplicasetpart1 I can't ping from dbreplicasetpart2 to dbreplicasetpart1. What is the solution?

推荐答案

已针对 Docker 1.10 更新

Docker 1.10 允许在撰写文件中定义网络.这是更新后的代码

Docker 1.10 allows the definition of networks within the compose file. Here's the updated code

version: "2"

services:
  replica1:
    image: mongo:2.6.8
    container_name: replica1
    networks:
      - my-net
    ports:
      - "27018"
    environment:
      REPLICA2_URL: "http://replica2:27019"
  replica2:
    image: mongo:2.6.8
    container_name: replica2
    networks:
      - my-net
    ports:
      - "27019"
    environment:
      REPLICA1_URL: "http://replica1:27018"

networks:
  my-net:
    driver: bridge

Docker 1.9 的上一个答案

从 Docker 1.9 开始,解决方案是创建一个自定义网络并将其传递给 docker-compose up 命令.

As of Docker 1.9, the solution to this is to create a custom network and pass it to the docker-compose up command.

  1. 创建网络<代码>docker network create --driver bridge my-net

在 docker-compose.yml 文件中将该网络作为环境变量 (${NETWORK}) 引用.例如:

Reference that network as an environment variable (${NETWORK})in the docker-compose.yml files. Eg:

```

replica1:
  image: mongo:2.6.8
  container_name: replica1
  net: ${NETWORK}
  ports:
    - "27018"
  environment:
    REPLICA2_URL: "http://replica2:27019"

replica2:
  image: mongo:2.6.8
  container_name: replica2
  net: ${NETWORK}
  ports:
    - "27019"
  environment:
    REPLICA1_URL: "http://replica1:27018"

```

注意http://replica1:27018中的replica1会解析为replica1服务(容器)的ip地址.无需硬编码 IP 地址;副本 1 的条目会自动添加到副本 2 容器的/etc/host 中.replica1 容器也是如此.Docker 将在其/etc/host 文件中为 replica2 添加一个条目.

Note that replica1 in http://replica1:27018 will resolve to the ip address of the replica1 service (container). No need to hardcode ip addresses; An entry for replica1 is automatically added to the /etc/host of the replica2 container. Same goes for the replica1 container. Docker will add an entry for replica2 in its /etc/host file.

  1. 调用 docker-compose,将您创建的网络传递给它<代码>NETWORK=my-net docker-compose up -d -f docker-compose.yml

我创建了一个桥接网络 以上仅在一个节点(主机)内有效.对开发有好处.如果需要让两个节点相互通信,则需要创建一个 覆盖网络.不过原理一样.您将网络名称传递给 docker-compose up 命令.

I've created a bridge network above which only works within one node (host). Good for dev. If you need to get two nodes to talk to each other, you need to create an overlay network. Same principle though. You pass the network name to the docker-compose up command.

这篇关于如何使用 docker-compose 将 docker 容器相互链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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