Override A File Under Mounted Volume In Docker With .dockerignore

By | 2018 年 5 月 28 日

Scenario

Given the following file structure:

configs
├── config.docker.js
├── config.js
...
├── OTHER CONFIG FILES

The purpose is that we want to use config.docker.js as config.js in the built docker image and mount configs directory in the container while it’s running, so we can get the best of Docker.

The problem

However, it won’t work in the normal configurations in Dockerfile and the docker run command line:

In Dockerfile:

...
COPY ./configs /app/configs
RUN mv /app/configs/config.docker.js /app/configs/config.js
...

Command line:

docker run -v ./configs:/app/configs -v ./configs/config.docker.js:/app/configs/config.js THE_BUILT_IMAGE

It won’t work, in the container, we can see the second override volume mount option takes no effect. The config.docker.js and config.js mapped independently in the container.

The solution

To solve this problem, we can introduce .dockerignore file to ignore the config.js file in the building and mounting process:

In .dockerignore file

configs/config.js

After get the .dockerignore file ready, rebuild the image and run the same docker run command line. In the running container, we got:

-rw-r--r-- 1 root root  218 May 28 16:54 config.docker.js
-rw-r--r-- 1 root root  218 May 28 16:54 config.js

Both of them mapped to the same ./configs/config.docker.js file. And changes to this file locally will immediately reflect in the running container.

发表评论

电子邮件地址不会被公开。 必填项已用*标注