The way to Mount or Symlink a Single File in a Docker Container

Docker quantity and bind mounts are used to bind directories on the host OS to areas within the container’s file system. Whereas they’re generally used to mount total directories, you may as well use them to symlink particular person recordsdata.

Symlink to Docker From The Host

Mounting total directories works effectively in the event you’re overwriting a complete listing of config recordsdata, however in the event you simply wish to goal one, you’ll want to make use of a single-file mount. That is additionally helpful if you wish to have a number of mounts into the identical listing, however from completely different areas. It really works very similar to an everyday Linux symlink, besides it’s dealt with by way of Docker and crosses the bridge between host and container filesystem.

Regular Docker quantity mounts require you to mount to a goal listing, inside which the quantity can be linked to.

docker quantity create nginx-config

docker run -d 
--name devtest 
--mount supply=nginx-config,goal=/and so forth/nginx 

Nonetheless, with bind mounts, the supply is usually a listing or file on the host. Quantity mounts don’t help this, and whilst you normally wish to use them, you have to use the decrease stage bind mounts if you wish to mount single recordsdata.

To do that, you possibly can run the container with the flag --mount sort=bind , which help particular person recordsdata in the event you specify the trail:

docker run -it 
--mount sort=bind,supply=/path/file.cfg,goal=/and so forth/instance/file.cfg 
nginx sh

Alternatively, in the event you actually needed to make use of Docker volumes, you would make a quantity with simply the file you intend to mount, mount it to a short lived listing within the container, after which create an precise symlink contained in the container (within the Docker construct script) that may hyperlink to the temp listing with the file. That’s most likely a worse answer than a bind mount total, nevertheless it does mean you can retailer the info by way of the Docker quantity API.

Supply hyperlink