代码之家  ›  专栏  ›  技术社区  ›  user1016765

从外部配置装载多个绑定

  •  0
  • user1016765  · 技术社区  · 6 年前

    我正在整理一些需要访问配置的工具,这些工具分布在主机上的不同位置。

    是否可以将装载配置外部化,以便我们可以按主机动态生成装载配置?

    我尝试了以下方法:

    container-env.json:
    
        {
          "mounts" : [
            "type=bind,source=//c/Users/foo/.ssh,target=/root/.ssh,ro",
            "type=bind,source=//c/Users/foo/Projects/ops-tools,target=/root/ops-tools",
            "type=bind,source=//c/tmp/certs,target=/root/certs"
          ]
        }
    
    docker run --env-file container-env.json...
    

    但是docker抱怨说 poorly formatted environment: variable '"mounts" : [' contains whitespaces.

    0 回复  |  直到 6 年前
        1
  •  1
  •   Nick ODell    6 年前

    你不能使用 --env-file 为此-这只是环境变量。

    < container-env.json jq -r '.mounts | map("--mount \"" + . + "\"") | join("\n")'
    

    产生此输出的:

    --mount "type=bind,source=//c/Users/foo/.ssh,target=/root/.ssh,ro"
    --mount "type=bind,source=//c/Users/foo/Projects/ops-tools,target=/root/ops-tools"
    --mount "type=bind,source=//c/tmp/certs,target=/root/certs"
    

    下面是如何在脚本中使用它。

    #!/usr/bin/env bash
    mount_options="$(< container-env.json jq -r '.mounts | map("--mount \"" + . + "\"") | join("\n")')"
    docker run $mount_options <rest of command goes here>