How can I share data between two running Docker containers

0 votes
How can I share data between two running Docker containers?

In many scenarios, two or more Docker containers must access and share the same data. For instance, an application container may require reading or writing data that another service container also requires. What are the best practices for sharing data between containers, and what are the advantages and disadvantages of each? Please discuss volumes, named volumes, and Docker networks as alternatives in which you explain how they enable data persistence and availability across containers.
Nov 5 in Docker by Anila
• 5,040 points
69 views

1 answer to this question.

0 votes

Share data between two running containers by various means depending on the use case. The most common and effective ways include Docker volumes, bind mounts, and inter-container communication through a shared network. Here's how each one works and when it is most applicable:

Using Volumes
Docker volumes are the suggested approach to persisting data as well as sharing data among containers because volumes are controlled by Docker, hence it's portable and secure among environments.

Create a named volume: You can have both containers share a named volume.

Mount the volume in both containers: You have to mount the volume at appropriate path in each of the containers when running them.

Access Data in Both Containers: Any data written to /data in one container will be available to the other, because both containers are using the shared_data volume.


This is useful if you need to share data files, databases, or configuration files between containers. Volumes are also persistent beyond container lifecycle, meaning that data won't be lost when containers are restarted.

Using Bind Mounts
Bind mounts allow you to map a directory on the host machine to a directory in each container, so you can share data with a shared host directory between containers.

Setup shared host directory Create a new shared space on your host, /shared_folder.

Mount the Shared Host Directory in both containers: Now run your two containers, each bound mount pointing to /shared_folder.

Access the data in both containers: Both the containers can access the /data written to it through the host directory.
This kind of mounts is useful while we need the data being written to a container needs to be accessible to some other containers and also needed to be accessible on the host for doing some back-up or any host level operation.

Shared Network and Inter-Container Communication
In situations where containers must share data in real-time using network requests such as through a REST API or TCP connections, one can use a Docker network
Create a Docker Network: In this step, you could create a custom bridge network.
Connect Containers to the Network: Start or join each container to this network.

Share Data using Networking Protocols: Containers can communicate with each other by referencing the container name as a hostname. For instance, if there is a container called container1, it can reach a container called container2 over a protocol like http://container2:port.

  • It is useful for microservices or applications that have some need to communicate in real-time but do not want to share data using a file-based mechanism.
  • Shared mounts enable sharing between the host and containers so that a shared host directory exists.
  • Shared networks allow information to be shared in a real-time basis through the use of network calls. There is also an application for when you have services that run inside multiple containers and no file data has to be accessed between these containers.
answered Nov 5 by Gagana
• 7,530 points

Related Questions In Docker

0 votes
1 answer

Share data between two different containers - Docker

Hi @Hannah, follow these steps: 1. Create an ...READ MORE

answered Nov 25, 2019 in Docker by Aria
1,880 views
0 votes
1 answer

How can I connect a docker container to two docker networks that I have created?

Hi@akhtar, It's correct that the network argument of ...READ MORE

answered Mar 23, 2020 in Docker by MD
• 95,460 points
10,908 views
+15 votes
19 answers

How can I run a ‘docker exec’ command inside a docker container?

you can run any command in a ...READ MORE

answered Dec 10, 2018 in Docker by Pramiti
1,229,880 views
+1 vote
1 answer

How do I pass environment variables to Docker containers?

You can pass environment variables to your ...READ MORE

answered Jul 20, 2018 in Docker by Kalgi
• 52,350 points
1,712 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,071 views
+2 votes
1 answer
0 votes
1 answer

How can I securely manage secrets (like API keys) within Docker containers?

For the secure management of secrets like API keys, passwords, or ...READ MORE

answered Nov 21 in Docker by Gagana
• 7,530 points
67 views
0 votes
1 answer

How can I reduce Docker container startup time, especially for large applications?

There is a need to make container startup faster for real-time deployments. Here's what you can tune: Use ...READ MORE

answered Nov 21 in Docker by Gagana
• 7,530 points
64 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP