Computer scienceSystem administration and DevOpsDockerDocker resources

Docker volumes

10 minutes read

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-Only access can be specified using the ro option when mounting a volume. This prevents data from being modified by the container.
Read-Write
  • Read-Write access can be set with the rw option, allowing data from the volume to be both modified and read by the container.
SELinux
  • This option ensures that the container has exclusive read-write access to the volume and prevents other containers or processes from accessing or modifying it. It can be set with the flag z.
  • This access option is specific to systems with SELinux enabled, which includes certain Linux distributions like CentOS or Fedora.
Private
  • The private access option allows you to mount a volume with private visibility.
  • It makes the volume accessible only to the container in which it is defined.
  • Other containers running on the same host won't have access to this volume.
Nocopy
  • The nocopy access option prevents Docker from copying the volume data from the host into the container when it starts.
  • Instead, the container starts with an empty directory at the mount point, and any changes made to the volume are persisted only on the host.
  • This option can be useful when you want to create a container that shares an existing volume without duplicating its data.
Shared
  • The shared access option allows multiple containers to share the same volume.
  • By default, volumes are shared. So it is not always necessary to specify this option.

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 -d flag runs the container in the background (detached mode). The --name container1 flag assigns the name container1 to this particular container instance. The -v mydata:/data flag mounts the previously created volume mydata to the container's /data directory. This allows the container to read and write data to the volume. Lastly, busybox sleep 3600 is 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 sh is used to enter into the running container1 interactively. This opens a shell inside the container.
  • The command echo "Data from container 1" > /data/shared.txt writes the text "Data from container 1" to the file /data/shared.txt within the container's file system. Since the volume is mounted at /data directory, 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 -d flag runs the container in the background, --name container2 assigns the name container2 to this instance, and -v mydata:/data mounts the same volume "mydata" to the /data directory inside the container. This binding allows the second container to access the same data stored in the volume.
  • Again, busybox sleep 3600 is 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 sh is used to enter into the running container2 interactively, opening a shell inside the container.
  • The command cat /data/shared.txt reads and displays the contents of the file /data/shared.txt within 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 exit is 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 mydata
  • docker run -d --name container2 -v mydata:/data busybox sleep 3600
  • docker exec -it container2 sh
  • cat /data/shared.txt
  • exit

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.

25 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo