Getting npm packages to be installed with docker-compose

If you’re trying to deploy your node app into a docker container and you’re also using docker-compose, here’s something to watch out for.

If you are running npm install on your Dockerfile, you might want to make sure that you mount /node_modules as a data volume in your docker-compose.

For eg. your Dockerfile might look something like this:

# Dockerfile
FROM node:5.5.0
ADD ./ /node_app
WORKDIR /node_app
RUN npm install

Normal stuff. Mount your app from the host to your container and install the npm dependencies. Nothing should go wrong right?

Unfortunately, if you also use docker-compose to bring up other services together with your node app, your compose might look something like:

  build: .
  command: node /node_app/dist/index.js
    - db
    - ./:/node_app
  image: rethinkdb

If you run docker-compose up you might encounter issues of missing modules even though npm install ran successfully and you’re sure every dependency is installed.

This is because the mounting of the volume by docker-compose would supercede the node_modules that was installed in your built image. This causes the volume to overwrite ontop of the built image.

In order to overcome this, you’ll need to make sure that you mount the directory as a data volume.

In your docker-compose.yml, just change:

# docker-compose.yml
    - ./:/node_app
    - /node_app/node_modules

Now if you run your docker-compose up, you should be able to see your node application running successfully now.