1

I am trying to understand method to overwrite environment variable inside running docker container.

I tried below options as suggested over other SO posts. Every time I set env variable via docker exec -e it shows me env variable as set. On the very next run it disappears.

Command to start docker:

docker run -itd --rm -e VAR1=test_var1 -e VAR2=test_var2  --name "test" phusion/baseimage:18.04-1.0.0

Running docker exec to set env variables

cloud_user@vijaygharge1c:/var/lib/docker$ docker exec -it -e VAR4=test_var4 test env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=de9cf4253ae9
TERM=xterm
VAR1=test_var1
VAR2=test_var2
DEBIAN_FRONTEND=teletype
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_ALL=en_US.UTF-8
VAR4=test_var4
HOME=/root
cloud_user@vijaygharge1c:/var/lib/docker$ docker exec -it -e VAR4=test_var4 test env | grep VAR
VAR1=test_var1
VAR2=test_var2
VAR4=test_var4
cloud_user@vijaygharge1c:/var/lib/docker$ docker exec -it -e VAR3=test_var3 test env | grep VAR
VAR1=test_var1
VAR2=test_var2
VAR3=test_var3
cloud_user@vijaygharge1c:/var/lib/docker$ 

Docker version:

cloud_user@vijaygharge1c:/var/lib/docker$ docker -v
Docker version 19.03.12, build 48a66213fe
cloud_user@vijaygharge1c:/var/lib/docker$ 

3 Answers 3

6

You have to delete and recreate the container. There are many other Docker options (volume mounts, network configuration, the actual image you're running) that can't be changed after the container is created either; deleting and recreating the container is extremely routine and this is a place you need to do it.

In terms of environment variables specifically, a process's environment is set when it's initially created (more correctly, when its predecessor execve(2) it) and after that point the process can setenv(3) its own environment but nothing else can change it – not its parent, not its children, not with root permission. This is a general Unix statement and isn't specific to Docker.

In Docker there are a couple of other places where environment changes aren't visible. The process in a container can change its own environment (it's common to do this in an entrypoint script) but that won't be visible in docker inspect output or in docker exec shells. In your example, you're docker exec a new shell inside an existing container and changing that shell's environment, but that doesn't change the main container process's environment and also won't change the docker inspect output.

Sign up to request clarification or add additional context in comments.

1 Comment

If I understand correctly - once process starts and reads env variables - changing them later during it's lifecycle won't matter. Thanks for elaborate details. Though other responses were informative - I am going to accept this comment as answer.
0

Environmentvariables are scoped within the runtime of your docker container. That means, when you start the container, it will be the var you set at the beginning.

A Container is not persistent, means, when you start an image in a container, it is created by scratch, is has no memory about the last run.

Depending on what you are trying to achieve you need another solution.

1 Comment

Thanks. I am not trying to stop and restart container in this case. I am figuring out how to overwrite env in the currently running container i.e. in between docker run & docker stop/kill.
-1

Think of the environment variables as any other variable exists inside an app running in your container. Would the last value of such a variable persist across docker runs? - No.

If you want to save the modified environment variables across sessions, you will have to use a volume as a persistent storage and store your environment variables to the volume as a file just before exiting the container.

How to create a volume and use it:

$ docker volume create my-vol

$ docker run -itd --rm  --name "test" phusion/baseimage:18.04-1.0.0 --mount source=myvol,target=/app

The rest is the responsibility of your app to access the volume via FS and save the environment variables there. Also the responsibility of your app is to access the volume at the startup and restore the last environment variable values.

2 Comments

Thanks. While I agree env won't persist across docker run - is there any method to overwrite env var on the running container i.e. before stopping / removing & restarting it fresh ? I am trying to understand concept & don't have any use case. For ex. if password of the db admin got changed due to some reasons - do I have stop and start container to reflect new password ?
// there any method to overwrite env var on the running container before stopping ? Yes - you should handle SIGTERM in your application and do your updates there. But what is the point? You will just lose the changes you made soon as the container is terminated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.