When creating a container image for production, two key factors to consider are image size and security. Multi-Stage builds in Docker provide a solution to these concerns.
In the development environment, container images used by developers often include development and build tools, as well as dev and build-time dependencies. However, these build and development dependencies not only increase the size of the image but also pose a security risk.
By using Multi-Stage builds, we can separate the build and development stage from the final production stage of creating container images. This way, the image built for production or deployment to any environment will only contain the necessary components for the app to run in production.
In this video, I will demonstrate how to use a multi-stage Dockerfile to create both a local development and a production-ready, deployable Docker image.
Following are the steps to walk through,
- Learn how to containerize an existing web application using Ruby/Rails
- Discover how to start with a simple, non-multi-stage Dockerfile to build for local development and test running containers locally
- Learn how to update the Dockerfile to add a multi-stage structure, starting with 2 stages for local development
- Discover how to test running the container locally, built using a multi-stage Dockerfile
- Learn how to add more stages to package and build a final, production-ready and deployable image
- Discover how to test running the production-ready image locally using Docker Compose
- Source Code: https://github.com/devteds/multi-stage-docker-build
- Kubernetes on AWS: Hands-on Get Started course: https://devteds.com/k8s