VOOZH about

URL: https://thenewstack.io/containers/how-to-share-data-between-docker-containers/

⇱ How to Share Data Between Docker Containers - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-02-11 09:11:39
How to Share Data Between Docker Containers
tutorial,
Containers

How to Share Data Between Docker Containers

there will be times when you need to have a centralized volume of data that more than one container can share. You need to be able to share that data between containers.
Feb 11th, 2022 9:11am by Jack Wallen
👁 Featued image for: How to Share Data Between Docker Containers

Let’s talk Docker. After all, without Docker, your entry into the world of containers might be a bit of a challenge. Imagine, your first steps with containerized deployments being centered completely on Kubernetes. That could quickly overwhelm the newly-minted.

What I want to specifically talk about is sharing data between containers within the realm of Docker.

If you’re new to Docker, you might be thinking, “Okay, that sounds cool.” If you already have a solid understanding of containers, you might be asking, “But aren’t containers supposed to be self-sufficient entities?” Yes, they are. But that doesn’t mean they can’t (or shouldn’t) share data.

Consider this: You deploy containers that need to share data. You might have a website, a web application, and a mobile application that all depend on the same data. Imagine the challenge it would be to keep each of those containerized applications in sync with one another.

What happens if the service in charge of the sync fails and you wind up with three different containers with three different sets of data. That could wind up being your biggest nightmare challenge of the week. Not necessarily an impossible task, but it’s certainly not one you’d want to deal with.

Now, before we get into this, know that your containers will most likely be self-sufficient. However, there will be times when you need to have a centralized volume of data that more than one container can share. Say, for example, you deploy multiple instances of an application or service that needs to use the same persistent data or cache. These could be simple websites, or even more complex database-driven applications. No matter the use case, you need to be able to share that data between containers.

Let me show you how this is done. We’re going to start from the very beginning, so get ready to head back to the basics.

How to Install Docker

I’m going to be demonstrating on my go-to server, Ubuntu. If you use a different distribution of Linux (or even Windows or macOS), make sure to alter the installation steps accordingly.

The first thing we’re going to do is install the necessary Docker dependencies. For this, log into your Ubuntu server instance and issue the command:

sudo apt-get install ca-certificates curl gnupg lsb-release -y

We’ll now add the official Docker GPG with the command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add the required Docker repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

We can now update apt and install Docker with the command:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y

When the installation completes, dd your user to the docker group with:

sudo usermod -aG docker $USER

Finally, log out and log back in, so the changes take effect.

Create a Volume

The first thing to be done is the creation of a volume that will house our data. Let’s create a volume named persistent-data with the command:

docker volume create --name persistent-data

The volume is created in the /var/lib/docker/volumes directory. You can add data to that folder, but you can only do so as the root user.

Now that our volume has been created, we can then deploy our first container, which will use the persistent volume. The commands to deploy the container looks like this:

docker run -ti --name=conatiner1 -v persistent-data:/data ubuntu:latest

The above command will create a container named container1, mount the persistent-data volume into the /data directory within the new container (based on the latest version of Ubuntu). It will also give you access to the running container.

Once the container is deployed, and you have access to the bash prompt, create a new file in the /data directory with the command:

echo "Hello, New Stack" >> /data/test.txt

Exit out of the running container with the command:

exit

We’ll now deploy a second container with the command:

docker run -ti --name=conatiner2 -v persistent-data:/data ubuntu:latest

At the container’s bash prompt, type the command:

cat /data/test.txt

You should see the following printed out:

Hello, New Stack

Let’s install the nano editor with the following commands:

apt-get update
apt-get install nano -y

Edit the test.txt file with the command:

nano /data/test.txt

Add the following at the bottom of the file:

This data is shared between container 1 and container 2.

Save and close the file.

Exit the container with:

exit

In order to once again gain access the containers, we have to redeploy them. First, locate the container ID with the command:

docker ps -a

Start both the containers (because they exited with the exit command) with:

docker start ID

Where ID is the ID of the containers.

Access the container with:

docker access -it ID /bin/bash

Where ID is the container ID for container1.

Issue the command:

cat /data/test.txt

You should see (in both instances of text.txt):

Hello, New Stack
This data is shared between container 1 and container 2.

Exit the running container with the exit command. This time around, both containers will remain running. You can stop and remove them with the commands:

docker stop ID
docker rm ID

Where ID is the container ID for each container.

And that’s all there is to share data between Docker containers with the help of volumes.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.