gpt4 book ai didi

json - Docker-如何检查容器的卷路径?

转载 作者:行者123 更新时间:2023-12-02 19:12:35 25 4
gpt4 key购买 nike

因此,从文档中,我可以查询容器的体积:

docker inspect --format="{{.Volumes}}" container

这将以以下格式返回路径:
map[/container/path/1:/host/path/1 /container/path/2:/host/path/2]

我的问题是,如何从返回的数据中提取一条路径?说我只想要 /host/path/2,所以我可以在脚本中使用它来备份该卷中的数据。

最佳答案

--format的参数是标准的go text/template表达式。如果要获取与特定容器路径相对应的主机路径,则可以执行以下操作:

$ docker inspect -f '{{index .Volumes "/container/path/1"}}' container
/host/path/1

如果要生成主机:容器对的列表,可以执行以下操作:
$ docker inspect -f '{{range $key, $value := .Volumes}}{{printf "%s:%s\n" $key $value}}{{end}}'

这将使您:
/container/path/1:/host/path/1
/container/path/2:/host/path/2

在此示例中,我们利用了以下语法:

A pipeline inside an action may initialize a variable to capture the result. The initialization has syntax

$variable := pipeline

where $variable is the name of the variable. An action that declares a variable produces no output.

If a "range" action initializes a variable, the variable is set to the successive elements of the iteration. Also, a "range" may declare two variables, separated by a comma:

range $index, $element := pipeline

in which case $index and $element are set to the successive values of the array/slice index or map key and element, respectively. Note that if there is only one variable, it is assigned the element; this is opposite to the convention in Go range clauses.

关于json - Docker-如何检查容器的卷路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32277171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com