If you’re creating a container image (Docker image) for production, image size and security are two essential considerations. Fortunately, Docker’s Multi-Stage builds can help address both concerns.

In the development environment, container images often include development and build tools, as well as dependencies necessary for the development and build process. However, these tools and dependencies can increase the image size and pose a security risk.

By using Docker’s Multi-Stage Build feature, we can separate the development and build 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 short video tutorial, you’ll learn how to write a multi-stage Dockerfile to create both a local development and a production-ready, deployable Docker image.

Here’s a breakdown of the steps you’ll learn in the tutorial:

  1. Containerize an existing web application using Ruby and Ruby on Rails
  2. Start with a simple Dockerfile to build for local development and test running containers locally
  3. Update the Dockerfile to add a multi-stage structure, starting with 2 stages for local development setup
  4. Test running the container locally, built using a multi-stage Dockerfile
  5. Add more stages to package and build a final, production-ready and deployable Docker image
  6. Test running the production-ready image locally using Docker Compose