skip to Main Content

How can I remove an environment variable inherited from a base image?

For example, when using:

image "myimage:a":

FROM alpine
ENV hello=world

I can override the variable in the descendant image like this:

image "myimage:b":

FROM myimage:a
ENV hello=none

And I even can make it empty:

FROM myimage:a
ENV hello=

But I want to remove the hello variable completely from myimage:b instead of making it empty, so that it doesnt show up in the GUI for container management software.

Is there a way to do that?

2

Answers


  1. Not directly a dockerfile command, but you can use unset to get rid of the environment variable.

    FROM myimage:a
    RUN unset hello
    
    Login or Signup to reply.
  2. That’s not supported by Docker, there’s a very old open issue: https://github.com/moby/moby/issues/3465

    This could be done by other tools that mutate container images, like Google’s crane mutate or my own regctl image mod command. However neither of these support that feature at the time of this answer. That would make a decent feature request if you wanted to go that direction.

    Otherwise, the best alternative I can think of is to rebuild a fork of image A without the variable set. A worse option would be copying the content from the other image into your own:

    FROM scratch
    COPY --from=myimage:a / /
    

    But then you’d lose all of the settings, like the entrypoint, ports, other environment variables, along with squashing the layers losing any layer reuse and layer history details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search