Networks can be specified in a compose file (v2). By default all the containers are in a shared network.
Start with this file: example/docker-compose.yml
:
version: '2'
services:
keys:
image: consul
command: agent -server -client=0.0.0.0 -bootstrap
test:
image: alpine
tty: true
command: sh
Starting this stack with docker-compose up -d
will create a network named after the parent directory, in this case example_default
. Check with docker network ls
> docker network ls
NETWORK ID NAME DRIVER SCOPE
719eafa8690b example_default bridge local
Connect to the alpine container to verify the containers can resolve and communicate:
> docker exec -ti example_test_1 sh
/ # nslookup keys
...
/ # wget -qO- keys:8500/v1/kv/?recurse
...
A compose file can have a networks:
top level section to specify the network name, driver, and other options from the docker network command.