Here is an example Dockerfile for building node.js application using docker multistage builds.

Steps to build a typical Node.js application using multistage docker build feature

  1. Take a light weight node image (for example node:10.15.2-alpine)
  2. Add package.json
  3. Install all dependencies including dev dependencies
  4. Copy source code
  5. Build
  6. Run code coverage, linters, code analysis etc
  7. Create a second Docker image from the same node image
  8. This time install only production dependencies
  9. Copy the application code from the previous image
  10. Push the production image to Docker registry or run it from here.

Why you should be using Multi-Stage Docker Builds?

  • Reduce Image size
  • Easy to ship and maintain
  • Simplifies CICD pipeline
  • Reduce security risks

If you want to learn more about Multi-Stage Docker builds here are some nice reads for you.

Happy Dockering !