Search code examples
node.jsdockerdockerfile

Speed up NPM install in Docker container


We use the standard practices of not including node_modules in version control. However, when moving through the CI/CD pipeline, we have to reinstall NPM dependencies in several places and it makes everything very slow.

Is there a way to somehow cache NPM dependencies with Docker? I searched Google "docker cache npm dependencies" and the first hit from 2014 yielded a dead link.

Nothing else of much value came up.

One solution is to include node_modules in version control, but I think that would be a huge mistake. I think caching the dependencies somehow would be the best option.

Here is the Dockerfile as is:

FROM node:6

COPY . .  # copy all files, but node_modules does not exist ( => gitignored)

RUN npm install --no-optional --only=production > /dev/null 2>&1
RUN npm install -g bower  > /dev/null 2>&1
RUN bower install --config.interactive=false --allow-root > /dev/null 2>&1

ENTRYPOINT ["/bin/bash", "/root/cdt/run.sh"]

Here is one possible solution, but I can't quite figure out how it works:

=> http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/


Solution

  • This method works like magic:

    https://blog.playmoweb.com/speed-up-your-builds-with-docker-cache-bfed14c051bf

    Docker has a special way of caching things for you, and apparently it's best to use the inborn caching ability.

    Cannot say I completely understand how it works, but it does work.

    If you follow this pattern, it will work for you:

    FROM mhart/alpine-node:5.6.0
    WORKDIR /src
    
    # Expose the port 3000
    EXPOSE 3000
    
    # Set the default command to run when a container starts
    CMD ["node", "server.js"]
    
    # Install app dependencies
    COPY package.json /src
    RUN npm install
    
    # Copy your code in the docker image
    COPY . /src