How to Share Data Between Docker Containers

Introduction

Docker allows users to run various applications isolated from the host computer, without the necessity of having separate operating systems for them to run on. Instead, you install and manage Docker containers with a containerization engine (Docker daemon), which has a similar role as the hypervisor for virtual machines.

As you start using Docker, you will face situations where you need to know how to share data between containers. 

Tutorial on how to share between Docker containers.

Prerequisites

  • A user with sudo privileges
  • Access to a terminal/command line
  • A stable release of Docker

Step 1: Create a Container with Data Volume

To demonstrate how to share between two containers you need to create a container (Container1) with data volume (datavolume1) you can later share.

1. First, create an independent volume which you will share between two Docker containers:

docker volume create --name DataVolume1

2. Then, create a Docker container and name it Container1 with data volume attached to it by running the following command:

docker run -ti --name=Container1 -v DataVolume1:/datavolume1 ubuntu

2. Next, create a file in the data volume and add some text to it:

echo "Share this file between containers" > /datavolume1/Example.txt

3. Exit the container with the command: exit

Create a container and add a data volume which you will share between containers.

Step 2: Create a New Container and Add to the Data Volume

Next, create a new container (Container2) which will share files with Container1.

1. Create Container2 and mount the volumes from Container1:

docker run -ti --name=Container2 --volumes-from Container1 ubuntu

2. Add text from Container2 to show that both containers can write to DataVolume1:

echo "This will also appear in DataVolume1" >> /datavolume1/Example.txt

3. To exit from Container2 use the command: exit

Create a container which shares a data volume with an existing Docker container.

Step 3: Verify You Can Share Data Between Docker Containers

1. Restart Container1 to check the changes to the data volume:

docker restart Container1

2. Confirm that both containers can read and write to the same data volume and that the text you wrote in Container2 appears in DataVolume1:

cat /datavolume1/Example.txt

If the text was successfully added to the data volume, the output should display the following:

Share this file between containers
This will also appear in DataVolume1

3. To finish, exit out of the container with the command: exit

Optional: Create Read-Only Volumes

Creating a “read-only” Docker container prevents others from making any changes to the data, allowing them only to view the files.

Add :ro to the container name which should be shared but not modified.

In the following example, Container2 will be allowed to see files from Container1 but will not be able to edit them.

docker run -ti --name=Container2 --volumes-from Container1:ro ubuntu

To check if Container2 has read-only privileges, try to remove the example file from Container1 by running the command:

rm /datavolume1/Example.txt

The output should show you have successfully created a read-only file. Consequently, other containers can access and view the data, but cannot make any contribution nor changes to it.

Conclusion

Now you know how to share files between Docker containers.

Also, this tutorial explained how to lock your files in Docker containers so that other parties cannot edit or remove them.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Set Up and Use Private Docker Registry
March 21, 2024

By setting up a private Docker registry, you can save valuable resources and speed up development processes...
Read more
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019

Docker allows users to create a container in which an application or process can run. In this guide, you will...
Read more
How to Manage Docker Containers? Best Practices
January 29, 2019

With Docker Container Management you can manage complex tasks with few resources. Learn the best practices of...
Read more
How to Install Docker on CentOS 7
October 22, 2018

Docker is an increasingly popular software package that creates a container for application development...
Read more