如何使用docker-compose将docker容器彼此连接起来 [英] how to link docker container to each other with docker-compose

查看:1011
本文介绍了如何使用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-compost 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"

```

请注意, replica1 http:// replica1:27018 将解析为replica1服务(容器)的ip地址。不需要硬编码ip地址; replica1的条目将自动添加到replica2容器的/ 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

  1. Call docker-compose, passing it the network you created NETWORK=my-net docker-compose up -d -f docker-compose.yml

我创建了一个 bridge network 以上,仅在一个节点(主机)内工作。适合开发者如果需要让两个节点相互通信,则需要创建一个 覆盖网络 。相同的原则虽然您将网络名称传递给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天全站免登陆