Docker Storage and Volumes
Estimated reading: 3 minutes 6 views

🐳 Docker Container to Container Volume Mapping – Complete Guide with Examples & FAQs

In Docker, volumes are the preferred mechanism for persisting data. One powerful use case is when you want to share a volume between multiple containersβ€”allowing them to read/write to the same data. This article will walk you through everything you need to know about container-to-container volume mapping.


πŸ”§ 1. Creating a Dockerfile with Volume Declaration

To begin, we’ll create a custom Docker image that declares a volume.

✍️ Dockerfile Example:

# Dockerfile
FROM ubuntu
VOLUME ["/data"]

🧱 Explanation:

  • FROM ubuntu: Uses Ubuntu as the base image.
  • VOLUME ["/data"]: Declares /data as a volume inside the container. This allows Docker to manage it.

πŸ”¨ Build the Image:

docker build -t myimage .

πŸ’‘ You can replace myimage with any tag you prefer.

πŸš€ Run the Container:

docker run --name container1 -it myimage /bin/bash

This launches a container named container1 from the image, with an interactive terminal. The /data directory is now an active volume.


πŸ“‚ 2. Declaring a Folder as Volume Using -v

Instead of declaring a volume in the Dockerfile, you can directly mount a host folder at runtime.

πŸ§ͺ Example:

docker run --name container2 -v /foldername -it ubuntu /bin/bash

πŸ“Œ Key Points:

  • /foldername will be treated as a volume.
  • Docker will manage it, even if it doesn’t exist on the host.
  • This approach is handy for one-off mounts or testing.

πŸ”„ 3. Sharing Volumes Between Containers Using --volumes-from

To share volumes between containers, use the --volumes-from option.

πŸ”— Example:

docker run --name container3 --volumes-from container1 -it ubuntu /bin/bash

🧩 What this does:

  • Mounts all volumes from container1 into container3.
  • Both containers can now access and modify the same data inside /data.

🏁 Final Thought

Using Docker volumes and sharing them between containers simplifies data management and allows persistent, collaborative storage for your apps. Whether you’re using a Dockerfile, -v, or --volumes-from, this guide has you covered.


❓ Frequently Asked Questions (FAQs)


πŸ”Ή Q1: What is the difference between declaring a volume in Dockerfile vs using -v at runtime?

A:

  • Declaring in a Dockerfile (VOLUME ["/data"]) is useful for making your volume part of the image design.
  • Using -v at runtime gives you more flexibility, such as mounting host directories.

πŸ”Ή Q2: Can I share volumes between containers from different images?

A:
Yes. As long as the container being launched uses --volumes-from another container, it will inherit all its volumes regardless of their image.


πŸ”Ή Q3: What happens to the volume if I delete the container?

A:
If the volume is unnamed and the container is removed, Docker may delete the volume. For persistence, use named volumes or external storage.


πŸ”Ή Q4: Can I share multiple volumes between containers?

A:
Yes. If the source container has multiple volumes declared or mounted, all of them will be shared when you use --volumes-from.


πŸ”Ή Q5: Can I use --volumes-from with a stopped container?

A:
Yes. Docker allows volume sharing even if the source container is stopped, as long as it still exists.


Leave a Reply

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

Share this Doc

Docker Container to Container Volume Mapping

Or copy link

CONTENTS
Scroll to Top