Skip to content

Push more than one image when using Dockerfiles with multi-stage builds

I am using Dockerfile to build my application with Auto Build DevOps and GitLab on Premises. In order to keep the image size down I am following one of the Docker best practices for writing Dockerfile: Docker multi-stage builds. It generates in my case two images: one with tools related to the compile/test/development environment (first stage of build) and another to the runtime/production environment (second stage of build).

I would like to push both images to GitLab Registry during the Auto Build. In this way, I could make Docker builds faster with Docker layer caching in subsequent jobs. For example, in job test I could execute the following scripts, based on this post. During the docker build, the dev image generated by the first stage of build could be retrieved from registry using --cache-from, accelerating tests.

docker pull my-docker-image:dev || true
docker pull my-docker-image:latest || true
docker build -t my-docker-image --target dev --cache-from=my-docker-image:dev --tag my-docker-image:dev .
docker run my-docker-image /app/vendor/bin/phpunit ...
docker push my-docker-image:dev

However, the Auto Build DevOps currently supports only single-stage images. The build.sh script used by Auto Build performs only one build and push. In this way only one image is pushed (prod image generated by the second stage of build). The two images generated by the multi-stage build are not pushed to registry.