When working with Docker, mounting volumes is a standard way to share data between your host and containers. While volumes are commonly used to mount entire directories, sometimes you only need to mount a single file—like a configuration file, certificate, or environment-specific script.
In this blog post, we’ll show you how to mount a single file into a Docker container, using both command-line examples and best practices.
📦 Volume Mounting in Docker: Quick Recap
Docker supports two primary types of mounts:
- Volumes (managed by Docker)
- Bind mounts (link host files/directories to containers)
When you want to mount a specific file from your host machine into the container, you’re using a bind mount.
✅ How to Mount a Single File
You can mount a single file using the -v
or --mount
flag when running a container.
🔹 Using -v
(short syntax)
docker run -v /path/on/host/my.conf:/etc/myapp/my.conf my-image
🔹 Using --mount
(long syntax)
docker run --mount type=bind,source=/path/on/host/my.conf,target=/etc/myapp/my.conf my-image
Both commands mount my.conf
from the host to /etc/myapp/my.conf
inside the container.
📝 Important Considerations
1. ✅ The File Must Exist on the Host
The file /path/on/host/my.conf
must already exist before running the container. Docker will not create it for you.
You can create a file with:
touch /path/on/host/my.conf
2. ✅ Container Path Must Be a File (Not a Directory)
Make sure the target path in the container matches the structure you intend. If you accidentally specify a directory path where a file should go, the container might behave unexpectedly.
3. 🔐 File Permissions
Be cautious with file permissions:
- On Linux, Docker uses the host filesystem permissions.
- On Windows/macOS, Docker Desktop handles this through virtualization, but permissions still matter.
Ensure the container has the right access (read/write) to the mounted file.
🧪 Example: Mounting a Config File
Let’s say you have a custom config file app.conf
and want to mount it into your container at /app/config/app.conf
.
Step 1: Create the file on your host
echo "PORT=8080" > ./app.conf
Step 2: Run the container with the mounted file
docker run -d \
-v $(pwd)/app.conf:/app/config/app.conf \
my-node-app
Now, inside the container, the application can read /app/config/app.conf
.
🧩 When to Use File Mounts
Mounting individual files is especially useful when:
- Injecting environment-specific configs into containers.
- Sharing SSL certificates or API keys.
- Replacing a single file in an otherwise unchanged volume.
- Testing different configurations without rebuilding the image.
🧼 Cleaning Up
If you no longer need the container:
docker rm -f container-name
The file remains on your host, but the mount is no longer active.
✅ Summary
Task | Command |
---|---|
Mount single file (short syntax) | -v /host/file:/container/file |
Mount single file (long syntax) | --mount type=bind,source=/host/file,target=/container/file |
Ensure file exists on host | touch /path/to/file |
Check permissions | chmod / adjust ownership if needed |
Conclusion
Mounting a single file into a Docker container is a clean and efficient way to customize configuration or inject data without creating full volumes or modifying your image. It’s especially useful in development and testing workflows where flexibility and speed matter.
By mastering file-level mounts, you’ll be able to build more modular, configurable, and maintainable container setups.