Sharing Volumes Between Containers Rumi, July 28, 2020 There are many situations where it is useful to share a Docker volume between containers, and several ways to accomplish this goal. Sharing a Volume on the Host If you create a volume on the host machine, it can be used by multiple different containers at once. This allows you to share data between containers and the host. For this example we will create a directory on the host, and use that directory as a shared volume between two containers. Begin by creating a directory to use as a Docker volume with the command: sudo mkdir /webdata Create a small test file in this directory with the command: sudo echo "Hello from the host." >> /webdata/host-hello.txt Next, launch a container named sql-database from the official PostgreSQL image, and map /webdata on the host to /data on the container with the command: sudo docker run -it --name sql-database -v /webdata:/data postgres /bin/bash Once you are at the new container’s command prompt verify that the shared volume is set up correctly with the command: ls /data You will see the host-hello.txt file which we created on the host. Let’s add a file to this shared volume with the command: echo "Hello from the sql-database container." >> /data/sql-hello.txt Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt. Now launch a container named webapp from the official PHP+Apache image, and map /webdata on the host to /var/www/html on the container. sudo docker run -it --name webapp -v /webdata:/var/www/html php:5.6-apache /bin/bash Once you are at the new container’s command prompt, verify that the shared volume is set up correctly with the command: ls /var/www/html You will see both the host-hello.txt file which we created on the host, and the sql-hello.txt file we created on the sql-database container. Let’s add a file from this container as well: echo "Hello from the webapp container." >> /var/www/html/webapp-hello.txt Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt. Once on the host machine, you will see all three files listed with the command: sudo ls /webdata Now that the two containers are sharing a directory which “lives” on the host, data can be transferred instantly between all three locations simply by moving it to that directory. Using a Container as a Shared Data Volume You can also set up a separate container as a shared data volume. To do this, first create the data container. Then, when you create the container that will be using that data container, add the following argument to the docker run command: --volumes-from [name or ID of data container] Note: This will work whether or not the target container is running. Docker volumes are never deleted, and persist even after the container has been stopped. For this example we will create a data container called data-storage which will serve as the data volume, and two other containers that share it as a storage volume. First, launch the data-storage container from the official CentOS 7 image: sudo docker run -it -v /shared-data --name data-storage centos /bin/bash Then add a small file to the /shared-data folder: echo "Hello from the data-storage container." >> /shared-data/data-storage-hello.txt Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt. Now launch the app container from the official Python image and mount the data-storage container as a volume: sudo docker run -it --name app --volumes-from data-storage python /bin/bash List the files in the shared volume with the command: ls /shared-data As you can see, the /shared-data folder has been mounted from the /shared-data folder on the data-storage container, and contains the data-storage-hello.txt file. Let’s add one from this container: echo "Hello from the app container." >> /shared-data/app-hello.txt Detach from the container with Ctrl-p + Ctrl-q and return to the host machine’s command prompt. Finally, launch the web container from the official Apache image and mount the data-storage container as a volume: sudo docker run -it --name web --volumes-from data-storage httpd /bin/bash List the files in the shared volume with the command: ls /shared-data You will see the files we created on the data-storage and app containers listed here. Src: https://www.ionos.com/community/server-cloud-infrastructure/docker/understanding-and-managing-docker-container-volumes/ Administrations Configurations (Linux) DockerDocker NFSDocker Volume