Skip to content

Mount a Volume

When to Use This Guide

Use this guide to attach a named volume to a container, so the data it writes there survives the container being removed.


Quick Command

docker run -d -v my-data:/var/lib/postgresql/data postgres:16

Step 1: Mount the Volume

Attach a volume with -v NAME:PATH, where PATH is the folder inside the container.

docker run -d --name db -e POSTGRES_PASSWORD=secret -v my-data:/var/lib/postgresql/data postgres:16

Docker creates the volume automatically if it does not exist yet.


Step 2: Prove the Data Survives

Remove the container.

docker rm -f db

Start a new one with the same volume, and the data is still there.

docker run -d --name db -e POSTGRES_PASSWORD=secret -v my-data:/var/lib/postgresql/data postgres:16

Common Issues

Data was not there after recreating

Check you used the exact same volume name both times. A typo creates a new, empty volume.

Wrong path inside the container

The volume must be mounted at the folder where the application actually stores its data. For Postgres that is /var/lib/postgresql/data.