Skip to content

Instantly share code, notes, and snippets.

@jonaseberle
Last active November 24, 2021 15:59
Show Gist options
  • Save jonaseberle/4ba7b3166cd54f6c6c51a8981fd5a461 to your computer and use it in GitHub Desktop.
Save jonaseberle/4ba7b3166cd54f6c6c51a8981fd5a461 to your computer and use it in GitHub Desktop.
containers in external network
# add as a/docker-compose.yaml and b/docker-compose.yaml
#
# $ docker network create ext-net
# run from 2 terminals (in directory `a`and `b`):
# $ docker-compose up
#
# Should output the same container ID/pseudo-hostname continuously
#
# tested with docker-compose 1.29.2, 2.0.1, 2.1.0, 2.1.1, 2.2.0
# switching the networks order to [ext-net, my-net] prioritizes name resolution on the common network and makes it fail
services:
web:
image: busybox
command: sh -c 'while true; do wget -qO - http://db:8000/etc/hostname; sleep 1; done'
networks: [my-net, ext-net]
db:
image: busybox
command: httpd -f -p 8000 -h /
ports: [8000]
networks: [my-net, ext-net]
networks:
my-net:
ext-net:
external: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment