Docker provides two methods for transferring data across containers: volumes and bind mounts. While both methods serve the same goal, there are several major variations between them that you should be aware of.
In this blog article, we will look at the differences between Docker volumes and bind mounts, as well as when to use each of them.
Docker Volumes
Docker volumes are managed by Docker and are kept in a Docker-managed part of the host file system. Volumes are used for both data persistence and data exchange across containers. A volume can be given in the docker run command when creating a container, or it can be created before using the docker volume create command.
One of the main advantages of Docker volumes is that they can be easily managed using the Docker CLI. Volumes may be created, deleted, and inspected using simple commands. Volumes may also be backed up and restored, making them a good choice for long-term data storage.
Another advantage of Docker volumes is that they may be used by multiple containers at the same time. This means that data may be easily transferred between containers, which is especially useful in microservice architectures.
Example of Using Docker Volumes
Say you have a container running a database server, and you want to keep the data even if the container is stopped or deleted. To store the data on the host machine, you can use a Docker volume.
First, use the docker volume create command to build a volume:
$ docker volume create my-database-data
Next, start the container and mount the volume:
$ docker run -d –name my-database -v my-database-data:/var/lib/mysql mysql:latest
With this command, we create a container named my-database from the mysql image and mount the volume we created before with the -v option. The volume is mounted within the container to the /var/lib/mysql directory, which is where MySQL stores its data.
If we stop or delete the container, the data will still be available in the my-database-data volume.
Bind Mounts
Bind mounts, on the other hand, are a file or directory on the host machine that is mounted into a container. Bind mounts are set with the -v or —mount option in the docker run command. Bind mounts are handy for sharing files or directories between the host and the container, or between containers.
Bind mounts have the benefit of providing direct access to the host file system, which means that changes made to files in the container are instantly reflected on the host machine. This is handy for development processes in which changes to code in the container may be tested instantly on the host.
Example of Using Bind Mounts
Say you have a web application on your host machine that you want to run within a Docker container. You may use a bind mount to mount the source code directory on your host machine inside the container.
To start, go to the directory containing your web application code and run the container and mount the directory as a bind mount:
$ docker run -d –name my-webapp -v $[pwd]:/app -p 80:80 my-webapp-image:latest
With this command, we create a container named my-webapp from the my-webapp-image image, and we use the -v option to mount the current directory [$[pwd]] on the host machine to the /app directory inside the container. We’re also using the -p option to map port 80 in the container to port 80 on the host machine.
Any changes you make to the code in the $[pwd] directory on your host machine will now be immediately reflected within the container. This allows you to build and test your web application within the container while still using your chosen development tools on your host machine.
Differences between Volumes and Bind Mounts
Now that we’ve explored the advantages of both volumes and bind mounts, let’s look at the key differences between them.
1. Docker manages volumes, whereas the host manages bind mounts.
2. Volumes may be backed up and restored, but bind mounts cannot.
3. Volumes can be used by multiple containers at the same time, whereas bind mounts can only be used by one container at a time
4. Volumes can be quickly managed with the Docker CLI, whereas bind mounts need manual management.
When to use Volumes vs. Bind Mounts
The question is, when should you use each one? Here are some recommendations
1. Use volumes when you need to persist data between containers or share data between containers.
2. Bind mounts are useful when you require direct access to the host file system or when you need to deliver data to a container that is not contained within the container image.
Start with volumes if you’re unsure which choice to select. They are simpler to manage and provide maximum choice for data transfer between containers.
Thanks for spending time reading this article. Follow us for more blogs like this.