大网数据广告活动
服务器运维问答

Docker Stack Deploy doesn't resolve environment variables with default value like Docker Compose does

· 52CE 我爱测

I have the following example files docker-compose.yml version: '3' services: web: image: webapp:${VARIABLE_A:-${VARIABLE_B}} env.conf VARIABLE_B=123 VARIABLE_A is not set on purpose so it should fall back to VARIABLE_B Docker com…

问题原文

原帖链接 · RabidTunes · CC BY-SA 4.0

I have the following example files

docker-compose.yml

version: '3'
services:
  web:
    image: webapp:${VARIABLE_A:-${VARIABLE_B}}

env.conf

VARIABLE_B=123

VARIABLE_A is not set on purpose so it should fall back to VARIABLE_B

Docker compose is able to resolve the default value of the environment variable, however docker stack deploy is unable to do the same resolution

user@laptop:~$ docker compose --env-file ./env.conf convert

name: dockercomposetest
services:
  web:
    image: webapp:123
    networks:
      default: null
networks:
  default:
    name: dockercomposetest_default
user@laptop:~$ env $(cat ./env.conf | xargs) docker stack deploy --compose-file docker-compose.yml stack

Creating service stack_web
failed to create service stack_web: 
  Error response from daemon: 
  rpc error: 
    code = InvalidArgument desc = ContainerSpec: "webapp:${VARIABLE_B}" is not a valid repository/tag

As you can see when using docker stack deploy it detects ${VARIABLE_A:-${VARIABLE_B}} is an environment variable that, because VARIABLE_A is not set, it should default to ${VARIABLE_B} however it does not resolve its value which is 123

Obviously, webapp does not exist, it's only an example, but the above error output should be this instead

user@laptop:~$ env $(cat ./env.conf | xargs) docker stack deploy --compose-file docker-compose.yml stack

Creating service stack_web
failed to create service stack_web: 
  Error response from daemon: 
  rpc error: 
    code = InvalidArgument desc = ContainerSpec: "webapp:123" is not a valid repository/tag

Why does this happen? Is there any workarounds for this?

已采纳解答

原帖链接 · Simon · CC BY-SA 4.0

For reference see in moby docker stack deploy in 1.13 doesn't load .env file as docker-compose up does. Specifically Comment from kinghuang

"I have my developers use docker-compose config to pre-process Docker Compose projects before passing them to docker stack deploy. You can do this in one line with:

docker stack deploy -c <(docker-compose config) stack-name-here

That way, all Docker Compose features including .env processing are fully applied."

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。