Multi stage build retain environment and entrypoint


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.

The specific project is that I crafting a specialized image on-top of GitHub - jupyter/docker-stacks: Ready-to-run Docker images containing Jupyter applications images. The Docker file I am currently using is here : notebooks/datascience-notebook/Dockerfile · master · csma / jupyterhub · GitLab

Do I miss some obvious or is it not possible to retain the complete set of env variables and entrypoint from the base image?

Did you compare entrypoint, cmd and envs from the base image with your custom image?

docker pull jupyter/datascience-notebook:hub-3.1.1
docker image inspect jupyter/datascience-notebook:hub-3.1.1 --format '{{json .Config.Entrypoint }}'
docker image inspect jupyter/datascience-notebook:hub-3.1.1 --format '{{json .Config.Cmd }}'
docker image inspect jupyter/datascience-notebook:hub-3.1.1 --format '{{json .Config.Env }}'

Then do the same for your custom image.

Note: If jq is installed in your system, you can add | jq to the end of the commands for better readability.


Yes, I compared and the the env and entrypoint is empty and I guess that is expected as I do

FROM scratch
COPY --from=build / /

do note that in the linked repository I do as a workaround
FROM build

And that retain the env and entrypoint but then cannot really accomplish the wanted size reduction and I am back to a single staged build.


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: 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.


Thanks for the help.

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.