HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Docker tags for multi-stage builds for series of Docker images out of one Dockerfile

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
imagesmultibuildsdockertagsonedockerfilestageforseries

Problem

Thanks to Docker multi-stage builds, we can compile some artefacts in one environment and put them for execution in other one.

As result, there are two images, where the final corresponding to the last stage and provided tag gets tagged as defined; there is no tag for intermediate (ones), but they remain there as well.

Question: is it possible to create a series of tagged images for all defined stages using multi stage build out of one Dockerfile?

Solution

You can build and tag any stage of a multi-stage build. Just use the --target option to docker build. E.g.

docker build --target jdk -t myapp-jdk:v1 .
docker build --target jre -t myapp:v1 .


With the layer caching, the the earlier steps will be reused from the cache and not rerun from scratch each time, so there's little downside to running the build to different targets multiple times.

If you absolutely need to run this from a single build command, you can parse the output of the build, get the image id of the intermediate step, and tag that image id:

docker tag  myapp-jdk:v1

Code Snippets

docker build --target jdk -t myapp-jdk:v1 .
docker build --target jre -t myapp:v1 .
docker tag <some_image_id> myapp-jdk:v1

Context

StackExchange DevOps Q#4447, answer score: 15

Revisions (0)

No revisions yet.