Docker Community Forums

Share and learn in the Docker community.

Tag Intermediate Build Stages (multi-stage build)

I’m interested in the ability to tag an intermediate build stage, and looking for suggestions.

I have 3 stages in my build:

  1. FROM node, Install prod node_modules
  2. FROM stage 1, install dev node modules including grunt/bower/etc and generate my static js and html files.
  3. FROM stage 1, copy the output from stage 2 and now I have a slim output container

However for development I prefer to run grunt inside a container - the intermediate grunt container is exactly what I need to execute grunt watch, except it is not given a tag and so would only be available by referencing the hash.

2 Likes

For anyone that is interested in something similar, I’ve found a workaround for now.

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.

3 Likes

The label trick works very well, thanks for sharing!

I didn’t find the need to remove the label on the the second stage of the build, however.

1 Like

You can use the --target keyword to build just a specific stage:

docker build --target builder-stage -t builder-image .

4 Likes

The label trick also works great for me since I am building on a Jenkins machine used by multiple teams within the company, and I wanted to avoid using docker image prune to not accidentally delete something used by other people.

Thanks

Thanks for this!

I used it with these 2 Dockerfiles:

Then I ran:
docker images --filter "label=builder=true"

and got:

And when running docker images I get:

with the following docker build command for synapse :

docker build -t as/anconnector-synapse:master -f docker/Dockerfile .

btw I deleted those images then like this:

docker rmi $(docker images --filter=label=builder=true -q)