free counter
Science And Nature

How exactly to Mount a Docker Volume While Excluding a Subdirectory

Docker logo

Docker Volumes provide persistent storage to your containers. Data stored in volumes is stored independently of containers so that it could be restored after restarts and replacements. Volumes support use with multiple containers simultaneously, facilitating data sharing situations.

Mounting a Docker volume makes its contents offered by a particular directory path in the target container. Everything within the volumes filesystem tree becomes accessible. This may develop a challenge if you need to exclude specific subdirectories in the quantity data. In this post, youll learn a straightforward way of mounting volumes while avoiding certain paths.


The default behavior of mounting the complete volume is generally desirable. Volumes are designed to store data developed by containers so their content ought to be highly relevant to your applications.

Volumes may also be populated using host bind mounts. These directly map a directory on your own machine to a path within your container. Changes made within the host directory will undoubtedly be automatically reflected in the container.

Bind mounts are generally used to accelerate application development. It is possible to modify source code and observe your changes without needing to rebuild your Docker image. Project working directories often contain some folders that you dont wish to be mirrored though, such as for example node_modules and vendor. These may already exist in your container, populated through the image build steps. Excluding your neighborhood folders from being mounted enables you to reliably test thoroughly your code utilizing the dependencies supplied by your image.

How exactly to Exclude Subdirectories from Docker Volume Mounts

Subdirectories could be excluded from the volume mount utilizing a simple technique: create another mount at the road you would like to ignore. If youre mounting ~/app to /opt/app in your container, it is possible to exclude the ~/app/node_modules directory by mounting another empty volume to /opt/app/node_modules:

$ docker run --name app 
    -v ~/app:/opt/app 
    -v /opt/app/node_modules 

This container begins with the contents of one’s hosts ~/app directory accessible at /opt/app. However /opt/app/node_modules will support the original content supplied by the bottom image, rather than your hosts ~/app/node_modules directory.

This works because Docker automatically populates newly created empty volumes with the prevailing content of the destination path theyre mounted to. In the event that you run npm install in your Dockerfile, /opt/app/node_modules will already contain all of your dependencies. The initial volume mount binds your host directory in to the container however the second one overrides it having an empty volume at /opt/app/node_modules. That is then populated with the files and folders contained in the image.

The order of one’s volume mounts is essential the subdirectory mount must apply following the less specific parent bind. Otherwise the contents of ~/app, including its version of node_modules, find yourself overriding the empty volume thats designed to create the exclusion.

Excluding Files

You may use a similar strategy to effectively exclude individual files. Mounting your hosts /dev/null to the file path will blank it out, as though it had no content.

$ docker run --name app 
    -v /dev/null:/opt/app/config.yaml 

This only works to exclude files /dev/null wont map onto directory paths. The technique doesnt work to omit a file while retaining the initial version from your own base image either. Itll map /dev/null onto the road, replacing any existing file thats there.

Using Docker Compose

These techniques both work with Docker Compose too. Adjust your service definitions volumes section to add your regular bind mount and a proper empty volume override.

services:   app:     image: app-image:latest    build: .    volumes:      - ~/app:/opt/app      - /dev/null:/opt/app/config.yaml      # Exclude file      - /opt/app/node_modules               # Exclude directory

Running docker-compose up could have exactly the same effect because the plain docker run example shown above.


Docker volume mounts replace everything in the containers destination path with the contents of the bound host directory. In a few situations you might like to customize this behavior by excluding specific host paths from being mounted.

In this post youve seen how mounting a clear volume to a subdirectory in the container will override the initial bind. This content of the container subdirectory will revert to the initial content supplied by your image. A variation of the technique could also be used for files, even though path will undoubtedly be overwritten with /dev/null instead.

Read More

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker