Docker for Web Development - Volumes

In the previous post, we covered the basic concepts of Docker and how it can enhance your web development workflow. In this post, we will dive deeper into Docker Volumes and how they can help you persist data and streamline your development process.

This is a series of posts about Docker for Web Development:

  1. Basic Concepts
  2. Volumes
  3. Custom Images with Dockerfile
  4. Container Networks
  5. Docker-Compose

The layered filesystem of Docker images

Docker images are built using a layered filesystem. Each layer represents a set of changes to the image, such as adding files, installing packages, or modifying configurations. When you build an image, Docker creates a new layer for each instruction in your Dockerfile.

Images are read-only, and when you run a container from an image, Docker creates a new writable layer on top of the image layers. This allows you to make changes to the container without affecting the underlying image.

layered filesystem

The writable layer is part of the container, not the image. An image can be deployed in multiple containers, and each container will have its own writable layer. This means that when you delete a container, all the changes made in that container are lost, but the underlying image remains intact and can be reused to create new containers.

writable-layer

For instance, if we run a database in that writable layer, all the data will be lost when the container is deleted. To persist data, we can use Docker Volumes, which we will cover in the next section.

Docker Volumes

Docker Volumes are a powerful feature that allows you to persist data outside of the container's writable layer. Volumes are stored on the host machine and can be shared between multiple containers. This means that even if a container is deleted, the data in the volume will remain intact.

Docker Volumes

There are 2 main types of volumes in Docker:

  1. Named Volumes (Docker managed): These are managed by Docker and can be created using the docker volume create command. Named volumes are stored in a specific location on the host machine and can be easily shared between containers.
Copy
docker volume create my_volume

Other than that, you can also create a named volume when running a container:

Copy
docker run -v my_volume:/path/in/container my_image
  1. Bind Mounts (custom managed): These allow you to mount a specific directory from the host machine into a container. Bind mounts are useful for development purposes, as they allow you to make changes to files on the host machine and have those changes reflected in the container in real-time.
Copy
docker run -v /path/on/host:/path/in/container my_image

Everytime you refer to the path in your container, it will point to the path on your volume or bind mount. This allows you to persist data and share it between containers, ensuring that your data is not lost when a container is deleted.

Useful Commands

  1. Create a new named volume.
Copy
docker volume create my_volume
Copy
docker run -v my_volume:/path/in/container my_image
  1. List all volumes on the host machine.
Copy
docker volume ls
  1. Inspect a specific volume to see its details.
Copy
docker volume inspect my_volume
  1. Remove a specific volume.
Copy
docker volume rm my_volume
  1. Remove all unused volumes (volumes not referenced by any containers).
Copy
docker volume prune
Copy
docker system prune --volumes

Published

June 09, 2026

Topics