1

So from the docs, I can query my container for its volumes:

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

this returns paths in the format:

map[/container/path/1:/host/path/1 /container/path/2:/host/path/2]

My question is, how do you extract a single path out of this returned data? Say I only wanted the /host/path/2 so i could use it in a script to backup the data in that volume.

1 Answer 1

2

The argument to --format is a standard go text/template expression. If you want to get the host path corresponding to a particular container path, you could do this:

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

If you want to produce a list of host:container pairs, you could do this:

$ docker inspect -f '{{range $key, $value := .Volumes}}{{printf "%s:%s\n" $key $value}}{{end}}'

Which would get you:

/container/path/1:/host/path/1
/container/path/2:/host/path/2

In this example, we're taking advantage of this syntax:

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.