Docker-compose: node_modules not present in a volume after npm install succeeds
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.
The node_modules
folder is overwritten by the volume and no more accessible in the container. I'm using the native module loading strategy to take out the folder from the volume:
/data/node_modules/ # dependencies installed here
/data/app/ # code base
Dockerfile:
COPY package.json /data/
WORKDIR /data/
RUN npm install
ENV PATH /data/node_modules/.bin:$PATH
COPY . /data/app/
WORKDIR /data/app/
The node_modules
directory is not accessible from outside the container because it is included in the image.
The solution provided by @FrederikNS works, but I prefer to explicitly name my node_modules volume.
My project/docker-compose.yml
file (docker-compose version 1.6+) :
version: '2'
services:
frontend:
....
build: ./worker
volumes:
- ./worker:/worker
- node_modules:/worker/node_modules
....
volumes:
node_modules:
my file structure is :
project/
│── worker/
│ └─ Dockerfile
└── docker-compose.yml
It creates a volume named project_node_modules
and re-use it every time I up my application.
My docker volume ls
looks like this :
DRIVER VOLUME NAME
local project_mysql
local project_node_modules
local project2_postgresql
local project2_node_modules