Linux Labs

Containers: Docker and compose / Stage 7 of 11

Container networking: names and ports

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 6 · on a machine · 6 min

A network of your own: names work

Docker answers with container names in a network you create. Here: how to create one and connect containers to it.

docker network create <name> creates a network of your own. Docker's DNS works in it: the /etc/resolv.conf of each of its containers has nameserver 127.0.0.11, and this server knows the names of every container in the network. A name is visible only inside the network: a container from another network will not find it.

A container joins a network when it is created — with the --network option of docker run — or on the fly, with docker network connect: no restart is needed for that. A container can be in several networks.

bash
sudo docker network create backnet          # a network of your own
sudo docker run -d --name worker --network backnet busybox sleep 600
sudo docker network connect backnet cache   # connect a running one
sudo docker network inspect backnet         # who is in it, addresses
Exercise · do it

Teach app to find the database

Make app connect to db by name: sudo docker exec app nc -w 2 db 5432 must print db: ok. The containers are running; there is no need to re-create them.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Let app connect to db by name not done yet
  • Link app and db with a Docker network not done yet

Step goals

0 of 2
  • Let app connect to db by name — not done yet
  • Link app and db with a Docker network — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Let app connect to db by name — not done yet
  • Link app and db with a Docker network — not done yet
Loading…