简体   繁体   English

如何在运行 docker 容器中覆盖环境变量

[英]how to overwrite environment variable in running docker container

I am trying to understand method to overwrite environment variable inside running docker container.我试图了解在运行 docker 容器内覆盖环境变量的方法。

I tried below options as suggested over other SO posts.我按照其他 SO 帖子的建议尝试了以下选项。 Every time I set env variable via docker exec -e it shows me env variable as set.每次我通过docker exec -e设置环境变量时,它都会向我显示设置的环境变量。 On the very next run it disappears.在下一次运行时,它消失了。

Command to start docker:启动 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运行 docker exec 来设置环境变量

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: Docker版本:

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

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;还有许多其他 Docker 选项(卷挂载、网络配置、您正在运行的实际映像)在创建容器后也无法更改; 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.具体而言,就环境变量而言,进程的环境在最初创建时设置(更准确地说,是在其前身execve (2) 它时),之后进程可以setenv (3) 它自己的环境,但没有其他任何东西可以改变它——不是它的父母,不是它的孩子,没有root权限。 This is a general Unix statement and isn't specific to Docker.这是一般的 Unix 语句,并不特定于 Docker。

In Docker there are a couple of other places where environment changes aren't visible.在 Docker 中,还有一些其他地方环境变化不可见。 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.容器中的进程可以更改其自己的环境(通常在入口点脚本中执行此操作),但这在docker inspect output 或docker exec外壳中不可见。 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. 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.

Environmentvariables are scoped within the runtime of your docker container.环境变量在 docker 容器的运行时范围内。 That means, when you start the container, it will be the var you set at the beginning.这意味着,当您启动容器时,它将是您在开始时设置的 var。

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. Container 不是持久的,这意味着,当您在容器中启动映像时,它是从头开始创建的,没有关于上次运行的 memory。

Depending on what you are trying to achieve you need another solution.根据您要实现的目标,您需要另一种解决方案。

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?这种变量的最后一个值是否会在 docker 运行中持续存在? - 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. rest 是您的应用程序通过 FS 访问卷并将环境变量保存在那里的责任。 Also the responsibility of your app is to access the volume at the startup and restore the last environment variable values.此外,您的应用程序的职责是在启动时访问卷并恢复最后的环境变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM