How can I build a Docker image and use it in subsequent steps without using an external registry?

HOW TO -️ October 18, 2021

A CircleCI workflow consists of three jobs which

  1. Compile & test the software
  2. Push a new Docker image to ECR
  3. Deploy to EKS

The first job, which compiles and tests, is based on the cimg/elixir image.

This works, but by now quite a lot of steps are duplicated between the CircleCI YAML configuration of first job (for installing all kinds of dependencies required at both build- as well as runtime) and the Dockerfile which is used by the second job. I'm looking for a way to resolve this duplication.

My idea was to adjust the Dockerfile such that it's a multi-stage build, with a specific build_env target which gets me the image which is used right before compilation starts. It's this exact image I'd like to use for the first job, for compiling and testing. The CircleCI workflow could then start out with an initial job which builds the image (passing the --target parameter to docker build) and then job 1) above could execute within that image. However, it seems that this requires using an external Docker registry -- pushing a few hundred megabytes over the Internet just to download them again in the very next job (the only usage!) appears a little wasteful.

A very brief support article suggests to use docker save and docker load -- but it's not clear to me how docker load would then help with actually running a CircleCI job within that newly-loaded image: how could I build a Docker image as part of a CircleCI workflow and then have subsequent jobs execute within that container without pushing it anywhere?


The docker load commands loads an image into the local Docker dameon; you can then docker run to start a container using that image. It sounds like that's exactly what you want to do.