How to Mount a Single File in a Docker Volume

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

TaskCommand
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 hosttouch /path/to/file
Check permissionschmod / 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.

Sharing Is Caring:

Leave a Comment