TNS
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%
NEW! Try Stackie AI
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
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).

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).

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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.