Storage & Mounts
Containers are disposable. Data isn’t. Everything on this page exists because someone forgot one of those two facts.
The three kinds of mounts
Section titled “The three kinds of mounts”| 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:
docker inspect -f '{{json .Mounts}}' db | jqAnd 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.
Bind mount gotchas
Section titled “Bind mount gotchas”Relative paths
Section titled “Relative paths”./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.
Missing host directory
Section titled “Missing host directory”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.
Mounting a file that doesn’t exist yet
Section titled “Mounting a file that doesn’t exist yet”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.
Editing a single-file bind mount
Section titled “Editing a single-file bind mount”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.
Mounting over existing content
Section titled “Mounting over existing content”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.
Permissions (the eternal UID problem)
Section titled “Permissions (the eternal UID problem)”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.
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 matchOr run the container as your host user, if the image supports it:
user: "1000:1000"Backups
Section titled “Backups”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:
docker compose exec -T db pg_dump -U app app | gzip > backup-$(date +%F).sql.gzdocker 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:
docker compose stop appdocker run --rm -v myproject_appdata:/data:ro -v "$(pwd)":/backup alpine \ tar czf /backup/appdata-$(date +%F).tar.gz -C /data .docker compose start appRestore is the reverse:
docker run --rm -v myproject_appdata:/data -v "$(pwd)":/backup alpine \ sh -c "cd /data && tar xzf /backup/appdata-2026-11-26.tar.gz"Cleaning up
Section titled “Cleaning up”Docker hoards. Images, stopped containers, build cache, dangling volumes. Eventually the disk fills and everything falls over at once.
docker system df # what's using spacedocker 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 containersdocker 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.
Container logs eating the disk
Section titled “Container logs eating the disk”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"