Understanding Docker Mounts: Volumes, Bind Mounts, and tmpfs
Brief overview to docker mounts and exploring use cases and benefits for each
Nowadays, Dockerš³ has become the de facto standard for containerization and is widely used in various software engineering branches such as Backend, Frontend, DevOps, and more .
If you have used Docker before, which Iām sure you have, you must have realized that data written to the container file system is completely lost when your container has been accidentally or intentionally restartedš. This presents a significant problem since we, as engineers, bear the heavy responsibility of ensuring persistent application data. To address this issue, Docker has provided several ways called Mounts to overcome this challenge and keep data on a more permanent basis.
In this article, we are going to explore each method, when and how to use them, and demonstrate how to utilize them using both Docker CLI and Docker Compose.š
As of today, there are three types of mounts in Docker, each designed for specific reasons and use cases. These types of mounts are:
I have included some commands for managing mounts at the end of the blog.
Are you ready? Letās dive in and discover more!āØ
Bind Mounts:
By default host directories are not available in the container file system , but with bind mounts we can access the host filesystem. bind mount is a way to connect or link a directory or file from your computerās file system to a specific location inside a Docker container.
You can easily update the files on your computer, and the changes will be instantly reflected inside the container without the need to rebuild or modify the container itself.
Bind mounts tightly couple the container to the host machineās filesystem, which means that processes running in a container can modify the host filesystem. This includes creating, modifying, or deleting system files or directories. Therefore, it is crucial to be cautious with permissions and ensure proper access controls to prevent any security risks or conflicts.
ā benefits of Bind mounts:
- Easy file sharing between host and container.
- Real-time updates for shared files.
- Persistent storage for long-term data.
- Can be shared between multiple containers.
- Bind mounts provide access to sensitive files (both beneficial and risky).
ā When to use Bind Mounts:
- Development environments
- Loading config files and live reload if any changes happen
- Host-Specific access and loading resources that may not be accessible from within the container, such as devices or system files
ā How to use Bind Mount:
Docker CLI:
docker run -v /host/path:/path/in/container image:tag
Docker Compose:
services:
myservice:
image: image:tag
volumes: - /host/path:/path/in/container
Volume Mounts:
Volume mounts are like bind mounts but they are fully managed by docker itself . Volumes are stored in the Linux VM rather than the host, which means that the reads and writes have much lower latency and higher throughput.
volume mounts are a way to manage and store data separately from the containers themselves. They provide a means to store and share files and directories that can be persist even if containers are deleted or recreated.
Furthermore, volume mounts enable data sharing among multiple containers. For instance, you can create a volume and attach it to different containers running different services, allowing them to access and manipulate the shared data. This promotes modularity and flexibility in your Docker setup.
Also when working with Docker volumes, it is advisable to follow these best practices:
- Assign custom names to your volumes.
- Document your volume configurations.
- Regularly back up important volumes.
ā Benefits of Volume Mounts:
- Better Performance due to their utilization of optimized drivers and file systems.
- Managed by Docker and can be easily backed up, restored, or migrated between different Docker installations.
- better portability and cross-platform compatibility
ā When to use Volume Mounts:
- Persisting Database Data
- Handling File Uploads and Downloads
- Storing log files
- Changing or Applying New Configuration Files
- Backup and Restore
ā How to use Volume Mounts:
Docker CLI:
docker run -v myvolume:/path/in/container image:tag
Docker Compose:
services:
myservice:
image: image:tag
volumes: - myvolume:/path/in/container
//Creating volume
volumes:
myvolume:
Tmpfs Mounts:
Tmpfs (temporary file system) Mount is a special type of mount that allows you to create a temporary file system in memory. Think of tmpfs as a virtual file system .Docker sets aside a portion of the containerās memory to be used as a temporary storage area.
tmpfs mount is temporary and exists only as long as the container is running. Once the container is stopped or restarted, the data in the tmpfs mount is lost. Therefore, tmpfs mounts are not suitable for storing data that needs to persist across container restarts.
ā Benefits of Tmpfs Mounts:
- very fast and lightweight storage (faster than other mounts because of in-memory )
- eliminating the need for disk I/O operations
ā When to use Tmpfs Mounts:
working with temporary data that doesnāt need to persist beyond the lifespan of the container
As Temporary Scratch Space: If your application requires temporary storage for processing data, such as caching or intermediate calculations
ideal for situations where speed and low latency are crucial, such as in-memory caching or processing large amounts of data in a short period.
working with Sensitive or Transient Data
ā How to use Tmpfs Mount:
Docker CLI:
docker run --tmpfs /path/in/container image:tag
Docker Compose:
services:
myservice:
image: image:tag
tmpfs: - /path/in/container
Managing Mounts and Volumes:
Now letās explore some docker Commands for managing volumes and mounts .
//Inspecting Mounts used in a container
docker inspect [container_id] --format='{{ .Mounts }}'
//Creating volumes
docker volume create [volume-name]
//Inspect info about volume
docker volume inspect [volume-name]
//Remove volume
docker volume rm [volume-name]
Thank you š for taking the time to read my first blog post! Your comments š¬and clapsšwould be greatly appreciated as they would motivate me to continue writing and improve. If you found this helpful, I encourage you to explore my other articles for further information and guidance.
Moein Moeinnia , My linkedin
My Other Articles: