TNS
VOXPOP
Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
0%
No: TypeScript remains the best language for structuring large enterprise applications.
0%
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
0%
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
0%
I don’t know and I don’t care.
0%
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:


We now must add the Docker repository like so:


Install a few dependencies with the command:


Update apt:


Install the Docker CE runtime engine with the command:


Add your user to the docker group with the command:


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:


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


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:


Exit out of the container with the command:


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:


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:


The output of the command should be:


Outstanding.

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

Exit from that container with the command:


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


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


In that file, paste the following:

Deploy the first NGINX container with the command:


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:


In that file, paste the following contents:

Deploy the second NGINX container with the command:


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.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.