Docker Networks

Concepts

Commands

# show networks
docker network ls

# inspect network
docker network inspect <networkName>

# create a network
docker network create <newNetworkName>

# attach a network to container
docker network connect <networkName> <containerName>

# detach a network from container
docker network disconnect <networkName> <containerName>

Other command tricks:

# which containers are connected to a specific network:
docker network inspect <networkName> | jq '.[].Containers'

# which networks the container is connected to
docker container inspect <containerName> | jq '.[].NetworkSettings.Network'

DNS

Containers are constantly launching and disappearing, so forget about IPs. Static IPs and using IPs for talking to containers is an anti-pattern. Do your best to avoid it.

Fortunately the Docker daemon has a built-in DNS server that containers use by default. It defaults the hostname to the container's name, but you can also set aliases.

Note: the default bridge network does NOT have the DNS built-in by default. So, it's better to create a new network.

Example:

# creating a new network
docker network create NewNetwork

# creating two containers connected to NewNetwork
docker container run -d --name container1 --network NewNetwork alpine sleep 1000
docker container run -d --name container2 --network NewNetwork alpine sleep 1000

# it's possible to ping one each other using the container name
docker container exec -it container2 ping container1

Assignment: DNS Round Robin Test

Note: due to a bug introduced in alpine 3.11.3, use alpine:3.10.

Assignment solution

# create a network
docker network create elastic

# create two containers, connected to the new
# network and with a network alias.
docker container run \
  --detach \
  --network elastic \
  --network-alias search \
  --name esearch1 \
  elasticsearch:2
docker container run \
  --detach \
  --network elastic \
  --network-alias search \
  --name esearch2 \
  elasticsearch:2

# check the DNS with alpine
docker container run \
  --rm \
  --network elastic \
  alpine:3.10 \
  nslookup search
# it should show the IPs of both containers
# esearch1 and esearch2

# check with curl using centos
docker container run \
  --rm \
  --network elastic \
  centos:7 \
  curl -s search:9200
# try this 👆 command a few times and see
# that it randomly gets response from different
# containers (check the 'name' property).

# to conclude, just remove the containers
docker container rm -f esearch1 esearch2