Bobcares

How To Share Data between Docker Containers?

by | Jan 27, 2021

Wondering How To Share Data between Docker Containers? We can help.

In this article, 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 Bobcares, we often get requests from our customers to share data between their Docker Containers.

Today let’s see how our support engineers implement this for our customers as a part of our Server Management Services.

 

How To Share Data between Docker Containers?

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

share data 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:

DRIVER VOLUME NAME
local DataVolume2

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

$ docker volume rm DataVolume2

[Need 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:

backups
cache
lib
local
lock
log
mail
opt
run
spool
tmp

 

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 assistance to share data from your Container? We are happy to help you!]

 

 

Conclusion

To conclude, we saw the steps followed by our Support Techs to share data between docker containers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.