Example Dockerfile For Multi-Stage Docker Build For A NodeJS Application
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
- Take a light weight node image (for example node:10.15.2-alpine)
- Install all dependencies including dev dependencies
- Copy source code
- Run code coverage, linters, code analysis etc
- Create a second Docker image from the same node image
- This time install only production dependencies
- Copy the application code from the previous image
- 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.
- Use multi-stage builds from Official Doc
- How to build a smaller Docker image from medium.com
- A Tale of Two (Docker Multi-Stage Build) Layers from hackernoon.com
Happy Dockering !