Bitpath

Lesson 2 · Unit 03

Container networking

Published ports, what localhost means inside, and names on a network

13 exercisesabout 3 min

What you practise

  • Publishing a port, localhost inside a container, user-defined networks and reaching a container by name

Try a question from this lesson

Pick one

An API runs in one container and its database in another, each with its own network namespace. The API is told to connect to localhost:5432. Where does that go?

Hint

localhost means "this machine". From inside a container, what counts as this machine?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

localhost is per container

Every container has its own localhost. So does the host.

A program that listens on a port in one container cannot be reached through localhost from another container, or from the host, unless something connects the two.

in api:    localhost = api
in db:     localhost = db
on host:   localhost = the host

Concept

A network of your own

docker network create makes a user-defined network. Containers started with --network join it.

On such a network Docker runs a small DNS server: a container's name resolves to its current IP. Other containers connect to cache:6379 and never need to know an address.

shell
docker network create shop
docker run -d --name cache \
  --network shop redis:7
docker run -d --name api \
  --network shop shop/api

Concept

Two directions

From outside in: a client connects to a port on the host, and -p forwards it to a port in one container.

Between containers: both are on the same network, and one connects to the other's name and container port. The host port plays no part.

browser -> host:8080 -> web:80    (-p)
web     -> cache:6379    (same network)

9 more exercises in the app

  • 4× multiple choice
  • 1× true or false
  • 1× fill the blanks
  • 1× type the answer
  • 1× match the pairs
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy