How to show comments when filtering for docker images?

Viewed 83

The goal of our team is to have about a dozen of docker images ready for testing purposes with various testing scenarios. In order to ease choosing we directly want to attach information about the scenarios to the images. I have identified adding labels and comments while committing containers as solutions to this problem using docker commit --message/--change. However, now I have the problem that docker does not seem to allow the display of the COMMENT column outside of the docker history command. My desired approach is to use

docker images --filter "label=product_version_full=X.X.X"

in order to list docker images suitable to test our team's version "X.X.X". While that is working fine I do not get enlightened with the comments that I had added previously to the images in the resulting table.

I have already spent some time on the internet looking for the solution but could find it neither here nor elsewhere. Has anyone already come across it?

Best regards!

2 Answers

I think you can use a format and filter the getted result like that by filtring using image name or image tag :

docker images --format "{{.Repository}} {{.Tag}}"

to get the list of images after that ,

docker images --format "{{.Repository}} {{.Tag}}" | grep <your-image-tag>

I hope that my idea can help you to resolve your issue.

Unfortunately docker image listing does not support pretty printing of labels. Ubuntu docker-image-ls man page has the best documentation on the currently supported placeholders.

Docker inspect might be useful but it shows huge amount of information for the image: (see Labels, that is the part you are interested of)

$ docker inspect 1bfebeeed10f
        .....clip
        "Entrypoint": [
            "/bin/sh",
            "-c"
        ],
        "OnBuild": [],
        "Labels": {
            "maintainer": "Softagram <no-reply@softagram.com>"
        ....clip..

Single Label key value can be extracted this way:

$ docker inspect --format='{{index .Config.Labels "maintainer"}}' 1bfebeeed10f

Softagram <no-reply@softagram.com>

The -q argument of docker images makes it good for chaining with xargs:

 docker images -q |xargs docker inspect --format='{{ .Config.Image }} {{.Config.Labels.maintainer}}'

In case you need to get all the labels, at once you may drop ".maintainer" out, so using {{.Config.Labels}} instead.

Combining Python scripts with Docker makes it very easy and maintainable:

>>> import docker
>>> client = docker.from_env()
>>> for image in client.images.list():
...     print(image.labels)
... 
{'maintainer': 'Softagram <no-reply@softagram.com>'}
{'maintainer': 'Softagram <no-reply@softagram.com>'}
{'maintainer': 'Softagram <no-reply@softagram.com>'}
...clip

Using Python will make automation smooth, read more about it here

Related