I’m interested in the ability to tag an intermediate build stage, and looking for suggestions.
I have 3 stages in my build:
FROM node, Install prod node_modules
FROM stage 1, install dev node modules including grunt/bower/etc and generate my static js and html files.
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.
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.
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.
# Phase 1: Build the source code
FROM nginx:latest AS build
WORKDIR /src
RUN echo "Build completed" > build.txt
# Phase 2: Run unit tests
FROM build as testrunner
WORKDIR /testresults
RUN echo "Placeholder" > placeholder.txt
RUN echo "Unit test completed" > testresults.txt
LABEL builder=true
# Phase 3: Publish the binary
FROM build as publish
WORKDIR /publish
COPY --from=build /src/build.txt .
RUN echo "Binary publish completed" > publish.txt
# Phase 4: Build the final docker image
FROM nginx:latest AS base
WORKDIR /app
COPY --from=publish /publish .
COPY --from=testrunner /testresults/placeholder.txt .
I build it with docker build -t testimage:dev ., after the build, I use docker images --filter "label=builder=true" to retrieve the intermediate image, the result is empty.
I’m running Docker Desktop for Mac on a Mac Mini M1, the version is 4.23.0 (120376).