Docker volumes have become increasingly popular for managing and sharing data in containerized applications. They provide a range of advantages like enhanced flexibility, seamless data portability, and robust security. Docker volumes play a vital role in managing and scaling applications by ensuring reliable data storage and accessibility.
In the following topic, you will look at what volumes are, the different types of volumes, and how to use them.
What are Docker volumes?
Docker volumes are a handy way to save data created by containers. You can save data in Docker volumes even after the containers are gone. This is useful for applications that require data persistence, such as databases or file servers. You can easily share data between containers by using Docker volumes, making it easier to develop, test, and deploy applications.
Types of Docker volumes
Docker volumes come in three types: anonymous, named, and host-mounted.
| Anonymous volumes | Automatically created by Docker when a container is created. Anonymous volumes do not have a user-defined name. They are typically used for storing temporary data that does not need to be shared between containers. |
| Named volumes | Created by the user and can be given a specific name. This makes it easy to identify and reuse them across multiple containers. Named volumes are often used when data needs to be shared between containers. |
| Host-mounted volumes | Directories on the host machine that are mounted into a container. This allows access to data that is stored on the host machine from within the container. Host-mounted volumes are useful when you want to provide persistent storage or when you need to share files between the host and the container. |
In addition to Docker volumes, there are other ways to manage data in Docker, such as bind mounts. Bind mounts are directories on the host machine that are mounted into a container, similar to host-mounted volumes. However, bind mounts do not create a new layer, which means changes made in the container are reflected on the host machine and vice versa.
Access options
Docker volumes also provide various access options. For example:
| Name | Description |
|---|---|
| Read-Only |
|
| Read-Write |
|
| SELinux |
|
Private |
|
Nocopy |
|
Shared |
|
Basic example
Here's an example of two containers sharing a volume:
Create a Docker volume using the command: docker volume create mydata
- This step creates a new volume called mydata, using the Docker command
docker volume create. A volume in Docker is a directory on the host machine (PC) that multiple containers can share and access.
Launch the first container and mount the volume: docker run -d --name container1 -v mydata:/data busybox sleep 3600
- In this step, container1 is launched using the Docker command
docker run. The-dflag runs the container in the background (detached mode). The--name container1flag assigns the name container1 to this particular container instance. The-v mydata:/dataflag mounts the previously created volumemydatato the container's/datadirectory. This allows the container to read and write data to the volume. Lastly,busybox sleep 3600is the command that is executed inside the container. The command puts the container to sleep for 3600 seconds (1 hour).
Enter the first container and write some data to the mounted volume
- The Docker command
docker exec -it container1 shis used to enter into the running container1 interactively. This opens a shell inside the container. - The command
echo "Data from container 1" > /data/shared.txtwrites the text "Data from container 1" to the file/data/shared.txtwithin the container's file system. Since the volume is mounted at/datadirectory, this data is stored in the volume on the host machine. - The command
exit, exits the container's shell.
Launch the second container and bind the same volume: docker run -d --name container2 -v mydata:/data busybox sleep 3600
- Here, another container named container2 is launched using
docker run. Similar to the previous step, the-dflag runs the container in the background,--name container2assigns the name container2 to this instance, and-v mydata:/datamounts the same volume "mydata" to the/datadirectory inside the container. This binding allows the second container to access the same data stored in the volume. - Again,
busybox sleep 3600is executed inside the container and puts it to sleep for an hour.
Enter the second container and verify that the data from the first container is present:
- The Docker command
docker exec -it container2 shis used to enter into the running container2 interactively, opening a shell inside the container. - The command
cat /data/shared.txtreads and displays the contents of the file/data/shared.txtwithin the container. Since the volume is mounted to this directory and it is the same volume used by container1, the data previously written by container1 will be accessible here. - Finally, the command
exitis used to exit the container's shell. - You should see that the second container has access to the same data written by the first container. This demonstrates how multiple containers can share and access data from the same Docker volume.
docker volume create mydatadocker run -d --name container2 -v mydata:/data busybox sleep 3600docker exec -it container2 shcat /data/shared.txtexit
It is worth knowing that Docker has different volume drivers with extra features. But for now, you should stick to the types of volumes that Docker suggests and provides without any extra installation.
Advantages of Docker volumes
Docker volumes offer several advantages. They provide a way to store and share data between containers, which makes it easy to share data between different applications running in Docker. They also provide data persistence, ensuring that data is not lost when a container is destroyed. Additionally, Docker volumes are portable and can be easily moved across different machines.
Conclusion
Docker volumes are a useful tool for managing data in Docker containers that offer flexibility, portability, and data persistence. By using volumes, developers can create more complex and robust applications that can easily share and access data between containers and host machines. By understanding Docker volumes and their advantages, developers can make the most out of this Docker feature.