Docker & Containers

Container Networking

Connect containers and expose ports.

30 min read intermediate 3 objectives

Status

Not started

What you will learn

  • Use bridge networks
  • Link containers by name
  • Publish ports

New to this? Start here

The basics, in plain English

Apps are rarely alone; a web app needs to reach its database. Docker networking lets containers find and talk to each other, and lets you expose a container to the outside world through a port.

Network
A private space where containers can reach each other by name.
Publish a port
Opening a container’s port to your computer so you can reach the app from a browser.
Service discovery
Containers finding each other by name instead of by changing IP addresses.
Host
The actual machine running the containers.
Mapping ( 8080:80 )
Connecting a port on your machine (8080) to a port inside the container (80).
01

Networks

Containers on the same user-defined bridge network reach each other by name. -p publishes a container port to the host.

Try it yourself

bash
$docker network create app
Create a private network so containers can talk by name.
$docker run --network app --name db postgres
Start the database, reachable on the network as “db”.
$docker run --network app -p 8080:80 web
Start the web app and expose port 80 as 8080 on your host.
↳ lines explain what each command does — only the commands get copied

Finished this topic?

Mark it done to earn 100 XP and keep your streak alive.