get label value from docker inspect

Larry Cai picture Larry Cai · Apr 6, 2016 · Viewed 22.5k times · Source

I had problem to get the value from the map list due to the key has "." inside.

docker inspect jenkins
...
  Config: {
  ..
      "Labels": {
            "com.docker.compose.config-hash": "85bcf1e0bcd708120185a303e2a8d8e65543c1ec77ec0c6762fc057dc10320aa",
            "com.docker.compose.container-number": "1",
            "com.docker.compose.oneoff": "False",
            "com.docker.compose.project": "new",
            "com.docker.compose.service": "sc2",
            "com.docker.compose.version": "1.5.2"
        }
    }
}

I can get map list

docker inspect -f {{.Config.Labels}} new_sc2_1
map[com.docker.compose.config-hash:85bcf1e0bcd708120185a303e2a8d8e65543c1ec77ec0c6762fc057dc10320aa com.docker.compose.container-number:1 com.docker.compose.oneoff:False com.docker.compose.project:new com.docker.compose.service:sc2 com.docker.compose.version:1.5.2]

But how can I get the project name new from key com.docker.compose.project

docker inspect -f {{.Config.Labels.com.docker.compose.project}} new_sc2_1
<no value>

Answer

thaJeztah picture thaJeztah · Apr 6, 2016

You can use index to get the value of that key (wrapped for readability);

docker inspect \
  --format '{{ index .Config.Labels "com.docker.compose.project"}}' \
  new_sc2_1

That should give you the name of the project