How to mimic '--volumes-from' in Kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes, with its robust architecture, is designed to manage containerized applications efficiently across a cluster of machines. While transitioning from Docker's straightforward composition capabilities, many developers confront the challenge of simulating features they have previously enjoyed, such as Docker Compose's --volumes-from. This option allows for the sharing of volumes between containers seamlessly. In Kubernetes, this requires a bit of manual configuration, though the platform provides powerful tools for achieving similar results. This article delves into how one can mimic Docker's --volumes-from functionality in Kubernetes, complete with technical explanations and examples.
Understanding Docker's --volumes-from
In a Docker setup, the --volumes-from option is a straightforward way to allow a new container to mount volumes from another container. This is often used when you want multiple containers to share data or configuration without explicitly defining volume paths for each.
For instance, in Docker you might use:
Here, container2 automatically mounts the same volume as container1.
Mimicking --volumes-from in Kubernetes
Kubernetes handles volumes differently through its PersistentVolume (PV), PersistentVolumeClaim (PVC), and ConfigMap abstractions. These concepts allow for the sharing of storage across containers within a pod or across multiple pods.
Explanation and Example
- Defining a Persistent Volume (PV): The PV is a resource in the cluster that represents a piece of storage. It can be backed by a physical disk, NFS, or a cloud provider's managed storage.
- Claiming the Persistent Volume through a Persistent Volume Claim (PVC): PVC is a request for storage by a user. It is similar to a pod.
- Configuring Pods to Use the PVC: Multiple pods can mount the same PVC, effectively sharing the data between the containers in those pods.
Summary Table
| Concept | Description |
| Persistent Volume (PV) | Represents a piece of storage in the cluster. |
| Persistent Volume Claim (PVC) | A request for storage, which pods can use to claim storage resources. |
| VolumeMounts | Allows a container to specify where to mount the storage resource. |
| Volumes | Specifies the storage resource that is to be available to the container. |
Additional Details
- Access Modes: The access modes for PV and PVC dictate how storage is accessed by the pods. The "ReadWriteOnce" mode allows the volume to be mounted as read-write by a single node. Other modes like "ReadOnlyMany" or "ReadWriteMany" can allow different sharing configurations.
- Flexibility with ConfigMap: Besides just sharing data volumes, one can also leverage
ConfigMapandSecretfor distributing configuration data and sensitive information between containers in a similar way. - Storage Classes: Kubernetes supports
StorageClassto provide dynamic provisioning of storage. This means you don’t have to manually create PVs as they will be allocated dynamically based on the specification in the StorageClass.
In conclusion, while Kubernetes does not have a direct equivalent of Docker's --volumes-from feature, with the effective use of its storage abstractions like PV, PVC, and ConfigMaps, you can achieve a similar functionality. This mimics the sharing of volumes between containers, fostering a more integrated and orchestrated container-based development environment.
Related reading
- How to mock the Kubernetes cluster/server?
- How to monitor disk usage of kubernetes persistent volumes?
- How to mount a Host folder in minikube VM
- How to mount a postgresql volume using Aws EBS in Kubernete
- How to mount a host directory in a Docker container
- How to mount a host directory in a Docker container
- How to mount /dev/kvm in a non-privileged pod?
- How to mount multiple files / secrets into common directory in kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.