From the docker build docs, you can specify a --target
flag with the name of the stage to build it. Also, the same is specified in the multi-stage build docs.
When you build your image, you don’t necessarily need to build the entire Dockerfile including every stage. You can specify a target build stage. The following command assumes you are using the previous Dockerfile but stops at the stage named builder:
$ docker build --target builder -t alexellis2/href-counter:latest .
I have a single Dockerfile to build my project. It has multiple stages:
# development
FROM node:carbon as development
...
# e2e tests
FROM node:carbon as puppeteer
...
COPY --chown=puppeteer:puppeteer --from=development /app /home/puppeteer
...
# prod build
FROM node:carbon as build
...
# prod image behind webserver
FROM nginx as prod
...
COPY --from=build /app/build /usr/share/nginx/html
...
When I try to build for prod
stage with:
docker build -t my-app --target prod .
I see the logs that it is building all the stages, causing the build to take very long time.
I want it to build just the target I specify (and its stage "dependency"), so that it builds the build
stage and then the prod
stage.
What am I doing wrong or what am I missing?
Note: I am also aware that maybe I have wrong expectations about what the multi-stage build does, but the documentation makes it sounds as if you can build the target you want instead of the whole file so I'll assume that's the case.