Careeroza
Named Volumes
Creating and Using a Volume
Example
PostgreSQL with its data in a named volume. Remove and recreate the container and the data is still there:
docker volume create pgdata
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
postgres:16-alpine
docker volume lsExample output
pgdata 6b2f4e1a9c... DRIVER VOLUME NAME local pgdata
Managing Volumes
docker volume inspect pgdata # where it lives, when created
docker volume rm pgdata # delete (only if no container uses it)
docker volume prune # delete all unused volumes (careful!)Note: docker rm -v and docker volume prune permanently delete data. Back volumes up before cleaning.
Backing Up a Volume
# archive the volume's contents into ./backup/pgdata.tar.gz using a temporary container
docker run --rm -v pgdata:/data -v "$PWD/backup":/backup alpine \
tar czf /backup/pgdata.tar.gz -C /data .Tip: For databases, prefer the database's own dump tool (pg_dump, mysqldump): a file copy of a running database can be inconsistent.