I am trying to use multi stage builds to reduce the size of the final docker image but I cannot get the ENV and enrypoint over to the final image.
The entrypoint and environment variables are unknown (or rather can change without my knowledge as they are set by a external image) and I need to retain them in the final image.
If your final image uses FROM scratch, then there is no base image. If you copy the files (to get rid of the layers) then of course nothing else than the files will be present in your final image.
This is expected behavior.
Either you add those instructions to your final stage as well, or you need to look for another approach.
You could check if docker build --squash (see: https://docs.docker.com/engine/reference/commandline/build/#squash) helps by merging all new layers to a single layer. I am not sure if this “still” exists, older docker versions listed it as argument in docker build --help.
If this is not what you are looking for, then use a tool that squashes the image to a single layer. As I would need to google for such a tool, I leave it for you to do so.
I am pretty sure the “–squash” options was removed and using external tools is not something I want to do at this point.
I am however a little surprised if there is no “official” way to copy the metadata (ie env, entrypoint and cmd) over to a new image in a multi stage build. I have thought of doing some script hacking and exporting the entrypoint/cmd/env (as you described in your examples) and then manually set them in the final image (ie after the copy from=build) but atm I feel it would unnecessarily introduce complexity in the build chain.