CodeBlanks Logo Icon CodeBlanks

Summary - Network

Combine create, connect and publish to set up multi-container.

Summary: networking

In this group:

  • Drivers: bridge (default), host, none, overlay.
  • docker network ls/inspect/create/rm/prune manage networks.
  • On user-defined bridge networks the container name is the DNS name; on the default bridge it isn't.
  • network connect/disconnect attach a running container to networks; --network-alias gives an extra name.
  • In-network traffic uses the container port; only entry points like a reverse proxy publish ports.
docker network create arka
docker run -d --name db --network arka postgres:16
docker run -d --name api --network arka -p 8000:8000 api:1.0

Practice A two-tier network

Write three commands: create the arka network; start a container named db from postgres:16 in the background on arka with POSTGRES_PASSWORD=gizli; start a container named api from api:1.0 in the background on arka, mapping 8000 to 8000 and with DATABASE_URL=postgresql://postgres:gizli@db:5432/postgres.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker network create arka
docker run -d --name db --network arka -e POSTGRES_PASSWORD=gizli postgres:16
docker run -d --name api --network arka -p 8000:8000 -e DATABASE_URL=postgresql://postgres:gizli@db:5432/postgres api:1.0

Expected output

6f708192a3b4…
2b3c4d5e6f70
3c4d5e6f7081

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the simplest way for two containers to find each other by name?

  • Publishing both their ports
  • Using --network host
  • Writing their IPs into /etc/hosts by hand
  • Putting both on the same user-defined network Correct answer

Explanation: Docker's built-in DNS works on user-defined networks.

2 Start the container on a specific network.

Question
docker run -d --name db ____ arka postgres:16
Answer
docker run -d --name db --network arka postgres:16

Explanation: --network takes the network name; --link is deprecated.

3 Match.

Term Meaning
bridge Default
DNS On user-defined networks
connect Attach later
-p Open to the outside

All lessons in Beginner