无法将变量传递到Docker-compose中的volumes部分

dced5bon  于 2023-01-08  发布在  Docker
关注(0)|答案(1)|浏览(141)

我有一个docker-compose.yml文件,它定义了如下的volumes部分:

volumes:
  seqfs:
    driver: azure_file
    driver_opts:
      share_name: seqtest
      storage_account_name: stacctest
      storage_account_key: ${STORAGE_ACCOUNT_KEY}

我尝试在构建命令期间传入STORAGE_ACCOUNT_KEY:

docker-compose -f docker-compose.yml build --build-arg STORAGE_ACCOUNT_KEY="##########"

但返回错误:

未设置STORAGE_ACCOUNT_KEY变量。默认为空字符串。

请注意,出于安全原因,我不想将STORAGE_ACCOUNT_KEY保存到.env之类的文件中--我想从命令行传递它。
我如何将一个参数传递给我的docker-compose.yml中的volumes部分?

cngwdvgl

cngwdvgl1#

关于您收到的错误:

docker-compose -f docker-compose.yml build --build-arg STORAGE_ACCOUNT_KEY="##########"

But an error is returned:

The STORAGE_ACCOUNT_KEY variable is not set. Defaulting to a blank string.
  • 事实上,--build-arg只处理Dockerfile中的**ARG**指令。
  • (顺便说一句,运行docker-compose up --build更简单

而不是运行docker-compose build *,然后运行 * docker-compose up

  • 我能想到的唯一解决办法就是:

1.将STORAGE_ACCOUNT_KEY=foobar放入.env文件
并运行docker-compose up --build
1.或将STORAGE_ACCOUNT_KEY=foobar放入other.env文件中
并运行docker-compose --env-file=other.env up --build
(**注意:**docer/compose语法包含两种不同的"env-file",有关这一微妙之处的详细信息,请参见this answer of mine: Pass variables from .env file to dockerfile through docker-compose。)
1.或运行STORAGE_ACCOUNT_KEY=foobar docker-compose up --build

结论:

您说您 * 出于安全原因 * 不想将STORAGE_ACCOUNT_KEY保存到.env之类的文件中,因此乍看起来您只会接受上面的解决方案3。
但是,请注意,如果您不信任Docker主机的环境,则3.不太可能更安全。实际上,.env文件可能会受到文件权限(和.gitignore FWIW)的保护,而上面的foobar字符串会自动泄漏正在运行的进程的名称(例如ps aux | grep STORAGE_ACCOUNT_KEY)。

相关问题