<p>I stumbled upon a solution which I thought was quite elegant, and only requires you to build once:</p>
<p>Use LABEL feature in your multistage builds, and then after building, you can tag based on label.</p>
<p>Advantages:</p>
<ul>
<li>only pull together the build context once</li>
<li>fast command to tag after the build has been done</li>
</ul>
<p>The example was discussed in <a href="https://forums.docker.com/t/tag-intermediate-build-stages-multi-stage-build/34795" rel="nofollow noreferrer">https://forums.docker.com/t/tag-intermediate-build-stages-multi-stage-build/34795</a></p>
<blockquote>
<p>Using the LABEL command in my dockerfile I added a label to the build
stage I’m interested in, then filter for it later:</p>
<pre><code>FROM node as builder-stage
LABEL builder=true

FROM node as app-stage
LABEL builder=false
</code></pre>
<p>Now to filter, sort newest first, cut out the date, and take the top
row:</p>
<pre><code>docker images --filter “label=builder=true” --format ‘{{.CreatedAt}}\t{{.ID}}’ | sort -nr | head -n 1 | cut -f2
</code></pre>
<p>Note that it is important to remove the label in the next build stage.</p>
</blockquote>
<p>This approach works really well for me</p>

CC BY-SA 4.0
2021-03-20T08:12:00.143Z
2021-03-20T08:12:00.143Z
Jesper Rønn-Jensen
<p>You can build and tag any stage of a multi-stage build. Just use the <a href="https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage" rel="noreferrer"><code>--target</code> option</a> to <code>docker build</code>. E.g.</p>

<pre><code>docker build --target jdk -t myapp-jdk:v1 .
docker build --target jre -t myapp:v1 .
</code></pre>

<p>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.</p>

<p>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:</p>

<pre><code>docker tag &lt;some_image_id&gt; myapp-jdk:v1
</code></pre>

CC BY-SA 4.0
2019-02-15T01:30:25.313Z
The developers are looking into better ways to handle this with buildkit. This issue would be good to follow: https://github.com/moby/moby/issues/38197#issuecomment-438348158
BMitch
CC BY-SA 4.0
2018-08-25T11:15:05.580Z
One reason to try to do this in a single command: the build context doesn't need to be pulled together more than one time.
seveibar
CC BY-SA 4.0
2018-08-25T11:23:12.033Z
@seveibar Good point. Buildkit will handle that much better by only sending any changes in the build context, but that's still in experimental right now.
BMitch
CC BY-SA 4.0
2018-07-05T13:35:52.593Z
2019-02-15T13:12:50.213Z
2019-02-15T13:12:50.213Z
BMitch
<p>Thanks to Docker <a href="https://docs.docker.com/develop/develop-images/multistage-build/" rel="noreferrer">multi-stage builds</a>, we can compile some artefacts in one environment and put them for execution in other one.</p>

<p>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.</p>

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

CC BY-SA 4.0
2021-03-20T08:26:16.913Z
Possible, yes, desirable -- f*k no.
Software Engineer
<automation>
Docker tags for multi-stage builds for series of Docker images out of one Dockerfile
Ta Mu
# Docker tags for multi-stage builds for series of Docker images out of one Dockerfile

(Asked by: username_0 on 2018-07-05)

Thanks to Docker [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/), 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?

**Tags:** automation

> **Comments:**
> **username_1**: Possible, yes, desirable -- f*k no.  

---

## 2 answers

### Answer by username_3 on 2018-07-05. Score: 15 | Accepted answer

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

> **Comments:**
> **username_4**: One reason to try to do this in a single command: the build context doesn't need to be pulled together more than one time.  
> **username_3**: @username_4 Good point. Buildkit will handle that much better by only sending any changes in the build context, but that's still in experimental right now.  
> **username_3**: The developers are looking into better ways to handle this with buildkit. This issue would be good to follow: https://github.com/moby/moby/issues/38197#issuecomment-438348158  

---

### Answer by username_2 on 2021-03-20. Score: 2

I stumbled upon a solution which I thought was quite elegant, and only requires you to build once:
Use LABEL feature in your multistage builds, and then after building, you can tag based on label.
Advantages:

only pull together the build context once
fast command to tag after the build has been done

The example was discussed in https://forums.docker.com/t/tag-intermediate-build-stages-multi-stage-build/34795

Using the LABEL command in my dockerfile I added a label to the build
stage I’m interested in, then filter for it later:
FROM node as builder-stage
LABEL builder=true

FROM node as app-stage
LABEL builder=false

Now to filter, sort newest first, cut out the date, and take the top
row:
docker images --filter “label=builder=true” --format ‘{{.CreatedAt}}\t{{.ID}}’ | sort -nr | head -n 1 | cut -f2

Note that it is important to remove the label in the next build stage.

This approach works really well for me


---

