Efficient Docker builds for large monorepos

Claudio Jolowicz
The Startup
Published in
10 min readMay 24, 2019

--

Read the original article on my blog

Photo by Thomas Kelley on Unsplash

As a developer working on a monolithic codebase, how can you use Docker to build and deploy the projects contained in it? If you take the naive approach, you quickly run into problems with bloated images and frequent rebuilds of the entire source tree.

In this post, I show you how to build images from monorepos incrementally, reusing previous builds beyond the Docker build cache. The solution I describe avoids code duplication, reduces image size, and speeds up builds dramatically. If you’re a developer who needs to run frequent integration tests on your work in progress, then this technique is for you.

Sample code is available in a GitHub repository. Each section in the post corresponds to a commit in the GitHub repository, linked to at the top of the section.

Overview

  1. Introduction
  2. Example codebase
  3. Writing Dockerfiles
  4. Avoiding code duplication
  5. Using Docker Compose for a builder image
  6. Reducing image size
  7. Building incrementally
  8. Summary

Introduction

--

--

Claudio Jolowicz
The Startup

Claudio Jolowicz is the author of the O'Reilly book "Hypermodern Python Tooling" and a senior software engineer at Cloudflare. https://a.co/d/0daDjiqP