【问题标题】:Multiple binds mounts from external config来自外部配置的多个绑定挂载
【发布时间】:2020-11-15 14:44:48
【问题描述】:

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

由于我无法控制的原因,不同的主机有不同的映射,所以我需要绑定挂载。

是否可以外部化挂载配置,以便我们可以为每个主机动态生成它?

我见过使用 JSON 的 docker-compose 示例,但找不到任何 docker run 等效项(docker-compose 不是我的用例的选项)。

我尝试了以下方法:

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.”,删除空格会抑制错误但没有安装任何内容

【问题讨论】:

    标签: docker external volumes env-file


    【解决方案1】:

    您不能为此使用 --env-file - 这仅适用于环境变量。

    我不知道如何在本地使用 Docker 来做到这一点,但你可以使用 Bash 和 jq 一起破解一些东西。这是一个将您的 JSON 文件转换为一系列挂载选项的命令:

    < 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多