Mount add files to existing directory using configmap volume mount
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
By default, mounting a ConfigMap as a volume in Kubernetes replaces the entire target directory, hiding any existing files. To add files from a ConfigMap to an existing directory without overwriting it, use subPath in the volume mount. Each file must be mounted individually with its own subPath entry. Alternatively, mount the ConfigMap to a separate directory and use an init container to copy files. Understanding the difference between a full volume mount and subPath is essential for configurations like adding config files to /etc/nginx/conf.d/ or adding properties files alongside application JARs.
The Problem: Full Volume Mount Overwrites
This mount replaces all contents of /etc/nginx/conf.d/. Any files that the nginx image ships in that directory (like default.conf) are hidden. Only app.conf from the ConfigMap is visible.
Fix: Use subPath to Add Individual Files
Each subPath mount adds a single file to the directory without affecting other files. The existing default.conf in /etc/nginx/conf.d/ remains intact alongside the new app.conf and custom.conf.
How subPath Works
subPath tells Kubernetes to mount only a specific key from the ConfigMap volume as a file at the exact mountPath, rather than mounting the entire volume as a directory.
Multiple Files with items and subPath
Each ConfigMap key is mounted as a separate file. The /app/config/ directory retains any files baked into the container image.
Init Container Approach
The init container copies ConfigMap files into an emptyDir volume, which the main container then mounts. This avoids the subPath limitation where each file needs its own mount entry, but the directory starts empty (no image-baked files preserved).
Projected Volume for Multiple Sources
Projected volumes combine ConfigMaps and Secrets into a single volume, which can then be mounted with subPath to add individual files to existing directories.
Setting File Permissions
defaultMode sets permissions for all files in the ConfigMap volume. Individual files can override with mode. Note that subPath mounts do not receive automatic updates when the ConfigMap changes.
Common Pitfalls
- subPath files do not auto-update: When a ConfigMap is updated, files mounted with
subPathare NOT automatically refreshed in the running pod. Only full directory mounts receive live updates. WithsubPath, you must restart the pod to pick up changes. - Forgetting subPath causes directory replacement: Mounting a ConfigMap to
/etc/nginx/conf.dwithoutsubPathreplaces the entire directory. All original files (likedefault.conf) disappear. Always usesubPathwhen you need to preserve existing directory contents. - Each file needs its own volumeMount: With
subPath, you cannot mount all ConfigMap keys at once — each key requires a separatevolumeMountsentry. For ConfigMaps with many keys, this becomes verbose. Consider the init container approach instead. - File ownership is root by default: ConfigMap files are owned by root with the mode specified in
defaultMode(default 0644). If your application runs as a non-root user and needs to write to the same directory, the ConfigMap file's permissions may conflict. - ConfigMap size limit: ConfigMaps are limited to 1 MiB of data. For larger configuration files, use a persistent volume or bake the config into the container image. Exceeding the limit causes a creation error.
Summary
- Default ConfigMap volume mounts replace the entire target directory
- Use
subPathto add individual ConfigMap files to an existing directory without overwriting - Each file requires its own
volumeMountentry withsubPath - Files mounted with
subPathdo not auto-update when the ConfigMap changes — pod restart required - Use init containers to copy ConfigMap files when many files are involved
- Use projected volumes to combine ConfigMaps and Secrets into a single mount source
Related reading
- Mount local directory into pod in minikube
- Mounting kubernetes volume with User permission
- Mounting NFS Persistent Volumes with authentication
- MountVolume.SetUp failed for volume kube-api-access-cvwdt object default/kube-root-ca.crt not registered
- MountVolume.SetUp failed for volume kube-api-access-fcz9j object default/kube-root-ca.crt not registered
- MountVolume.SetUp failed for volume nfs mount failed exit status 32
- MountVolume.Setup failed for volume xxx couldn't get secret
- mTLS between two kubernetes clusters

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.