Docker Community Forums

Share and learn in the Docker community.

Tag Intermediate Build Stages (multi-stage build)


(Kylejw) #1

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.


(Kylejw) #2

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.


(Gaieges) #3

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.


(Cloudhotspot) #4

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

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