给出以下撰写文件:
version: '3.6'
services:
hello-world:
image: hello-world
environment:
- test:test
如果我使用
docker stack deploy -c docker-compose.yml test
部署它,则按如下方式修改撰写文件:
services:
hello-world:
image: hello-world
environment:
- test:test
- test2:test2
现在,如果我运行
docker service inspect test_hello-world
,我会在任务模板中看到以下内容:
"Env": [
"test:test"
],
是否有一个命令可以用来获取从撰写文件添加的第二个环境变量(test2)?
总之,请更新 docker-compose 后运行以更新服务:
docker stack deploy -c docker-compose.yml test
解释:
请在运行
docker ps -a
后运行docker stack deploy -c docker-compose.yml test
。可以看到容器的状态是Exited (0)
。这意味着你的容器当时没有运行。
使用
test2:test2
更新 docker-compose.yml 并运行 docker service inspect test_hello-world
后,您不会自动更新您的服务。
您应该在 docker stack deploy -c docker-compose.yml test
使用
docker-compose.yml
更改后再次运行
test2:test2
进行更新。然后就可以看到已经更新了。我测试了它以确保它能像那样工作。输出:
"Env": [
"test2:test2",
"test:test"
],
您也可以使用 nginx
而不是
hello-world
进行测试。因为它总是运行(向上)。更新 docker-compose 后,请运行
docker stack deploy -c docker-compose.yml test
进行更新。终端:
Updating service test_hello-world (id: m8le589r3cqap8yg3acega0te)
那时,之前的版本终止了,并创建了新版本。 docker ps -a
显示它。终端(docker ps -a):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10a90caaa627 nginx:latest "/docker-entrypoint.…" 2 seconds ago Created test_hello-world.1.rcrbfttxqw0j22mkw0l149etp
130db6b1d3da nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp test_hello-world.1.hdbj9bjt72sw45obx7h6nga77