Docker Volumes & Data — DevOps

Files the app writes inside a container are lost once the container is deleted (down, docker rm), but stay if the container is only stopped (stop) or…

The notes-api database ran fine for three weeks. Then you ran docker compose down to switch versions, ran docker compose up -d again, and every user's notes were gone. No error, no warning. The database came up empty, like new.

What was missing is a volume: a place to keep data outside a container, so the data stays even when the container is deleted and made again. Picture working at a coworking space (a shared office) with free desks that anyone can use. Things on your desk are safe while the desk is still yours, including when you go out for lunch. Once you check out, the desk is cleared for the next person. In Docker, the desk is the container, and the things on it are the files the app writes. There are two ways to make your things last. You can put them in a named locker the manager rents out (a named volume, a volume with a name that Docker manages). Or you can bring your own flash drive and keep it with you (a bind mount, a folder on your own computer).

Data inside a container is deleted along with the container

Every container has its own writable layer on top of the image. Any file the app writes inside the container goes into that layer. The writable layer exists as long as its container exists:

Take the attachments (files that people upload) in notes-api. The app stores them in storage/attachments inside the container, with no volume. docker compose exec app ls ... runs ls inside the app container:

$ docker compose exec app ls storage/attachments
7f3a9c1e.jpg
$ docker compose restart app
$ docker compose exec app ls storage/attachments
7f3a9c1e.jpg
$ docker compose down
$ docker compose up -d
$ docker compose exec app ls storage/attachments/7f3a9c1e.jpg
ls: storage/attachments/7f3a9c1e.jpg: No such file or directory

After restart, the file is still there. After down then up -d, the file is gone. That error message uses the format of Alpine Linux's ls (Alpine is a small Linux), because our app image is built from node:24-alpine. Temporary files may vanish like this. Users' attachments and the database certainly may not.

Anonymous volume: a volume with no name

Postgres keeps its data in the folder /var/lib/postgresql/data inside the container. The Dockerfile of the postgres:16 image marks that folder as a VOLUME. So if you write no volumes: at all, Docker quietly makes an anonymous volume at that folder: a volume with no name, just a random 64-character ID, for that one container.

The result is much like a file in the writable layer. After down then up, the new container gets a new, empty anonymous volume, so the database starts from zero. The old volume isn't deleted. It's left behind with no container using it (dangling), and it keeps taking up disk.

Named volume: a locker Docker manages

This is notes-api's docker-compose.yml with a named volume, the same one the Lab uses:

services:
  app:
    build: .
    env_file: .env
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: notes
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

docker volume ls lists the volumes:

$ docker volume ls
DRIVER    VOLUME NAME
local     notes-api_db-data

Without Compose, create the volume with docker volume create db-data, then run docker run -d -e POSTGRES_PASSWORD=$DB_PASSWORD -v db-data:/var/lib/postgresql/data postgres:16. docker volume inspect db-data shows the volume's details, such as where it lives on the host.

Bind mount: a host folder mounted into the container

A bind mount mounts a folder that already exists on your computer (the host) straight into the container. The difference from a named volume: a named volume is managed by Docker in its own location, while a bind mount points at a host path you choose. This command mounts the folder you're in at /app in the container:

docker run -d -p 3000:3000 -v $(pwd):/app notes-api

A bind mount .:/app covers everything in /app from the image, including the node_modules installed at build time. The usual trick: add the line - /app/node_modules. That line with no colon is an anonymous volume, and this volume keeps the image's node_modules visible.

Reading a mount line

One line under volumes: (or after -v) tells you right away which kind of mount it is (how the data is attached to the container). Check for the colon first, then the source (the part left of the colon):

Line Kind How you can tell
./src:/app bind has a : and the source is a path, starting with / or ./
db-data:/var/lib/data named has a : and the source is a plain name
/app/node_modules anonymous no :, just a path inside the container

In the exercise, the rules in this table are enough. In real Docker, a source starting with ../ or ~ is a bind mount too.

down and down -v

In the Lab, you can save some notes, then run Compose commands for the three places Postgres's data can live: an anonymous volume, a named volume and a bind mount. The table below starts from 3 notes. Before you look at it, guess: with the named volume db-data, how many notes are left after down then up -d? And after down -v?

Postgres data in restart down then up -d down -v then up -d
no volumes: (anonymous volume) 3 0, the old volume is left behind 0
named volume db-data 3 3 0
bind mount ./pgdata 3 3 3

The output of down -v names the volume it removed, notes-api_db-data:

$ docker compose down -v
[+] Running 4/4
 ✔ Container notes-api-app-1  Removed    0.4s
 ✔ Container notes-api-db-1   Removed    0.3s
 ✔ Volume notes-api_db-data   Removed    0.1s
 ✔ Network notes-api_default  Removed    0.2s

Postgres's log also tells you whether the data is still there. When its data folder is empty, Postgres sets up a new database and prints PostgreSQL init process complete; ready for start up. When the data is still there, Postgres prints PostgreSQL Database directory appears to contain a database; Skipping initialization.

⚠️ Volumes forgotten, overdone, or mistyped

TL;DR: Files the app writes inside a container are lost once the container is deleted (down, docker rm), but stay if the container is only stopped (stop) or restarted (restart). Data that must last goes in a volume (a place for data outside the container). A named volume (db-data:/var/lib/postgresql/data, declared under the volumes: at the bottom of the Compose file) is managed by Docker and survives down, but is deleted by down -v. A bind mount (./src:/app or -v $(pwd):/app) mounts a folder on the host (the computer that runs Docker), is good for development, and is never deleted by Compose. A line with no colon (/app/node_modules) is an anonymous volume: a nameless volume owned by one container. How to read one mount line: no : means anonymous, a source (the part left of :) starting with / or ./ means bind, anything else means named.

⚡ Data inside a container is deleted with it

Write:

$ docker compose down
$ docker compose up -d
$ docker compose exec app ls storage/attachments/7f3a9c1e.jpg
ls: storage/attachments/7f3a9c1e.jpg: No such file or directory

Meaning: The notes-api app stores users' attachments in storage/attachments inside the container, with no volume (a place for data outside the container). That file goes into the writable layer (where the container writes its files). restart keeps the same container, so the file stays. down deletes the container together with its writable layer. So after up -d, ls run inside the app container (exec app) can't find the file anymore.

⚡ Named volume: a locker Docker manages

Write:

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

volumes:
  db-data:

Meaning: A named volume is a volume that has a name and is managed by Docker, like a named locker. Here the volume db-data is mounted at Postgres's data folder: left of the colon is the source, right of it the path inside the container. The volumes: at the bottom declares (registers) the volume. Without that declaration, Compose refuses to run.

⚡ Bind mount: a host folder mounted into the container

Write:

docker run -d -p 3000:3000 -v $(pwd):/app notes-api

Meaning: A bind mount mounts a folder on the host (the computer that runs Docker) into the container. Here -v $(pwd):/app mounts $(pwd), the folder you're in, at /app. Files you edit on your laptop appear in /app right away, so it's good for development. How to tell the kinds of mount apart from one line: no : means an anonymous volume (a volume with no name). A source (left of :) starting with / or ./ means a bind mount. Anything else means a named volume.

⚡ down and down -v

Write:

docker compose down -v
Guess first: there are 3 notes in the named volume db-data. The flag -v makes down delete volumes too. How many are left after down then up -d? After down -v?

After down, still 3, because named volumes aren't touched. After down -v, 0, because the volume is deleted too.

Meaning: On your laptop, down -v is handy for starting from zero. On a server, one -v deletes the production database.

What you will learn