MEHDI.
RETURN_TO_INDEX

Docker Volumes Explained: Why Your Data Disappears

3 min read
#Docker#DevOps#Containers

Introduction

As a developer new to Docker, one of the most common pitfalls is losing data after restarting a container. This issue stems from a lack of understanding 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 avoid this common mistake.

Docker Volumes

Docker volumes are directories that are shared between the host machine and a container. When you create a volume, Docker stores the data in a specific directory on the host machine, which is then mounted to the container. This allows data to persist even after the container is restarted or deleted.

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

docker volume create my-volume

You can then mount this volume to a container using the -v flag:

docker run -v my-volume:/app/data my-image

In this example, the my-volume volume is mounted to the /app/data directory in the container.

Bind Mounts

Bind mounts are similar to volumes, but instead of storing data in a specific directory on the host machine, you can mount a directory from the host machine directly to the container. This allows you to share data between the host machine and the container.

To use a bind mount, you can use the -v flag with the path to the directory on the host machine:

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

For example, if you want to share a directory called data in the current working directory with the container, you can use:

docker run -v $(pwd)/data:/app/data my-image

Tmpfs

Tmpfs is a temporary file system that stores data in memory (RAM) instead of on disk. This makes it very fast, but also means that data is lost when the container is restarted.

To use tmpfs, you can use the --tmpfs flag:

docker run --tmpfs /app/data my-image

Docker-Compose Examples

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

For example, here is a docker-compose.yml file that defines a volume and a bind mount:

version: '3'
services:
  app:
    image: my-image
    volumes:
      - my-volume:/app/data
      - ./data:/app/data/bind-mount
volumes:
  my-volume:

In this example, the my-volume volume is mounted to the /app/data directory in the container, and the ./data directory on the host machine is mounted to the /app/data/bind-mount 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. Here are some best practices to keep in mind:

  • Use Docker volumes for data that needs to be persisted across container restarts.
  • Use bind mounts for data that needs to be shared between the host machine and the container.
  • Avoid using tmpfs for data that needs to be persisted.
  • Use docker-compose to define and run multi-container Docker applications.
  • Always test your Docker application to ensure that data is being persisted correctly.