
Docker Compose beyond up: networks, volumes and where your data lives
DevOpsHomeLabDockerDocker ComposeVolumesNetworksLinux
How to organise networks and volumes in Docker Compose so you always know where your data is, and the commands that can delete it without warning: down -v and volume prune -a.
Contents
Starting a container with docker compose up -d is easy. The hard part comes months later, when you have twenty containers and someone asks where each one keeps its data, which container talks to which, and what happens if you delete something. This guide explains how I organise it in my homelab and the commands I have learned not to run lightly.
Diagram generated with Claude.
One folder per stack
Every application lives in its own folder, with its compose.yaml, its .env and, right next to them, the data:
docker-compose-files/
├── immich/
│ ├── compose.yaml
│ ├── .env
│ ├── photos/
│ └── postgres/
└── authentik/
├── compose.yaml
├── .env
└── media/
With this layout, backing up a service means copying a folder, and knowing where the important stuff is does not require opening the compose file. Compose uses the folder name as the project name, and that decides the names of the networks and volumes it creates.
One network per stack
If you say nothing, Compose creates a bridge network per project, called <project>_default. Inside it, containers find each other by service name:
services:
app:
image: example/app:1.4
environment:
DATABASE_URL: postgres://user:password@db:5432/app
db:
image: postgres:16-alpine
volumes:
- ./postgres:/var/lib/postgresql/data
The application reaches the database with the name db. From outside the stack nobody else can see it, which is exactly what you want.
When one service has to talk to several stacks
A proxy that forwards traffic to several applications must be on all of their networks. Each network is declared as external, meaning it was created by another stack:
services:
proxy:
image: nginx:stable
networks:
- default
- immich_default
- panel-network
networks:
immich_default:
external: true
panel-network:
external: true
This has a practical consequence. On my server the proxy is on three networks at once. If I recreate those networks and lose the attachments, one of the services stops being served through the proxy. It is worth writing down which containers share which networks, and I do it in the stack’s own documentation.
To see what is on each network:
docker network ls
docker network inspect immich_default --format '{{range .Containers}}{{.Name}} {{end}}'
Empty networks left over from stacks that no longer exist can be removed with docker network prune, which only deletes the ones no container is using.
Named volumes or bind mounts
There are two ways to keep data outside a container, and choosing one or the other changes where it lives:
| Bind mount | Named volume | |
|---|---|---|
| Syntax | ./data:/path | data:/path |
| Where the data is | In the folder you point to | In Docker’s own area |
| Viewing and copying it | Directly, like any folder | With docker volume inspect and docker cp |
| Risk when cleaning up | Only if you delete the folder | volume prune -a and down -v delete it |
On my server I have both. The photos and the database of Immich, or MySQL, are in bind mounts; the Authentik database, Grafana or Immich’s model cache are in named volumes. For data you cannot lose, bind mounts fit the “one folder per stack” idea better: the data is in plain sight and ends up in any copy you make of that folder. I would keep named volumes for data that can be regenerated.
One thing to watch with bind mounts: the folder must exist and have the right owner before you start the container. Otherwise Docker creates it as root and the service may not be able to write to it.
Finding out where your data really is
If you inherit a server, or time has passed, this command lists the source of every mount of every running container:
docker ps -q | xargs docker inspect \
--format '{{.Name}}{{range .Mounts}}{{"\n "}}{{.Type}}: {{.Source}} -> {{.Destination}}{{end}}'
And to see how much everything takes and how much Docker considers “reclaimable”:
docker system df
docker volume ls
The commands that delete data
docker compose down -v
The -v option removes the project’s volumes. It is not an exotic option: it shows up in many tutorials as a way to “start from scratch”. One of my servers had a docker compose down -v in a backup script. It never did any harm because the script’s path was wrong; if someone had fixed the path without reading that line, the first backup would have destroyed the server’s data.
To stop and start safely:
docker compose stop
docker compose start
And if you need to recreate a container after changing the compose file:
docker compose up -d --force-recreate
docker volume prune
Since Docker 23, on its own it only removes anonymous volumes that no container is using. With -a (--all) it also removes named volumes with no container, and there “no container” does not mean “no data”. On my server there were two volumes holding data whose containers did not exist at that moment, and a volume prune -a would have deleted them. docker system df listed them as 58 % of the volumes being “reclaimable” when in fact they were data. On versions older than 23, the command without -a already behaves that way.
The other prune commands are safe for everyday use:
docker image prune -a # unused images
docker builder prune # build cache
My rule is simple: I never run volume prune -a. If I want to delete a volume I remove it by name with docker volume rm, after checking what it is with docker volume inspect.
What you install inside a container gets lost
A container is an image plus a writable layer. If you get in with docker exec and run apt install curl, the program stays in that layer. One of my containers ran for two weeks with a hand-installed curl, and it vanished when I recreated it, because it was not in the image.
If a container needs something, it goes in the Dockerfile or the compose file. And remember that recreating a container discards everything that had drifted from its image: good news for cleanliness and bad news for any manual fix.
Publish only what you need
A ports: "8080:80" publishes the port on every interface of the machine. If the service is only used by a local proxy, publish it on the loopback address only:
ports:
- "127.0.0.1:8080:80"
The reason is that Docker bypasses the ufw firewall for published ports. I explain it in detail in the next guide of the series.
Summary
- One folder per stack, with the compose file, the
.envand the data next to them. - One network per stack; share it with a proxy only when needed, and write it down.
- Bind mounts for what matters, named volumes for what can be regenerated.
docker compose stopandstartfor day-to-day work;down -vandvolume prune -aonly with great care.- Never install programs with
docker exec: put them in the image or the compose file.
Next steps
The logical step after organising your containers is protecting them. In A firewall that really filters with Docker I explain why ufw cannot see Docker’s ports and how to filter them with DOCKER-USER.
Copyright: CC BY-NC-ND 4.0 · José Ángel Alejo Sillero