2

I have created a multi-stage build.

  • Stage 1: use node:16.10-alpine to compile sources
  • Stage 2: use an alpine nginx image, copy build folder from first stage, move configuration, expose port, add entrypoint.

Here's the Dockerfile:

# Builder
FROM node:10.16-alpine AS builder

... # Do stuff

# Release
FROM nginx:1.16.0-alpine

COPY --from=builder /usr/src/app/build /usr/share/nginx/html

... # Do stuff

ENTRYPOINT [ ... ]

Pretty straight-forward, I'd say. I know that one you run docker build -t website:latest . it will create an intermediate image <none>, which is that builder image, which might be used later, if there were no changes to sources or package.json.

So, my thought was to tag the builder step something like this: docker build --target builder -t website-builder . and so that even if the sources change, it will just use the same tag and not (please correct me if I'm wrong) create other images.

If I run build with --target flag first and run build again without it doesn't use the builder.

To summarize:

  1. docker build --target builder -t website-builder . First Step
  2. docker build -t website:latest . Second Step

New <none> image was created which is the same as website-builder.

How can I achieve that the builder image is used by the 'Release' stage and rewritten, if changed? If that can't be done, what's the best-practice in this case? Is there one?

Thanks for sticking through. I appreciate any feedback or advice.

Filipp Sher
  • 128
  • 7

1 Answers1

2

I think you may use:

COPY --from=website-builder:latest .....
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • Oh! This did work! Thank you so much! With this `--from` I'll need a script to build the builder image first and then the actual build. Doesn't this break the idea of a multi-stage build or I'm having a wrong idea about it? – Filipp Sher Oct 24 '19 at 08:26
  • IMHO you need to remove the website-builder from your docker file and just use the copy statement in your release dockerfile – LinPy Oct 24 '19 at 08:27
  • So basically have 2 Dockerfiles, one for the builder and one for the release. Right? The only problem is that the builder image is used _only_ for release. Development happens with a dev server outside docker. – Filipp Sher Oct 24 '19 at 08:45
  • Thank you, I really appreciate it. Could you, please, share when is it worth using multi-stage builds then? – Filipp Sher Oct 24 '19 at 08:50
  • it all mentioned here :) https://docs.docker.com/develop/develop-images/multistage-build/ – LinPy Oct 24 '19 at 08:52
  • Great, thank you! Sorry for picking your brain so much. Have a great day. – Filipp Sher Oct 24 '19 at 08:52