Skip to content

Instantly share code, notes, and snippets.

@sr105
Created February 5, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sr105/09d0228ebbcd57296042b6d2b6172f36 to your computer and use it in GitHub Desktop.
Save sr105/09d0228ebbcd57296042b6d2b6172f36 to your computer and use it in GitHub Desktop.
Same named network for multiple docker compose files

Same Network for Multiple docker-compose files

The A file creates the network. The B file connects to it.

Run

docker-compose -f docker-compose-A.yaml up -d
docker-compose -f docker-compose-B.yaml up -d

Testing what names Docker puts into DNS:

docker exec -it test_a ash

Then inside the container:

/ # apk add bind-tools
...
/ # echo test_a_service test_a a test_b_service test_b b blah | xargs -n 1 host
test_a_service has address 172.20.0.2
test_a has address 172.20.0.2
a has address 172.20.0.2
test_b_service has address 172.20.0.3
test_b has address 172.20.0.3
b has address 172.20.0.3
Host blah not found: 3(NXDOMAIN)
version: '3.5'
services:
# All 3 names will resolve in DNS:
# - test_a_service
# - test_a
# - a
test_a_service:
container_name: test_a
image: python:3.6-alpine3.7
command: sleep 10000
restart: always
networks:
name_only_for_file:
aliases:
- a
networks:
name_only_for_file:
name: test_net
version: '3.5'
services:
# All 3 names will resolve in DNS:
# - test_b_service
# - test_b
# - b
test_b_service:
container_name: test_b
image: python:3.6-alpine3.7
command: sleep 10000
restart: always
networks:
another_name_only_for_file:
aliases:
- b
networks:
another_name_only_for_file:
external:
name: test_net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment