MEHDI.
RETURN_TO_INDEX

Docker Volumes Explained: Why Your Data Disappears

3 min read
#Docker#DevOps#Containers

Introduction to Docker Volumes

As a developer new to Docker, one of the most frustrating experiences is losing data after restarting a container. This issue often arises from a misunderstanding of how Docker handles data persistence. In this article, I will explain Docker volumes, bind mounts, and tmpfs, and provide examples using docker-compose to help you persist data effectively.

What are Docker Volumes?

Docker volumes are directories that are shared between the host system and a container. They allow data to be persisted even after a container is restarted or deleted. Volumes are stored in a specific directory on the host system, usually /var/lib/docker/volumes/ on Linux.

Creating a Docker Volume

To create a Docker volume, you can use the docker volume create command. For example:

docker volume create my-volume

This command creates a new volume named my-volume.

Bind Mounts

Bind mounts are another way to share directories between the host system and a container. Unlike volumes, bind mounts are stored in a specific location on the host system that you specify. Bind mounts are useful when you want to share a directory that already exists on the host system with a container.

Creating a Bind Mount

To create a bind mount, you can use the -v flag when running a container. For example:

docker run -v /path/to/host/directory:/path/to/container/directory my-image

This command mounts the /path/to/host/directory directory on the host system to the /path/to/container/directory directory in the container.

Tmpfs

Tmpfs is a temporary file system that stores data in memory (RAM) instead of on disk. Tmpfs is useful when you need to share data between containers, but you don't need to persist the data after the containers are restarted.

Creating a Tmpfs Mount

To create a tmpfs mount, you can use the --tmpfs flag when running a container. For example:

docker run --tmpfs /path/to/container/directory my-image

This command mounts a tmpfs file system to the /path/to/container/directory directory in the container.

Using Docker-Compose

Docker-compose is a tool that allows you to define and run multi-container Docker applications. You can use docker-compose to create volumes, bind mounts, and tmpfs mounts.

Example Docker-Compose File

Here is an example docker-compose file that creates a volume and a bind mount:

version: '3'
services:
  web:
    image: my-image
    volumes:
      - my-volume:/path/to/container/directory
      - /path/to/host/directory:/path/to/container/directory
    ports:
      - '80:80'
volumes:
  my-volume:

This file creates a service named web that uses the my-image image. It also creates a volume named my-volume and mounts it to the /path/to/container/directory directory in the container. Additionally, it mounts the /path/to/host/directory directory on the host system to the /path/to/container/directory directory in the container.

Practical Takeaways

To avoid losing data after container restarts, make sure to use Docker volumes or bind mounts to persist data. Use the docker volume create command to create a new volume, and the -v flag to create a bind mount. Use docker-compose to define and run multi-container Docker applications, and to create volumes and bind mounts. Remember to use tmpfs mounts when you need to share data between containers, but you don't need to persist the data after the containers are restarted.