Building a multi-stage Dockerfile with --target flag builds all stages instead of just the specified one

As of Docker 18.09 you can use BuildKit. One of the benefits is skipping unused stages, so all you should need to do is build with DOCKER_BUILDKIT=1 docker build -t my-app --target prod .

Reference


In docker build docs it says When building a Dockerfile with multiple build stages, --target can be used to specify an intermediate build stage by name as a final stage for the resulting image. Commands after the target stage will be skipped.

This means, that when you specify --target option, you only specifying the last target which will be built so all before that will be included in the build process. To build multiple environments you probably need to have two Dockerfiles.

Tags:

Docker