Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker mount an directory of container to another container

I have an container Container-A which has directory /opt/test/data which contains files which get created during building image. Now I want to share /opt/test/data(container-a directory) to Container-B as it requires to use those files during its lifecycle. I have tried below option

docker run -it -d -v shareddata:/opt/test/data --name container-a test

and then run container-B as

docker run -it --volumes-from container-a --name container-b test-prod:latest

The data got shared but it creates an named volume shareddata on the host which contains a duplicate data of /opt/test/data directory of container-a.

I have tried another option of creating a name volume data and mounting it in container-a. Then I created a symlink of /opt/test/data on mounted volume as below

ln -s /data/data /opt/test/data

Now mounted the /data volume to container-b but the symlink didn't work on container-b as it shows it as broken links.

Is there a way I can share container-a directory to container-b without duplicating the data as we want to keep data which is static and quite big approx 2.5G to one image and then an instance keep running which is shared across multiple container of test-prod image?

like image 842
Abhinav Avatar asked Sep 12 '25 11:09

Abhinav


1 Answers

If files already exist in the directory of the container you're mounting as a volume, the contents of the directory will be copied to the volume. See Populate a volume using a container .

There's no way to mount a container's directory as a volume without copying. An alternative is to not store the data in a container and instead create a volume on the host and populate it with the data. This way, the data will only exist on once.

like image 162
six8 Avatar answered Sep 15 '25 02:09

six8