我必须使用docker-compose设置一个mongo副本集。对于副本集,容器必须彼此了解。
我试过了 docker-compose.yml
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。解决办法是什么?
为Docker 1.10更新
Docker 1.10允许在compose文件中定义网络。这是更新的代码
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命令。
docker-compose up
创建一个网络 docker network create --driver bridge my-net
docker network create --driver bridge my-net
${NETWORK}在docker-compose.yml文件中将该网络作为环境变量()引用。例如:
${NETWORK}
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"
请注意,replica1in http://replica1:27018将解析为copy11服务(容器)的IP地址。无需对IP地址进行硬编码;复制品1的条目会自动添加到复制品2容器的/ etc / host中。对于replica1容器也是如此。Docker将在其/ etc / host文件中为copy2添加一个条目。
replica1
http://replica1:27018
NETWORK=my-net docker-compose up -d -f docker-compose.yml
我创建了一个 桥接网络 ,该 网络 仅在一个节点(主机)内工作。对开发人员有好处。如果需要让两个节点互相通信,则需要创建一个 覆盖网络 。虽然相同的原则。您将网络名称传递给docker-compose up命令。