Share Data between Docker Containers

Are you trying to share Data between Docker Containers?

This guide is for you.

Here, we will create Docker volumes and then share data between them.

Docker Containers allow to package up any application with all parts of it such as libraries and other dependencies and deploy it as one package.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to share data between their Docker Containers.

In this context, we shall look into how to implement the creation of Docker volumes.

How To Share Data between Docker Containers?

We will see how data can be shared between Docker Containers.

Docker Volumes are mounted on Docker containers to store data the Docker Container generates while running.

The access to data is provided with Docker Volumes. It allows us to create a volume without relating it to any particular container with the help of commands.

A ubuntu 20.04 server with Docker installed and having the sudo user in the Docker group will be considered as the prerequisite to implement this.

Steps to Share Data between Docker Containers

The steps that our Support Techs follow for sharing data between Docker Containers are given below:

Creating an Independent Docker Volume

We will create a volume, attach it to a container, and verify its persistence with the following steps:

1. First, use the following command to add a volume named DataVolume1:

$ docker volume create --name DataVolume1

'DataVolume1' can be seen as output for the above command.

2. Next, we will create a new container from the Ubuntu image, using the –rm flag to automatically delete it when we exit and -v to mount the new volume.

$ docker run -ti --rm -v DataVolume1:/datavolume1 ubuntu

3. Then from the container we will write some data to the volume:

# echo "Example1" > /datavolume1/Example1.txt

4. We can verify that the volume is present on our system with the following command:

$ docker volume inspect DataVolume1

5. Next, we will start a new container and attach DataVolume1:

$ docker run --rm -ti -v DataVolume1:/datavolume1 ubuntu

Finally, we can verify if the content persists:

# cat /datavolume1/Example1.txt

Creating a Volume that Persists when the Container is Removed

In this step, we will create a volume and container simultaneously, delete the container, then attach the volume to a new container.

1. First we will create a new container and a volume 'DataVolume2'.

$ docker run -ti --name=Container2 -v DataVolume2:/datavolume2 ubuntu

2. Next we will add data to the volume, verify it, and exit the container using the following commands:

# echo "Example2" > /datavolume2/Example2.txt
# cat /datavolume2/Example2.txt
# exit

2. The volume gets mount automatically when we restart the container.

$ docker start -ai Container2

3. Finally, we will verify that the volume is mounted and exit:

# cat /datavolume2/Example2.txt
# exit

4. Docker does not allow to remove volumes if they are referenced by a container, we will check what happens when we try to remove:

$ docker volume rm DataVolume2

We can see an error response with the long version of container ID:

Error response from daemon: unable to remove volume: remove DataVolume2: volume is in use - [d0d2233b668eddad4986313c7a4a1bc0d2edaf0c7e1c02a6a6256de27db17a63]

5. Using this ID we will remove the container:

$ docker rm d0d2233b668eddad4986313c7a4a1bc0d2edaf0c7e1c02a6a6256de27db17a63

6. To ensure that removing the container did not affect the volume we can list the volumes present in the docker and check:

$ docker volume ls

We can see the following output:

local DataVolume2

7. And we can use docker volume rm if we wish to remove it:

$ docker volume rm DataVolume2

[Need urgent assistance to create volume? We are happy to help you! ]

Creating a Volume from an Existing Directory with Data

1. First, we will create a container and add the data volume at /var with the following command:

$ docker run -ti --rm -v DataVolume3:/var ubuntu

2. Then exit the current container.

3. To view the contents of the volume without entering the shell:

$ docker run --rm -v DataVolume3:/datavolume3 ubuntu ls datavolume3

It can be seen that directory datavolume3 has a copy of the contents of the base image’s /var directory:


Sharing Data Between Multiple Docker Containers

The steps to follow are given below:

1. We will create Container4 and DataVolume4 with the following command:

$ docker run -ti --name=Container4 -v DataVolume4:/datavolume4 ubuntu

2. Next, we will create a file and add text and exit the container:

# echo "This file is shared between containers" > /datavolume4/Example4.txt
# exit

3. After that, we will create Container5 and mount volumes from Container4 with the following command

$ docker run -ti --name=Container5 --volumes-from Container4 ubuntu

3. Then we will check the data persistence:

# cat /datavolume4/Example4.txt

4. We will append some text from Container5 with the following command and exit the container:

# echo "Both containers can write to DataVolume4" >> /datavolume4/Example4.txt
# exit

5. We can view changes made in Container5 by restarting Container4:

$ docker start -ai Container4

6. To check the changes made we can use the following:

# cat /datavolume4/Example4.txt

7. And exit the container:

# exit

[Need urgent assistance to share data from your Container? We are happy to help you! ]


This article will guide you on steps to share #data between #docker #containers. To mount a data volume to a container add the --mount flag to the docker run #command. It adds the volume to the specified container, where it stores the data produced inside the virtual 3environment.

From the Docker #host, as Docker Volumes: Volumes are stored in an area of the host filesystem that's managed by Docker. Bind mounts can map to any folder in the host filesystem, so access can't be controlled from a Docker process and can pose a security risk as a container could access sensitive OS folders.

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

To copy from Docker to local container:

1. First, set the #path in your localhost to where the file is stored.

2. Next set the path in your docker container to where you want to store the file inside your docker container.

3. Then copy the file which you want to store in your docker container with the help of CP command.

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on

Also for Tech related tips, Visit

Related Posts

Focus on your business, not your servers.

Click Here to Learn More

Recent Post