4

Thanks to Docker multi-stage builds, we can compile some artefacts in one environment and put them for execution in other one.

As result, there are two images, where the final corresponding to the last stage and provided tag gets tagged as defined; there is no tag for intermediate (ones), but they remain there as well.

Question: is it possible to create a series of tagged images for all defined stages using multi stage build out of one Dockerfile?

5

You can build and tag any stage of a multi-stage build. Just use the --target option to docker build. E.g.

docker build --target jdk -t myapp-jdk:v1 .
docker build --target jre -t myapp:v1 .

With the layer caching, the the earlier steps will be reused from the cache and not rerun from scratch each time, so there's little downside to running the build to different targets multiple times.

If you absolutely need to run this from a single build command, you can parse the output of the build, get the image id of the intermediate step, and tag that image id:

docker tag <some_image_id> myapp-jdk:v1
  • One reason to try to do this in a single command: the build context doesn't need to be pulled together more than one time. – seveibar Aug 25 '18 at 11:15
  • 1
    @seveibar Good point. Buildkit will handle that much better by only sending any changes in the build context, but that's still in experimental right now. – BMitch Aug 25 '18 at 11:23
  • The developers are looking into better ways to handle this with buildkit. This issue would be good to follow: github.com/moby/moby/issues/38197#issuecomment-438348158 – BMitch Feb 15 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.