Reputation: 3389
I'm developing a NodeJS app which uses webpack
server.Webpack
server is configured to take care of automatic file reloads. I want this to be mounted as a volume so that there is no need to rebuild the image every time I change the code. I want the node_modules
folder to be available within the image so that I don't have to get the modules each time I start the container.
Actually the source code and node_modules
should be at the same hierarchy level. But the problem is when I do a volume mount of my source code, node_modules
(since it is part of the image) will be missing as volume mounted path will be used.
Is there any way I can get this thing working?
Upvotes: 0
Views: 251
Reputation: 56946
I know this problem well!
When you mount a volume it deletes the files inside the directory mounted inside the container (well, technically it does not delete them but for our purposes it does). This is because the files are part of the BUILD and not part of the RUN.
There are 3 fixes:
1 - Instead of doing an npm install
at build time, do it at runtime - e.g. when you execute the docker run
command. Container files created at runtime inside the mounted directory WILL BE available from the host.
ENTRYPOINT npm install (something like that)
2 - Copy the files somewhere else during the build (e.g. /temp/node_modules) and then copy them back at runtime
ENTRYPOINT scriptToCopyFilesBackIntoNodeModules.sh
Either of the first two should be used if you need access from the host. If you need access from another container use volumes_from
- this just simply works without any of the above workarounds.
container1:
volumes: /node_modules
container2:
volumes_from: container1
It is something like that - the above syntax is docker-compose syntax.
Upvotes: 1