Docker Community Forums

Share and learn in the Docker community.

What are multistage benefits?

I’m thinking of using multistage build.
I have a builder stage and an app stage.
Of course, I would want to use --cache-from so I could build on different docker host machines without changing the builder for no real change.
Because of --cache-from I must push my builder stage as well.
It looks to me that both outputs of building scripts are quite the same, with multistage or without.

  • This is an example for using a multistage build

#!/bin/bash
set -euo pipefail
#Pull images so cache would be used
docker pull myorg/myapp:builder || true
docker pull myorg/myapp:latest || true

#Build builder image
docker build --target builder --cache-from=myorg/myapp:builder --tag myorg/myapp:builder .

#Build production image
docker build --target app --cache-from=myorg/myapp:builder --cache-from=myorg/myapp:latest --tag myorg/myapp:latest .

#Push images
docker push myorg/myapp:builder
docker push myorg/myapp:latest

  • And this is an example for using a different image as builder

#!/bin/bash
set -euo pipefail
#Pull images so cache would be used
docker pull myorg/builder:latest || true
docker pull myorg/myapp:latest || true

#Build builder image
docker build --cache-from=myorg/builder:latest --tag myorg/builder:latest .

#Build production image
docker build --cache-from=myorg/builder:latest --cache-from=myorg/myapp:latest --tag myorg/myapp:latest .

#Push images
docker push myorg/builder:latest
docker push myorg/myapp:latest

It can be said that the second version, where the image is standalone and not part of multistage build. I can easily reuse for other images as well, if needed.
Is this right to say? Or each app needs it own specific builder?

Please let me know your thoughts.