docker compose网络设置

(系统:Centos 7.4 ,docker 版本:18.03.1-ce, docker-compose version 1.18.0)

 

cat docker-compose.yml
version: '3'
services:
test1:
image: busybox:latest # 镜像为 busybox
entrypoint: # 容器启动后执行 top 命令,使容器没法立即退出
- top
networks:
backend: # 使用指定的网络 backend, 并且设置网络别名为 test1,
aliases: # 设置网络别名后,可以在其他容器中 ping test1 访问到该容器
- test1

test2:
image: busybox:latest
entrypoint:
- top
networks:
backend:
aliases:
- test2

networks:
backend:

启动

docker-compose up -d

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d05ceb2088d busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test2_1
f4ccafa24664 busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test1_1

docker exec -it 4d05ceb2088d /bin/sh
/ # ping test1
PING test1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.061 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.062 ms

ping ibaboss_test1_1
PING ibaboss_test1_1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.045 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.056 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.061 ms

# 在网络中可以通过 容器名字或者网络的别名 进行通信

 

posted @ 2018-08-28 09:27  哥的头像很别致~  阅读(3302)  评论(0编辑  收藏  举报