EasyConcept

What are Docker volumes and why do you need them?

Technology:
Docker
Experience:
Junior,
Mid-level

Quick answer

Volumes are storage managed by Docker that lives outside a container鈥檚 writable layer, so data such as database files survives when the container is removed or replaced.

Detailed explanation

A container鈥檚 writable layer is deleted with the container, and it is slower than native storage. Anything that must persist, such as a database, uploaded files or caches you want to keep, needs to live outside it.

There are three options. Named volumes are created and managed by Docker and are the default choice for persistent data. Bind mounts map a host directory into the container, which is handy in development for live-reloading source code. tmpfs mounts keep data in memory only.

Volumes are also how containers share data, and they can be backed by network storage in production. Back them up like any other data: removing the container does not remove a named volume, but docker volume rm or docker compose down -v does.

Example

bash
docker volume create pgdata
docker run -d --name db \
  -e POSTGRES_PASSWORD=secret \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

# the data survives replacing the container
docker rm -f db
docker run -d --name db -e POSTGRES_PASSWORD=secret \
  -v pgdata:/var/lib/postgresql/data postgres:16

Key points

  • Container writable layers are temporary
  • Named volumes for persistent data, bind mounts for development
  • Volumes outlive containers until explicitly removed

Follow-up questions

  • DockerEasy

    What is the difference between a Docker container and a virtual machine?

    A virtual machine runs a full guest operating system on virtualised hardware, while a container shares the host鈥檚 kernel and isolates only the process, its filesystem and resources, which makes containers much lighter and faster to start.

    Intern 路 Junior 路 DevOps
  • DockerEasy

    What is the difference between a Docker image and a container?

    An image is a read-only template made of layers that contains the application and its dependencies; a container is a running (or stopped) instance of an image with its own writable layer on top.

    Intern 路 Junior 路 DevOps
  • DockerMedium

    How do you write a good Dockerfile? What are multi-stage builds?

    Use a small, pinned base image, order instructions so dependencies are cached before source code, use multi-stage builds to leave build tools out of the final image, run as a non-root user and keep secrets out of the image.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    What is Docker Compose used for?

    Docker Compose defines a multi-container application (services, networks and volumes) in one YAML file so you can start, stop and rebuild the whole stack with a single command, typically for local development and testing.

    Mid-level 路 Senior 路 DevOps
  • Node.jsEasy

    What is the difference between package.json and package-lock.json?

    package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.

    Intern 路 Junior 路 Programming Fundamentals