Play Video

Building Production-Ready Docker Images with Multi-Stage Builds


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,

  1. Learn how to containerize an existing web application using Ruby/Rails
  2. Discover how to start with a simple, non-multi-stage Dockerfile to build for local development and test running containers locally
  3. Learn how to update the Dockerfile to add a multi-stage structure, starting with 2 stages for local development
  4. Discover how to test running the container locally, built using a multi-stage Dockerfile
  5. Learn how to add more stages to package and build a final, production-ready and deployable image
  6. Discover how to test running the production-ready image locally using Docker Compose