How to link multiple docker-compose services via network code example

Example: How to link multiple docker-compose services via network

How to link multiple docker-compose services via network

$ docker network create external-example
2af4d92c2054e9deb86edaea8bb55ecb74f84a62aec7614c9f09fee386f248a6


Modified first docker-compose file with network configured
version: '3' 
services: 
  service1: 
    image: busybox 
    command: sleep infinity 

networks: 
  default: 
    external: 
      name: external-example 
      
Modified second docker-compose file with network configured
version: '3' 
services: 
  service2: 
    image: busybox 
    command: sleep infinity 

networks: 
  default: 
    external: 
      name: external-example
      
With the containers using the external-example network, they are able to ping one another.


# By service name
$ docker exec -it compose1_service1_1 ping service2
PING service2 (172.24.0.3): 56 data bytes
64 bytes from 172.24.0.3: seq=0 ttl=64 time=0.054 ms
^C

Tags:

Misc Example