使用Docker更新容器的环境变量 [英] Updating Environment Variables of a Container using Docker

查看:3560
本文介绍了使用Docker更新容器的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了带有多个Docker容器的服务器,可以通过 jwilders nginx反向代理访问.运行容器时,可以设置VIRTUAL_HOST环境变量.我一直在尝试找出在容器启动后更新这些内容的方法.

I've setup a server with multiple docker containers, accessible with jwilders nginx reversre proxy. When you run the containers you can set the VIRTUAL_HOST environment variable. I've been trying to figure out a way of updating these after a container was launched.

此处发布的解决方案:

您只需停止docker守护程序并在其中更改容器配置

You just stop docker daemon and change container config in

/var/lib/docker/containers/[container-id]/config.json

要求您停止docker守护进程,但我希望不必诉诸于此.

Requires you to stop the docker daemon, but I would prefer not to have to resort to that.

另一个此处,使用docker commit来保存实例信息:

Another here, uses docker commit to preserve the instance information:

话虽如此,您可以保留文件系统中的更改 容器,方法是将其提交为新图像;

Having said that, you -can- preserve filesystem changes in the container, by committing it as a new image;

$ docker run -it --name=foobar alpine sh
$ docker commit foobar mynewimage
$ docker rm foobar
$ docker run -it --name=foobar mynewimage sh

尽管对于更改环境变量来说,这似乎也有点过头.

Though this also seems to be a bit over the top for just changing an environment variable.

我查看了 docker更新,但这主要是为了重新配置容器资源.

I've looked in docker update, but that is mainly for reconfiguring container resources.

当然,如果没有其他选择,我将使用上面的两种方法,但是我想知道是否有人找到了其他解决方案?

Of course, if I have no other choice I will use either of the methods above, but I'm wondering if anyone has found some other solution?

推荐答案

销毁您的容器,并使用docker run -e ...使用新的环境变量启动一个新容器.这与在正在运行的进程中更改环境变量相同,您将其停止并使用传入的新值重新启动.用销毁并重新创建新容器代替重新启动进程的概念.

Destroy your container and start a new one up with the new environment variable using docker run -e .... It's identical to changing an environment variable on a running process, you stop it and restart with a new value passed in. Replace the concept of restarting a process with destroying and recreating a new container.

如果您的容器包含无法丢失的文件,则您应该使用卷.容器文件系统的其他内容应该是一次性的或不可变的.

If your container contains files that cannot be lost, then you should be using volumes. The other contents of the container filesystem should be either disposable or immutable.

这篇关于使用Docker更新容器的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆