VOOZH about

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

⇱ How to Share Data between 2 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
2023-07-01 07:00:00
How to Share Data between 2 Docker Containers
Operations

How to Share Data between 2 Docker Containers

Imagine you could deploy multiple containers that all use the same data. With a bit of imagination, the sky's the limit for what you can do.
Jul 1st, 2023 7:00am by Jack Wallen
👁 Featued image for: How to Share Data between 2 Docker Containers
Photo by Michal Balog on Unsplash.

The Docker container ecosystem is full of really handy (and cool) tricks. One such trick is volumes, which allow you to deploy containers with persistent storage. Consider this: With persistent storage, you could deploy a container that saves data to a directory on your host machine.

Now, imagine you could deploy multiple containers that all use the same data. The implications and applications are numerous. You could serve the same site on multiple ports or even use the shared base directory and create subdirectories for different sites. With a bit of imagination, the sky’s the limit for what you can do.

I want to show you how you can share data between Docker containers. It’s much easier than you think (take that, Kubernetes).

What You’ll Need

The only thing you’ll need is an operating system that supports docker and a user with admin permissions. I’ll demonstrate this on Ubuntu Server 22.04. The only thing that will differ is how you install Docker on your platform of choice. If you already have Docker up and running, you’re already one step ahead.

Let’s get busy.

Installing Docker on Ubuntu Server

The first thing we’ll do is install Docker. To do that, 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

We now must add the Docker repository like so:

echo "deb [arch=amd64 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

Install a few dependencies with the command:

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

Update apt:

sudo apt-get update

Install the Docker CE runtime engine with the command:

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

Add your user to the docker group with the command:

sudo usermod -aG docker $USER

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

Share Simple Data between 2 Containers

The first thing I’ll demonstrate is the basic sharing of data between two containers.

To begin with, create a new volume with the command:

docker volume create --name shared-data

With the volume created, deploy a basic container that uses the volume with the command:

docker run -ti --rm -v shared-data:/shared-data ubuntu

The above command will not only deploy the container but it will also place you within the container at the bash prompt. Let’s create a new file in the shared-data volume with the command:

echo "Hello, TNS!" > /shared-data/test.txt

Exit out of the container with the command:

exit

Just because we’ve exited the container, the volume is still up and running and houses the test.txt file we created.

Deploy a second container that also uses the shared volume with the command:

docker run -ti --rm -v shared-data:/shared-data ubuntu

Once again, you’ll find yourself at the bash prompt of the running container. Since we’ve already created the test.txt file in the volume, it should be available to the second container. Test that by issuing the command:

cat /shared-data/test.txt

The output of the command should be:

Hello, TNS!

Outstanding.

Now, let’s see how we can share a data volume between two NGINX containers.

Exit from that container with the command:

exit

Create a directory on the host computer to house the volume with the command:

mkdir ~/nginx-data

In that directory, create an index.html file with the command:

nano ~/nginx-data/index.html

In that file, paste the following:

Deploy the first NGINX container with the command:

docker run --name docker-nginx -p 8082:80 -d -v ~/nginx-data:/usr/share/nginx/html nginx

Once the container deploys, point your web browser to http://SERVER:8082 (where SERVER is the IP address of your Docker server) and you should see the custom index.html page we created (Figure 1).

👁 Image

Figure 1: Our NGINX container is serving data from the volume.

Let’s now create another index file, called index2.html with the command:

nano ~/nginx-data/index2.html

In that file, paste the following contents:

Deploy the second NGINX container with the command:

docker run --name docker-nginx -p 8083:80 -d -v ~/nginx-data:/usr/share/nginx/html nginx

After the container deploys, point your web browser to http://SERVER:8083/index2.html (where SERVER is the IP address of your Docker server) and you should see the content of the second index file (Figure 2).

👁 Image

Figure 2: Our second NGINX container is using the same volume as the first.

You could also point the browser to http://SERVER:8083 (where SERVER is the IP address of your Docker server) and it will display the contents of the first index file.

Congratulations, you’ve taken yet another important step in your Docker journey. With this skill and a bit of creativity, you can do far more than you might imagine.

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.