Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

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

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes