how to remove an ENV setting from a docker image

2019-06-18 15:55发布

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.

标签: docker
2条回答
再贱就再见
2楼-- · 2019-06-18 16:24

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 http://docs.docker.io/en/latest/commandline/command/commit/ for more info.

查看更多
smile是对你的礼貌
3楼-- · 2019-06-18 16:31

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

查看更多
登录 后发表回答