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
Not directly a dockerfile command, but you can use
unset
to get rid of the environment variable.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 ownregctl 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:
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.