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. 

How to Share Between Docker Containers

Prerequisites

  • Ubuntu 18.04 Bionic Beaver
  • A user with sudo privileges on Ubuntu
  • Access to a terminal/command line
  • A stable release of Docker

Tools/Software

How to Share Between Docker Containers

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. Firstly, create a Docker container and name it Container1 with data volume attached to it by typing in 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

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

4. Restart Container1 to check the changes to the data volume: docker restart Container1

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

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

Create read-only volumes

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.