How to Use Volume Sharing in Docker Swarm?


Docker Swarm is a popular container orchestration platform that allows users to deploy and manage containers at scale. One of the key features of Docker Swarm is its support for volume sharing, which allows containers to access and share data stored in persistent volumes. In this article, we will explain what volumes are, how they are used in Docker Swarm and show examples of how volume sharing can be implemented in different scenarios.


  • Basic knowledge of Docker and containerization

  • Familiarity with Docker Swarm

  • Understanding of volumes in Docker

  • Experience with the command line

What is Docker Swarm?

Docker Swarm is a tool for deploying and managing containerized applications at scale. It provides a simple and efficient way to scale out applications across a cluster of nodes. It offers a range of features for building and maintaining highly available systems.

To use Docker Swarm, you will need to install Docker on your system and create a Swarm cluster. You can create a Swarm cluster by running the following command −

$ docker swarm init --advertise-addr --listen-addr


Swarm initialized: current node (xi0zolslqrszt94yed0wglxxo) is now a manager.
To add a worker to this swarm, run the following command:
   docker swarm join --token SWMTKN-1-4phd8znfpiprjuijltfdlz62bq5yghdmwhnpj0rf8f7kyv-bgpelv8wu5n2rzdmexhsc8wvq
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

This will create a single-node Swarm cluster, with the current node serving as the manager. You can then add additional nodes to the Swarm by running the docker swarm join command on the other nodes, and specifying the manager's IP address and port number.

What are volumes in Docker?

Volumes are a way to store data in Docker, separate from the container's image or file system. They can be used to persist data between container restarts, share data between containers, or migrate data between environments.

There are two main types of volumes in Docker −

  • bind mounts

  • volumes

Bind mounts allow you to mount a file or directory from the host system into the container, while volumes are managed by Docker and are stored in a separate location on the host system.

To create a volume in Docker, you can use the docker volume create command like this −

$ docker volume create my-volume



This command will create a new volume named "my-volume" that any container can use. You can also create a volume and mount it to a container at the same time, using the -v flag when running the container −

$ docker run -d --name my-container -v my-volume:/container/data Ubuntu



This command will create a new container named "my-container" based on the Ubuntu image and mount the "my-volume" volume to the /container/data directory inside the container.

How does Docker Swarm implement volume sharing?

In Docker Swarm, volumes are managed by volume drivers or plugins that handle the creation, management, and removal of volumes. Docker Swarm comes with a built-in volume driver called "local," which stores volumes on the host system, but you can also use other volume drivers such as NFS, GlusterFS, or Ceph.

To create a volume in Swarm, you can use the docker service create command like this −

$ docker service create --name my-service --mount type=volume,source=my-volume,target=/usr/local/apache2/htdocs/ httpd


overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged 

This command will create a new service named "my-service" based on the httpd image and mount the "my-volume" volume to the /usr/local/apache2/htdocs/ directory inside the containers that are part of the service.

You can also specify the volume driver to use when creating a volume, using the `--mount

--mount type=volume,source=my-volume,target=/app/data,volume-driver=nfs

This option will create a volume using the NFS volume driver, which allows you to store volumes on a remote NFS server.

To share a volume between multiple services in Swarm, you can use the --mount flag when creating the additional services, and specify the same volume name and target path. For example −

$ docker service create --name my-service-2 --mount type=volume,source=my-volume,target=/app/data ubuntu

This command will create a new service named "my-service-2" that shares the "my-volume" volume with the "my-service" service. The containers in both services will be able to access and modify the data in the volume.

You can also use the docker volume inspect command to view the details of a volume in Swarm, including its driver, mount points, and size −

$ docker volume inspect my-volume


This command will output information about the "my-volume" volume, such as the following −

   "CreatedAt": "2022-12-31T00:00:00Z",        
   "Driver": "local",  "Labels": {},        
   "Mountpoint": "/var/lib/docker/volumes/my-volume/_data",        
   "Name": "my-volume",        
   "Options": {},        
   "Scope": "local"    

Use cases for volume sharing in Docker Swarm

Volume sharing can be helpful in a variety of scenarios in Docker Swarm, including −

  • Storing persistent data − By storing data in a volume, you can ensure that it is not lost when a container is stopped or removed. This type can be helpful for storing databases, logs, or other data types that need to be retained over time.

  • Sharing data between services − By sharing a volume between multiple services, you can allow them to access and modify the same data. This type can be helpful in sharing files, cache data, or other types of data that need to be accessed by multiple services.

  • Migrating data between environments − By using a volume driver that supports storing volumes on a remote server, you can easily migrate data between different environments, such as from a development environment to a production environment.

There are also some challenges and considerations to keep in mind when using volume sharing in Docker Swarm, such as −

  • Performance − Depending on the volume driver and storage backend, volume performance may vary. For example, using a networked volume driver such as NFS may result in slower performance than a local volume driver.

  • Data consistency − When multiple services or containers access and modify the same data in a volume, it is crucial to ensure that the data remains consistent and is not corrupted. This consistency can be achieved by using appropriate locking mechanisms or by designating a single service as the primary writer of the data.


Here we have explained volumes and how they can be used in Docker Swarm. We have also provided examples of how volume sharing can be useful in different scenarios.