Vitaly Kushner
Vitaly Kushner

Reputation: 9455

how to remove an ENV setting from a docker image

I have a docker image which sets HOME and PATH:

[{
  ...
  "config": {
    "HOME=/",
  }
  ...

I know I can replace it, but is it possible to remove it (and let the normal bash profile settings be used instead). I'd prefer not to hack the shell profile files to override it.

Upvotes: 8

Views: 13928

Answers (2)

creack
creack

Reputation: 121682

You can simply perform a manual commit.

This operation is not available within the Dockerfile, but can be done manually.

When doing docker inspect <image>, you can retrieve the ID of the container that has been used in order to create this image.

You can do then docker commit <container id> <new image name> and all the ENV and other config will get flushed.

If the container has been removed, you can run the image docker run -d <image> <any command>, and then commit the resulting container.

If you want to keep some of the configuration, you can use the docker commit -run '{}' <container id> <new image name> syntax. Cf https://docs.docker.com/engine/reference/commandline/commit/ for more info.

Upvotes: 3

Alex Soto
Alex Soto

Reputation: 6285

I couldn't get @creack's answer to work for me.

In my case I had inadvertently committed some environment variables to an image and I wanted to remove them from the image.

I ended up launching the image and 'unsetting' the environment variable by using -e option with no value for the variable,

For example to unset the environment variable FOO

docker run -it -e FOO IMAGE /bin/bash

Then commit the container

docker commit -m 'removed FOO' CONTAINER_ID IMAGE

Upvotes: 6

Related Questions