1

I have a docker image which has 4 lower layers.

I want to reduce the size of my current image layer using multistage, but this causes a loss of environment, port and cmd config properties across the stages. Is there a way to pass on such config variables across stages in Dockerfile.

Mohit Mutha
  • 2,921
  • 14
  • 25
  • Can you include a more complete example? I'd imagine it to be a little unusual to define `EXPOSE` and `CMD` anywhere other than the final stage of a Dockerfile. – David Maze Dec 03 '19 at 12:08
  • Actually I am worried about environment variables specifically. If somehow I could fetch them from lower layers to final one dynamically. – Aarushi Gupta Dec 06 '19 at 06:45

1 Answers1

4

You can do one of the following

Use a base container and set the environment values there

FROM alpine:latest as base
ARG version_default
ENV version=$version_default

FROM base
RUN echo ${version}

FROM base
RUN echo ${version}

Other way is to use ARGS as below. There is some repetition but it becomes more centralised

ARG version_default=v1

FROM alpine:latest as base1
ARG version_default
ENV version=$version_default
RUN echo ${version}
RUN echo ${version_default}

FROM alpine:latest as base2
ARG version_default
RUN echo ${version_default}

Note examples copied from https://github.com/moby/moby/issues/37345

Mohit Mutha
  • 2,921
  • 14
  • 25