Today, we experimented with the network settings under Docker and recorded the process to avoid forgetting.
(System: Centos 7.4, docker version: 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: - test2networks: backend:
Start
docker-compose up-ddocker ps-acontainer ID IMAGE COMMAND CREATED STATUS PORTS names4d05ceb2088d busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test2_1f4ccafa24664 busybox:latest "top" 5 s Econds ago up 4 seconds ibaboss_test1_1docker exec-it 4d05ceb2088d/bin/sh/# ping Test1 PING test1 (172.19.0.2): Data bytes64 bytes from 172.19.0.2:seq=0 ttl=64 time=0.061 ms64 bytes from 172.19.0.2:seq=1 ttl=64 time=0.062 msping ibaboss_test1_1ping ibaboss_test1_1 (172.19.0.2): Data bytes64 bytes from 172.19.0.2:seq=0 tt l=64 time=0.045 ms64 bytes from 172.19.0.2:seq=1 ttl=64 time=0.056 ms64 bytes from 172.19.0.2:seq=2 ttl=64 time=0.061 ms # communication can be made on the network through the name of the container or the alias of the network
Compose container name format is:< project name >< service name >< serial number >
Although you can customize the project name, the service name, but if you want to have complete control over the naming of the container, you can use this tag to specify:
Container_name:app
cat docker-compose_v1.yml version: ‘3‘services: test1: image: busybox:latest entrypoint: - top container_name: test1 networks: - backend test2: image: busybox:latest entrypoint: - top container_name: test2 networks: - backendnetworks: backend:
Start
docker-compose -f docker-compose_v1.yml up -ddocker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES132859fc77c2 busybox:latest "top" About a minute ago Up About a minute test2cd0a78dc9bd4 busybox:latest "top" About a minute ago Up About a minute test1docker exec -it 132859fc77c2 ping test1PING test1 (172.19.0.2): 56 data bytes64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.070 ms64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.068 ms64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.059 ms
Examples of docker-compose networks