Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

docker - In Dockerfile, how to append a value to an environment variable that can exist in base image?

Let's assume we have a Dockerfile like this:

FROM SomeBaseImage:Version

ENV MyVar="MyValue"

In case SomeBaseImage already has a value for MyVar, we usually copy that and set the new value in our Dockerfile. In our case we need to append to it. But this has a drawback:

In case a newer version of SomeBaseImage modifies the value of MyVar, we'll end up with a hard-coded value based on the older version.

Is there a way to access the environment variable from the base image (and then possibly append to it)?

P.S: From my understanding, Arg is not the answer. It allows access to arguments passed by the build command, not environment variables set in the base image.

question from:https://stackoverflow.com/questions/65912376/in-dockerfile-how-to-append-a-value-to-an-environment-variable-that-can-exist-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should have access to and be able to append to ENV variables in Dockerfiles:

ENV MyVar=${MyVar}-modified

Test base Dockerfile, built with: docker build -t base -f Dockerfile.base .

# Dockerfile.base
FROM alpine:3.9
ENV Version=v1.0

Test child Dockerfile built with docker build -t child -f Dockerfile.child .

# Dockerfile.child
FROM base:latest
ENV Version=${Version}-alpha
RUN echo ${Version}

The build output showed that the Version variable was read from the base and added to in the child:

 ...
 => [internal] load build definition from Dockerfile                                                                                
 => [1/2] FROM docker.io/library/base:latest                                                                                    0.0s
 => [2/2] RUN echo v1.0-alpha                                                                                                0.0s
 ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...