This happens because you have added your worker
directory as a volume to your docker-compose.yml
, as the volume is not mounted during the build.
When docker builds the image, the node_modules
directory is created within the worker
directory, and all the dependencies are installed there. Then on runtime the worker
directory from outside docker is mounted into the docker instance (which does not have the installed node_modules
), hiding the node_modules
you just installed. You can verify this by removing the mounted volume from your docker-compose.yml
.
A workaround is to use a data volume to store all the node_modules
, as data volumes copy in the data from the built docker image before the worker
directory is mounted. This can be done in the docker-compose.yml
like this:
redis:
image: redis
worker:
build: ./worker
command: npm start
ports:
- "9730:9730"
volumes:
- ./worker/:/worker/
- /worker/node_modules
links:
- redis
I’m not entirely certain whether this imposes any issues for the portability of the image, but as it seems you are primarily using docker to provide a runtime environment, this should not be an issue.
If you want to read more about volumes, there is a nice user guide available here: https://docs.docker.com/userguide/dockervolumes/
EDIT: Docker has since changed it’s syntax to require a leading ./
for mounting in files relative to the docker-compose.yml file.