Docker image cache as dependency during multi-stage DinD build?

TL;DR I want to store the image cache from a job as an artifact so that I can use locally built images in downstream jobs. How do I do that?

Full question:

I want to have a three-stage build process in which I (1) create a Docker image, (2) run tests within it, and (3) push the resulting image to the GitLab repository if the current branch is master and all the tests pass.

I am using a shared runner, so I believe (but am not certain) that my only option for doing so is the “Docker-in-Docker” approach as discussed in the GitLab CI documentation.

Each job runs within an independent Docker container. As such, if I build a Docker image during the build stage, I cannot access that image unless I push it to the GitLab registry. But that means pushing to the registry even if the current stage is not master, or if the tests don’t ultimately pass.

The ideal solution would be to treat the locally built Docker image as a dependency for the downstream jobs, and not get the remote registry involved until all appropriate conditions are met. Is there a way to do this?

1 Like