What options do you have to ensure that important data is not lost when containers stop or are removed? We previously discussed Docker volumes here, but in this article we will explore the differences between three common methods of data persistence in Docker: Docker Volumes, Bind Mounts, and tmpfs. Keep reading to discover which option is best for your needs.
Before continuing, here is a list of previous articles where we discussed different Docker components:
Docker Volumes: The Versatile Solution for Data Persistence
Docker Volumes are a powerful feature that Docker provides for managing data persistence efficiently and reliably. These volumes are stored in the host filesystem and are independent of the container lifecycle. To create a Docker volume using the Docker CLI, run:
docker volume create nombre_del_volumen
Or, using Docker Compose, you can define a volume as follows:
volumes:
nombre_del_volumen:
driver: local
Example: Suppose you want to persist the data of a MySQL database in a Docker volume called mysql_data. You can configure it like this:
docker volume create mysql_data
Or in Docker Compose:
volumes:
mysql_data:
driver: local
The key point is that when using a Docker volume, a volume is created from the container’s initial data at /var/lib/docker/volume. It survives container restarts or crashes.
Bind Mounts: Direct Integration with the Host Filesystem
Bind Mounts provide a simple and direct way to mount a specific directory or file from the host filesystem inside a container. This creates close integration between host and container files, which is useful for development and debugging. To create a bind mount, use the -v or --mount option when running a container with the Docker CLI:
docker run -v /ruta/del/host:/ruta/del/contenedor imagen_del_contenedor
Or in Docker Compose:
volumes:
- /ruta/del/host:/ruta/del/contenedor
Example: Suppose you have a website and want to mount your project directory in an Nginx container. You can do this:
docker run -v /ruta/de/tu/proyecto:/usr/share/nginx/html nginx
Or in Docker Compose:
volumes:
- /ruta/de/tu/proyecto:/usr/share/nginx/html
The important point about Bind Mounts is that they mount a host directory inside the container. If the directory is empty while the container has data, that data is overwritten in the container. Bind Mounts are especially useful for applications that start with an empty directory and generate data over time, making those files accessible from the host system.
tmpfs: In-Memory Storage for Temporary Data
tmpfs is a special in-memory filesystem that Docker can use to store temporary data. Unlike volumes and bind mounts, data in tmpfs does not persist after the container stops or is removed. However, it can be useful for applications that need fast access to temporary data and do not require long-term persistence. To mount tmpfs in a Docker container, use the --tmpfs option:
docker run --tmpfs /ruta/del/contenedor imagen_del_contenedor
Example: If you are running a container that needs to store temporary files in memory, you can do so as follows.
Conclusion: Choose the Best Option for Your Needs
In summary, Docker offers several options for data persistence: Docker Volumes, Bind Mounts, and tmpfs. Each has its own advantages and use cases. Docker Volumes are ideal for data that needs long-term persistence and must be independent of the container lifecycle. Bind Mounts are perfect for directly integrating host files into a container. tmpfs is useful for temporary data that needs fast in-memory access. Choose the option that best fits your needs and make the most of Docker for efficient and reliable data management.
We hope you now have a clearer understanding of the differences between Docker Volumes, Bind Mounts, and tmpfs. If you have a question or comment, leave it below.

