Volumes & Persistent Data

Intermediate

Overview

Containers are disposable, so anything written inside one disappears when it is removed. Volumes solve this by storing data outside the container's writable layer.

Named volumes are managed by Docker (docker volume create data). Bind mounts map a host directory into the container, which is great for local development.

Mount with -v: a named volume like -v data:/var/lib/postgresql/data, or a bind mount like -v $(pwd)/src:/app/src.

Cheatsheet

docker volume create dataCreate a named volume
docker volume lsList volumes
-v data:/var/lib/dataMount a named volume
-v $(pwd):/appBind mount the current directory
docker volume rm dataRemove a volume

Try it

A safe, simulated terminal. Run the suggested commands to see typical output.

simulated terminal

Type a command and press Enter, or click a suggestion below to run it.

$

Quick quiz

1. Why do you need a volume for a database container?

2. Which mounts the current host directory into /app?