Volume and File-Based Secret Mounting
What you'll learn: How to securely deliver secrets to applications by mounting them as files in the filesystem, rather than passing them through environment variables.
The Core Idea
Instead of injecting secrets as environment variables (which can leak through process listings or error logs), many platforms allow you to mount secrets as files or volumes at runtime. The application reads these files to retrieve credentials, API keys, or certificates. This approach keeps secrets out of the process environment entirely.
Think of it like this: rather than shouting your password across a room (environment variables), you lock it in a secure drawer and give the application a key to open that specific drawer when needed.
How It Works
Orchestration platforms like Kubernetes, Docker, and cloud services can project secrets into a container's filesystem as temporary volumes. These appear as regular files at predictable paths (e.g., /run/secrets/db-password), but they:
- Exist only in memory (tmpfs), never written to disk
- Are isolated per container
- Can have restricted file permissions (read-only, specific user/group access)
- Disappear when the container stops
Your application code reads from these file paths at startup or runtime, just like reading any configuration file.
Managing File Permissions
Critical security practice: set strict file permissions on secret files. Typically:
- Owner: the application user (not root)
- Permissions:
0400(read-only by owner) or0600(read-write by owner only) - No group or world access
This ensures even if an attacker compromises part of your container, they cannot access secrets unless they compromise the specific application user.
Key Takeaway: Volume and file-based secret mounting delivers secrets as filesystem files with controlled permissions, keeping them out of environment variables and process memory, while enabling fine-grained access control at the OS level.