Docker Community Forums

Share and learn in the Docker community.

Docker share data between the containers

I’m using the docker version 1.10.3 . In that i want to access the data like ( /etc/java/configurations.yml ) from one container to another container using --link option . For example :

sudo docker run -d --name=test1 redis:latest

sudo docker run -d --link test1:test --name=test2 redis1:latest

i) test1 container has /etc/java/configurations.yml file.

ii) I want to access the above file from test1 to test2 container . Is it possible to do it.?

Your suggestions are highly appreciated and it would be helpful for me.

Thanks,
Kaleeswaran N

It is not possible for one container to directly access the file in another container. One option is the container exposes the files via such as NFS. Or both containers access the same volume.

By the way, --link will be deprecated. You should switch to use overlay network.

Reviving a three year OLD thread -
You can create a volume and “Mount” It in both containers giving you a common FS between them

docker volume create custom
custom

docker volume ls
DRIVER VOLUME NAME
local custom
docker volume inspect custom
[
{
“CreatedAt”: “2020-02-28T10:12:05-06:00”,
“Driver”: “local”,
“Labels”: {},
“Mountpoint”: “/var/lib/docker/volumes/custom/_data”,
“Name”: “custom”,
“Options”: {},
“Scope”: “local”
}
]
add the line to your Run on Both Containers -

-v custom:/usr/custom

and there you have it.
and

Docker Volumes can be created and attached in the same command that creates a container, or they can be created independently of any containers and attached later. In this article, we’ll look at four different ways to share data between containers.

Step 1 — Creating an Independent Volume
Introduced in Docker’s 1.9 release, the docker volume create command allows you to create a volume without relating it to any particular container. We’ll use this command to add a volume named DataVolume1:

docker volume create --name DataVolume1

The name is displayed, indicating that the command was successful:

Output
DataVolume1
To make use of the volume, we’ll create a new container from the Ubuntu image, using the --rm flag to automatically delete it when we exit. We’ll also use -v to mount the new volume. -v requires the name of the volume, a colon, then the absolute path to where the volume should appear inside the container. If the directories in the path don’t exist as part of the image, they’ll be created when the command runs. If they do exist, the mounted volume will hide the existing content:

docker run -ti --rm -v DataVolume1:/datavolume1 ubuntu

While in the container, let’s write some data to the volume:

echo “Example1” > /datavolume1/Example1.txt

docker volume inspect DataVolume1

Output
[
{
“CreatedAt”: “2018-07-11T16:57:54Z”,
“Driver”: “local”,
“Labels”: {},
“Mountpoint”: “/var/lib/docker/volumes/DataVolume1/_data”,
“Name”: “DataVolume1”,
“Options”: {},
“Scope”: “local”
}
]

Next, start a new container and attach DataVolume1:

docker run --rm -ti -v DataVolume1:/datavolume1 ubuntu

Verify the contents:

cat /datavolume1/Example1.txt

Output
Example1

===========================
Step 2 — Creating a Volume that Persists when the Container is Removed

docker run -ti --name=Container2 -v DataVolume2:/datavolume2 ubuntu

When we restart the container, the volume will mount automatically:

docker start -ai Container2

Let’s verify that the volume has indeed mounted and our data is still in place:

cat /datavolume2/Example2.txt

Output
Example2

Step 3 — Creating a Volume from an Existing Directory with Data

As an example, we’ll create a container and add the data volume at /var, a directory which contains data in the base image:

docker run -ti --rm -v DataVolume3:/var ubuntu

Step 4 — Sharing Data Between Multiple Docker Containers
Create Container4 and DataVolume4
Use docker run to create a new container named Container4 with a data volume attached:

docker run -ti --name=Container4 -v DataVolume4:/datavolume4 ubuntu
Create Container5 and Mount Volumes from Container4
We’re going to create Container5, and mount the volumes from Container4:

docker run -ti --name=Container5 --volumes-from Container4 ubuntu

View Changes Made in Container5
Let’s check for the changes that were written to the data volume by Container5 by restarting Container4:

docker start -ai Container4

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. …
Step 2: Create a New Container and Add to the Data Volume. …
Step 3: Verify You Can Share Data Between Docker Containers.