Bobcares

WeSupport

Call Us! 1-800-383-5193
Call Us! 1-800-383-5193
Call Us! 1-800-383-5193

Need Help?

Emergency Response Time custom

Our experts have had an average response time of 11.06 minutes in March 2021 to fix urgent issues.

We will keep your servers stable, secure and fast at all times for one fixed price.

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 *

Docker Support

Spend time on your business, not on your servers.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.
Docker Support

Spend time on your business, not on your servers.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.

Categories:

Tags:

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF