Show contents
Chapter 4 of 7 · 20 min
Volumes and networks
By the end of this chapter, you'll be able to
- Explain why a container's data disappears with it
- Keep data with a named volume or a folder mount
- Connect containers through a network and make them communicate by name
- Pass configuration with environment variables
Disposable containers
A container writes into a writable layer placed on top of its image. When it is removed, that layer is removed with it. This is intended: you must be able to destroy and recreate a container at any time (for an update, after a failure) without losing anything important. So data must be stored elsewhere.
Volumes
A volume is a storage space managed by Docker, which lives outside containers. It is mounted into a folder of the container: everything the container writes in that folder actually goes into the volume.
docker volume create club-data
docker run -d --name db \
-e POSTGRES_PASSWORD=change-me \
-v club-data:/var/lib/postgresql/data \
postgres:17
-v club-data:/var/lib/postgresql/data mounts the club-data volume into the folder where PostgreSQL stores its files. You can now remove the db container and create another one with the same volume: the data is still there.
There are two kinds of mounts:
-v club-data:/var/lib/postgresql/data named volume: managed by Docker,
for a database's data
-v "$PWD/site":/usr/share/nginx/html mount of a folder of your machine:
handy during development
With a folder mount, a change to a file on your machine is immediately visible in the container: ideal for developing without rebuilding the image.
A new container mounts the same volume and finds the 42 registrations.
Text description of the animation
- The database saves 42 registrations in /var/lib/postgresql/data.
- This folder is mounted on the volume: the files actually live in the volume.
- We remove the container to switch to a fixed version of the image. The volume is untouched.
- A new container mounts the same volume and finds the 42 registrations.
Environment variables
The -e NAME=value option passes an environment variable to the container. It is the standard way to configure an image without rebuilding it: database password, address of a service, production or development mode. The postgres image reads for example POSTGRES_PASSWORD, POSTGRES_USER and POSTGRES_DB.
Networks
An application often needs several containers: the web application and its database. For them to talk, they are placed on the same Docker network. On a user-created network, Docker provides an internal DNS: each container is reachable by its name.
docker network create club
docker run -d --name db --network club -e POSTGRES_PASSWORD=change-me \
-v club-data:/var/lib/postgresql/data postgres:17
docker run -d --name web --network club -p 5000:5000 \
-e DATABASE_URL=postgresql://postgres:change-me@db:5432/postgres \
library:1.0
In DATABASE_URL, the host is simply db, the name of the database container. Note that the database publishes no port (-p): it is only reachable from the club network, not from the Internet. Only the web container publishes its port.
Your turn
Worked exercise
Worked exercise: a site and its files
You are developing a static site in your project's site/ folder. Write the commands to:
- serve this folder with
nginx:1.29on port 8080, so that a change to a file is immediately visible; - run a
postgres:17database whose data survives the container's removal; - let a future
apicontainer reach this database by name, without exposing it to the outside.
Hint 1
/usr/share/nginx/html folder.Hint 2
docker network create provides name resolution.Show the solution and common mistakes
# 1. Mount of the local folder, read-only (:ro)
docker run -d --name showcase -p 8080:80 \
-v "$PWD/site":/usr/share/nginx/html:ro nginx:1.29
# 2 and 3. A network, a volume, a database with no published port
docker network create app
docker volume create data
docker run -d --name db --network app \
-e POSTGRES_PASSWORD=change-me \
-v data:/var/lib/postgresql/data postgres:17
The future api container will be run with --network app and will use the host db. The :ro suffix mounts the site read-only: nginx can read the files but not modify them.
Common mistakes:
- Publishing the database's port 5432 with
-p 5432:5432"so the API can find it": it is useless on a Docker network, and dangerous on a server, because the database becomes reachable from the Internet. - Writing
-v site:/usr/share/nginx/htmlwithout a full path: Docker then creates an empty volume namedsite, instead of mounting your folder. - Leaving the password
change-mein production: on a real server, it comes from a.envfile or a secrets manager, never from the code.
Summary
- 1What a container writes disappears with it: important data goes into a volume.
- 2A named volume suits databases; a folder mount suits development.
- 3Environment variables (-e) configure a container without rebuilding the image.
- 4On a user-created Docker network, containers reach each other by name.
- 5Inside a container, localhost means the container itself.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.