Popular Searches

How to Mount or Symlink a Single File in a Docker Container

Docker volume and bind mounts are used to bind directories on the host OS to locations in the container’s file system. While they’re commonly used to mount entire directories, you can also use them to symlink individual files.

Symlink to Docker From The Host

Mounting entire directories works well if you’re overwriting a whole list of config files, but if you just want to target one, you’ll need to use a single-file mount. This is also useful if you want to have multiple mounts into the same directory, but from different locations. It works much like a regular Linux symlink, except it’s handled through Docker and crosses the bridge between host and container filesystem.

Normal Docker volume mounts require you to mount to a target directory, within which the volume will be linked to.

docker volume create nginx-config

docker run -d \
--name devtest \
--mount source=nginx-config,target=/etc/nginx \

However, with bind mounts, the source can be a directory or file on the host. Volume mounts don’t support this, and while you usually want to use them, you must use the lower level bind mounts if you want to mount single files.

To do this, you can run the container with the flag --mount type=bind , which support individual files if you specify the path:

docker run -it \
--mount type=bind,source=/path/file.cfg,target=/etc/example/file.cfg \
nginx sh

Alternatively, if you really wanted to use Docker volumes, you could make a volume with just the file you plan to mount, mount it to a temporary directory in the container, and then create an actual symlink inside the container (in the Docker build script) that would link to the temp directory with the file. That’s probably a worse solution than a bind mount overall, but it does allow you to store the data through the Docker volume API.

Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.