Stores database data in a managed named volume.

Section: Volumes and Storage

Named volume for persistent data

yaml
yaml
services:
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata: {}
Explanation

Preferred over bind mounts for durable container data.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Volumes and Storage
Remove stack and its volumes
Tears down the project and deletes named volumes created for it.
OpenIn sheetbashsame section
Bind mount local source
Mounts the current directory into the container.
OpenIn sheetyamlsame section
Read-only bind mount
Mounts a host file as read-only in the container.
OpenIn sheetyamlsame section
Use tmpfs mount
Creates an in-memory temporary filesystem for the service.
OpenIn sheetyamlsame section
Publish container port
Maps host port 8080 to container port 80.
OpenIn sheetyaml2 tag match
Mount a secret file
Mounts a file-backed secret into the container.
OpenIn sheetyaml2 tag match