Ports
A container's ports are private until you publish them
-p 8080:80 maps a host port to a container port.
A web server in a container listens on port eighty. But that is port eighty inside its own network namespace, not on the machine. So by default, only the host itself can reach it. To open it up, you publish a port. This flag says: take port eighty eighty on the host, and connect it to port eighty in the container. Now a browser anywhere calls the host on eighty eighty, and the request is passed inside.
Ports · Going deeper · 1 of 2
Lab: run a server and reach it
Run it, call it, read its logs, step inside, remove it.
Publishing ports · Going deeper · 2 of 2
Six ways to write a published port, and who can reach each
-p 8080:80 opens every interface. 127.0.0.1:8080:80 opens this machine only.
Volumes
What a container writes dies with it, unless it is in a volume
The writable layer is temporary. A volume outlives the container.
A container can write files, but where do they go? Without any setup, into the thin writable layer on top of the image. Remove the container, and that layer is deleted. For a database, that is everything. So you mount a volume: a directory kept by the host, attached at the path where the database stores its files. Now the container can be thrown away and replaced by a newer version. The new one mounts the same volume and finds the data.
Kinds of mount · Going deeper
Named volume, bind mount, tmpfs: what each keeps and hides
A named volume is filled from the image. A bind mount hides it.
Environment
One image runs everywhere; the settings come from outside
Build once. Configure each container with environment variables.
An image should not know where it is running. Take one image, built once. On a laptop it starts with settings for development: a local database and noisy logs. In staging, the same image gets the staging database. And in production, the production one. Nothing was rebuilt in between, so what you tested is exactly what you ship.
Networks
On a shared network, containers find each other by name
web calls db by name. Only web is published.
Real systems are several containers working together. Here are two on one host, joined to a network that you created. On that network, each container can look up the others by name. The web server simply connects to d b, port five four three two. The web server is published on host port eighty eighty, so browsers can reach it. The database keeps its files in a volume. And notice what is missing: the database publishes no port, so nothing outside the host can touch it.
Compose
Compose writes the whole picture down in one file
One file, one command: docker compose up.
Starting that system by hand takes four long commands in the right order. Compose turns it into a file. Read it against the picture: two services, web and d b. Web publishes its port and is told the database's name. The database mounts a named volume. One command, docker compose up, creates all of it, and docker compose down removes it.
Compose · Going deeper · 1 of 2
Lab: two containers from one file
Up, look, resolve db by name, down. The volume stays.
Start order · Going deeper · 2 of 2
depends_on waits for started, not for ready, unless a health check says so
healthcheck on db, and condition: service_healthy on web.
Lesson complete
You finished “Running containers”
Nicely done.