How to Share Data Between Docker Containers

Docker is a containerization platform that enables us to pack, deploy, and deliver applications, software, and programs in an isolated environment. Usually, a container has all the things that are required to run the application or program and due to isolation, it can not directly access the data from the host machine or from other containers. 

However, sometimes users do not want to store sensitive files directly in a container image as they may need to share them with others. For this purpose, they may need to save the file on the host or a shared space from which the container can access the required document. In such situations, users are often required to share data between Docker containers or containers and the host system.

In this article, we will demonstrate the method of sharing data between Docker containers and from the host to multiple Docker containers.

Sharing Data Between Docker Containers

Data can be shared between Docker containers using two basic methods:

  1. Docker Volume: Docker volume is utilized to persist container data outside the container. It acts as an external file system that retains Docker container data even after the container is deleted. A single independent volume (file system) can be shared among multiple containers to persist and share data between them.
  2. Shared Source or Directory: The second method involves sharing a common space or directory between different containers. This allows containers to save their data in a shared location, which can then be accessed by other containers for sharing purposes.

Method 1: Share Data Between Containers Using Independent Volume

To share data between Docker containers, first install Docker on Windows, then create an independent volume and mount it to different Docker containers.

Follow the below steps for practical illustrations.

Step 1: Create Volume 

First, create an independent volume in the Docker host through the following command:

docker volume create shared_vol
creating a volume

To inspect the created volume use the “docker volume inspect <vol-name>” command:

docker volume inspect shared_vol
inspect the created volume

Step 2: Generate First Container

Now, generate the first container and mount the created volume with the container:

docker run -it --name cont1 -v shared_vol:/shared_vol ubuntu

In the above command, “-it” starts the container’s TTY terminal, the “–name” option assigns the unique name to the container and the “-v” option is utilized to mount the shared_vol volume in the container’s shared_vol directory:

generate first docker container

Step 3: Create a File in the Shared Volume

Now, create the file inside the container using the “echo” command:

echo "Creating shared file" >>/shared_vol/file.txt
create a file in the shared volume

Step 4: Create Second Container 

In the next step, create another container named “cont2” and mount the shared_vol volume:

docker run -it --name cont2 -v shared_vol:/shared_vol ubuntu
create second container

Step 5: Verify the Data Sharing

Now, check if the data stored by “cont1” is accessible by “cont2” or not. For this purpose, read the file created by cont1 using the “cat <path/to/file>” command:

cat /shared_vol/file.txt

The output shows that data is effectively shared between two containers through independent Docker shared_vol volume:

verify docker container data sharing

Method 2: Shared Data From Persisted Volume Even Containers are Removed

Volume is external storage that saves the data even if containers are removed. To share data from an existing volume of containers with other containers, even after previous containers have been removed, go through the following section.

Step 1: Remove Docker Containers

First, remove the containers that are using the existing volume. For instance, we are using the above-generated volume and containers.

Remove the first container using the “docker rm <container name/id>” command:

docker rm cont1
remove docker containers

Similarly, remove the second container “cont2”:

docker rm cont2
remove second docker container

Step 2: Create a Container With Existing Volume

Now, generate the fresh container, mount the existing volume, and check if this container has access to the data that is generated by removed containers or not:

docker run -it --name cont3 -v shared_vol:/shared_vol ubuntu

To verify the data, just read the file generated by the deleted container using the “cat” command:

create a docker container with existing volume

The outcome above confirms that the new container effectively accesses the data through the volume, even though the old container no longer exists.

Method 3: Share Data Between Multiple Containers by Sharing Shared Host Directory

Another possible way to share Docker data or files is by using a shared source or directory that different containers can access. Follow these steps to share a single host directory with multiple containers:

Step 1: Create Shared Directory

First, create a new directory in the host system:

mkdir shared_dir
create shared directory

Step 2: Create a Container

Now, create a first container and mount the created directory as a shared source on the container path /shared_dir:

docker run -it --name cont-1 -v C:/Users/Dell/Documents/shared_dir:/shared_dir ubuntu
create a docker container

Step 3: Create a File in the Container

Create a file inside the container through the “echo” command:

echo "file is creating in shared directory" >>/shared_dir/file.txt
create a file in docker container

Step 4: Check the File in the Shared Directory

Now, access the shared directory from the host:

cd shared_dir

Also, you can run the following command to check if the file is saved in the mounted directory or not:

dir

The result indicates that file.txt is saved on the host system that is created by the above container:

check if the file is saved in the mounted directory

Step 5: Create Second Container

Now, create another container and mount the same shared host directory to the container’s /shared_dir path:

docker run -it --name cont-2 -v C:/Users/Dell/Documents/shared_dir:/shared_dir ubuntu

Next, access the file that is created by the first container and saved in the shared source shared_dir:

create a second docker container

That is all about sharing data between Docker containers.

Conclusion

Enhancing flexibility and collaboration is a key feature of sharing data between containers. You can achieve this by either mounting a single independent volume with a container or sharing the same data storage space or directory among different containers. In this article, we explained different methods of docker data sharing, along with screenshots.

Share data between Docker containers easily on Windows Server with Ultahost. It provides strong processing, more memory, and great storage. You can focus on managing your containers, while Ultahost ensures the speed and resources needed for smooth data sharing.

FAQ

Why would you need to share data between Docker containers?
What are methods for sharing data between Docker containers?
How do you share data using Docker volumes?
What happens to data stored in a Docker volume if the container is deleted?
How do you share data between containers using host directory?
Can multiple containers access the same Docker volume or shared directory simultaneously?
What are the advantages of using Docker volumes over shared host directories?

Related Post

How to Set Docker Environment Variables

Docker has revolutionized how we build, deploy, and run...

How to Install PHP Dependencies on Docker

As a web developer, you're likely no stranger to the im...

How to Install Docker on Debian

Docker is a powerful containerization platform that all...

How to Push and Pull a Docker Image from Dock

Docker is an open-source platform that enables develope...

How to Set Up and Use Private Docker Registry

Setting up a private Docker registry can significantly ...

How to List, Start and Stop Docker Container

Docker is a platform that allows you to build, deploy, ...

Leave a Comment