In the multi-stage construction of Dockerfile, how to specify a real mount path?

  docker, question

Can docker only mount one directory at runtime? Because I use multi-phase build (refer to …), the dependency needs to be downloaded again in the first phase of each build.

I think it is possible to mount a path on the parent machine to the third-party library that is relied on in the first stage, and the next construction or other project construction will be much faster.

Can it be done?

Let’s take Node.js, which I am a little familiar with, as an example. you probably don’t want to create the node_modules dependency package folder in the container, but to mount a directory of the host in the past, right? But in the process of construction, do you need to download and rely on more than once? Or are there multiple dependent package folders?
I haven’t done any experiments, but I guess even if you have more than one installation package folder, the containers can be mounted separately at runtime, then this should be possible:

docker run -d -v /dir1:/dir1 -v /dir2:/dir2 image:version

However, in theory, this is not very good. After all, the mirror’s dependency package is too involved with the running environment. This is how I got Dockerfile:

COPY ./package.json /app
 CMD npm install
 COPY ./ /app
 RUN npm start

I first copy the dependency configuration file into the mirror, then install the dependency package, and then copy the project.
In this way, as long as the configuration file of the dependent package does not change, the mirror layer in the step of installing the dependent package can be reused and the construction speed will be greatly improved.

I hope I can help you.