1

I am writing a bash script and I am aware that I can get information of the mounts attached to a container using docker inspect --format '{{.Mounts}}' <container-name> which gives a result such as this:

[{volume portainer-data /docker/portainer/data /data local-persist rw true } {bind /var/run/docker.sock /var/run/docker.sock rw true rprivate}]

which is an abbreviated version of the full JSON:

 "Mounts": [
            {
                "Type": "volume",
                "Name": "portainer-data",
                "Source": "/docker/portainer/data",
                "Destination": "/data",
                "Driver": "local-persist",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "bind",
                "Source": "/var/run/docker.sock",
                "Destination": "/var/run/docker.sock",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

How can I get that information so that I could check the Type and then use the Name (or any of the other values)?

I've tried docker inspect --format '{{.Mounts}}{{.Type}}' but it doesn't work however a similar command docker ps --format '{{.ID}}: {{ .Mounts }}' prints the container name and the mounts.

1 Answer 1

1

So, if anything, {{Mounts.Type}} should work. If it doesn't, jq is your friend an can work with JSON documents directly, e.g.,

docker inspect container-name \
  | jq \
  '.[] 
   | {
      "container": .Name,
      "mount": .Mounts.[] 
       | {"source": .Source, "target": .Destination, "type": .Type}
     }
  '

should give you a sequence of JSON objects, each like

{
  "container": "mycoolcontaier",
  "mount": {
    "source": "/home/marcus/data",
    "target": "/data",
    "type": "bind"
  }
}

so that I could check the Type and then use the Name

that sounds like you need some kind of string parsing to do that looking-for-types, and jq can absorb that!

docker inspect container-name \
  | jq \
  '.[] 
   | {
      "container": .Name,
      "mount": .Mounts.[] 
       | {"source": .Source, "target": .Destination, "type": .Type}
     }
   | select(.mount.type == "bind")
  ' 

gives you only the bind-type mounts.

1
  • Running docker inspect --format '{{Mounts.Type}}' container-name gives me back this error: template parsing error: template: :1: function "Mounts" not defined. I did think of jq but was hoping it would be simpler. I will dig into that, thanks. Commented Sep 29 at 12:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.