Skip to content

Storage & Mounts

Containers are disposable. Data isn’t. Everything on this page exists because someone forgot one of those two facts.

Type Lives where Good for
Named volume Managed by Docker, under /var/lib/docker/volumes/ Databases, app state. The default choice.
Bind mount A path on the host you pick Config files, code in dev, data you want to poke at directly
tmpfs RAM Scratch space, secrets that shouldn’t touch disk
services:
db:
image: postgres:17
volumes:
- pgdata:/var/lib/postgresql/data # named volume
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # bind mount, read-only
tmpfs:
- /tmp
volumes:
pgdata:

Rule of thumb: if the container owns the data, use a named volume. If you own the file (you edit it, you version it), use a bind mount.

Data in the container layer is already gone

Section titled “Data in the container layer is already gone”

If you didn’t mount anything at the path where the app writes, the data lives in the container’s writable layer. Next docker compose up after an image update recreates the container, and it’s gone. No warning.

Check what a container has mounted:

Terminal window
docker inspect -f '{{json .Mounts}}' db | jq

And check the image’s documentation for where it keeps its data. Postgres: /var/lib/postgresql/data. MySQL/MariaDB: /var/lib/mysql. Everything else: read the docs, then read them again.

./data is relative to the directory of the compose file, not where you ran the command. With docker run -v, you need an absolute path. $(pwd)/data is the usual workaround.

If the host path doesn’t exist, Docker creates it as root with -v and default Compose bind syntax. Your non-root container then can’t write to it. Create it yourself with the right owner first.

Bind-mount ./nginx.conf before the file exists, and Docker creates a directory called nginx.conf. Then nginx fails because it’s trying to read a directory as a config file. Delete the directory, create the file, try again. Everyone does this once. Some of us do it annually.

Many editors (vim included) save by writing a new file and renaming it over the old one. That’s a new inode, and the container still sees the old file. Mount the directory instead of the single file, or restart the container after edits.

A bind mount hides whatever was at that path in the image. Mount an empty dir over /usr/share/nginx/html and the default page disappears. That’s by design. Empty named volumes are different: Docker copies the image’s content into them on first use. Bind mounts never get that treatment.

Files on a bind mount are owned by numeric UIDs, not names. If the container runs as UID 1000 and the host directory is owned by root, the app can’t write. If the container runs as root, everything it creates on the host is root-owned, and you’ll be sudo-ing to delete your own logs.

Terminal window
docker exec app id # what UID/GID does the container run as?
ls -ln ./data # what numeric owner do the host files have?
sudo chown -R 1000:1000 ./data # make them match

Or run the container as your host user, if the image supports it:

user: "1000:1000"

You can’t back up a running database by copying its files and expect them to be consistent. Use the database’s own dump tool:

Terminal window
docker compose exec -T db pg_dump -U app app | gzip > backup-$(date +%F).sql.gz
docker compose exec -T db mariadb-dump -u root -p"$MYSQL_ROOT_PASSWORD" --all-databases > backup.sql

(-T disables the TTY, so the output isn’t mangled by pipes.)

For a generic volume, stop whatever uses it, then tar it up from a throwaway container:

Terminal window
docker compose stop app
docker run --rm -v myproject_appdata:/data:ro -v "$(pwd)":/backup alpine \
tar czf /backup/appdata-$(date +%F).tar.gz -C /data .
docker compose start app

Restore is the reverse:

Terminal window
docker run --rm -v myproject_appdata:/data -v "$(pwd)":/backup alpine \
sh -c "cd /data && tar xzf /backup/appdata-2026-11-26.tar.gz"

Docker hoards. Images, stopped containers, build cache, dangling volumes. Eventually the disk fills and everything falls over at once.

Terminal window
docker system df # what's using space
docker image prune # dangling images (untagged leftovers)
docker image prune -a # ALL images not used by a container. Next pull will be slow.
docker builder prune # build cache. Often the biggest one on CI hosts.
docker container prune # stopped containers
docker volume prune # unused volumes. Read the next box first.

docker system prune bundles most of the above (not volumes unless you add --volumes). Fine to cron on a build server. Not fine on a production database host.

The default json-file log driver has no size limit. A chatty container will eventually fill the disk all on its own. Set limits globally in /etc/docker/daemon.json:

{
"log-driver": "local",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}

sudo systemctl restart docker. This only applies to newly created containers, so recreate your stacks (docker compose up -d --force-recreate). Or set it per-service in Compose:

logging:
driver: local
options:
max-size: "10m"
max-file: "3"