Developing a node app in docker

Problem

We want to rapidly develop our node app inside a docker container, being able to install modules, make code changes, and see instant results. The problem is that while the official node image supports a handy onbuild feature which will grab the package.json and install everything we need, this also means having to rebuild the image every time a dependency changes.

Solution

Use this image, which places the node_modules folder one level higher, meaning it isn’t overwritten by the docker mount, and you can still mount and install your own node modules on the fly.

Edit:

An easier but less obvious way to solve the problem is to specify your /usr/src/app/node_modules folder as a volume with no mapping to the host. This preserves the container copy and allows you to keep your local copy.

Example:

volumes: – /usr/src/app/node_modules

When you’re deploying the image and need to copy the entire app in, you can use the .dockerignore file to prevent your host node_modules from being loaded into the build context, improving build time