Docker How to set and unset environment variables in Linux

When you are building docker you can have “ENV” in dockerfile that sets the environment in the linux. In some case, you want to set the environment in dockerfile and unset it as it is not necessary or as it is a policy violation. For example, you may need to set http proxy for building the docker and then you will need to unset the proxy while it is running as there should not be anything that is going to internet.

There is not “UNSET” command in docker. You cannot source a environment file to unset the environments.

One thing is to set the ENV to empty string – this is a poor and quick solution.

One other solution is to use Docker compose to set the environment but this you should use it if you have proper infrastructure setup in your DEVOPS platform to be able to get the proper Docker compose file for the proper environment.

Right solution is to use linux “source” to unset the environment. You can use unset command to unset environment.

After much experiment, I settle on the following solution.

You source the environment file that does unset the environment variable as the ENTRYPOINT in the docker. Following is a sample code in the dockerfile.

ENTRYPOINT ["bash", "-c", "source /dir/ && \"$@\"","-s"]

Running the docker interactively with Entry point as follows:

docker run -t -i image /bin/bash