Skip to content
BENIBIX Learn

Container Volume

Infrastructure📦 ContainersContainer Storage and Networking

Also known as Docker Volume, Volume (Containers)

Storage that outlives the container. Files kept there stay when the box is gone.

Think of it like

It is like a backpack you keep when you change coats. Your things stay safe in it.

Example

Jack runs a database in a container and keeps its data in a volume. He swaps in a newer version. Every record is still there.

Why it matters

Without a volume, the files a container saves vanish with it. That is fine for an app, and a disaster for a database.

Definition

A container volume is storage that lives outside the container. The container sees it as a normal folder. When the container is deleted, the volume and its files stay. A new container can plug in the same volume and carry on. This is how a database in a container keeps its data.

Example code

console
$ docker volume create notes
notes
$ docker run --rm -v notes:/data alpine sh -c 'echo hi > /data/note.txt'
$ docker run --rm -v notes:/data alpine cat /data/note.txt
hi
The first container writes a note and is deleted. The second one still reads the note from the volume.
The old container is gone, but its volume is not. The new container picks up the same files.

Common mistake

Leaving the volume off a database container. The first time the box is replaced, all the data is lost.

Key takeaways

  1. 1
    Lives outside. The data sits apart from the container.
  2. 2
    Outlasts the box. Deleting the container keeps the files.
  3. 3
    Can be shared. Two containers can attach the same volume.

Compare