Understanding Docker Volumes

kartikkhk
2 min readMar 21, 2021

Volumes are folders on the hard drive of your host machine which are mounted (“made available”, mapped) into containers.

Volumes persist even if a container shuts down. If a container (re -) starts and mounts a volume. Any data inside of that volume is available inside of that container.

A container can both read and write data on a volume.

Two types of External Data Storages in Docker

  • Volumes (Managed by docker)
  • Bind Mounts (Managed by user)
  • Named Volumes are great for data which should be persistent but which you don’t need to edit directly.
  • Sample named volume: docker run -p 8000:80 -d — rm -v feedbacklist:/app/feedback feedback:latest
  • docker run <options> <volume-name>:<path of file to persist> <container-id>
  • Some useful commands:
docker volume list
docker volume prune
  • If you don’t specify a volume in your docker run command then an anonymous volume will be created. Data will be persisted to the volume but if you stop the container and run the image again another volume with another random name will be created hence you won’t be able to use the persisted data.

Bind Mounts

Bind mounts are great for persistent, editable data.

Sample command:

docker run -p 8000:80 -d -v list:/app/feedback -v "/path/to/code/folder:/app" feedback:latest

Bind Mounts — Shortcuts

Just a quick note: If you don’t always want to copy and use the full path, you can use these shortcuts:

macOS / Linux: -v $(pwd):/app

Windows: -v "%cd%":/app

--

--