Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get label value from docker inspect [duplicate]

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> 
like image 254
Larry Cai Avatar asked Apr 06 '16 01:04

Larry Cai


People also ask

How do I get a docker image label?

To check the labels of a particular Image, you can use the Docker Inspect command. Start the Docker Container. Execute the Inspect Command. Inside the LABELS object, you can find all the labels associated with the image that you have specified inside your Dockerfile.

Can I get source code from docker image?

if you write a compiled application (C, C++, go, …), the source code will not be part of the docker image, and they cannot access source code. But if you are using python, PHP, shell, for sure, if they get your docker image, they can access everything.

What does Ctrl C do in docker?

Starting with docker 0.6. 5, you can add -t to the docker run command, which will attach a pseudo-TTY. Then you can type Control-C to detach from the container without terminating it. If you use -t and -i then Control-C will terminate the container.

Can you inspect a docker image?

Based on the help output, the inspect command can be run on one or more images or containers. Either the name or id can be used to identify an image or container, and by using docker images a list of local images can be found.


2 Answers

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

like image 187
thaJeztah Avatar answered Oct 07 '22 00:10

thaJeztah


You could pipe the output of docker inspect to jq. Given content like this:

...         "Labels": {             "com.docker.compose.config-hash": "a804d541a5828f4aaf17df862b650e58ac5bae77b70ff5ebdb2f0f3682326954",             "com.docker.compose.container-number": "1",             "com.docker.compose.oneoff": "False",             "com.docker.compose.project": "postgis",             "com.docker.compose.service": "postgis",             "com.docker.compose.version": "1.7.0rc1"         } ... 

I can extract an individual label value like this:

docker inspect mycontainer | jq -r '.[0].Config.Labels["com.docker.compose.project"]' 

Which gets me:

postgis 
like image 41
larsks Avatar answered Oct 07 '22 01:10

larsks