Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'docker images' output as json using --format

Tags:

json

docker

I am having some difficulty understanding how to make use of docker's --format option.

For example, if I run 'docker images' i get the following:

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
repo1               305                 123456678676        4 hours ago         500MB
repo1               latest              123431241245        4 hours ago         500MB
repo2               305                 135151251531        4 hours ago         2.39GB

I would like to get results for the images of 'repo1', in JSON format. I found the following page: https://docs.docker.com/config/formatting/ . For the 'json' example, it mentions 'go formatting' is used, however the link provided I am having a hard time making the connection. And the json example on the page is only for a single column. I'm having difficulty figuring out how to get all of the columns, but only for certain repo images.

Also - does anyone know if this is backwards compatible? I will need it to work on older versions of docker, so if it is only available on newer version, maybe it's best to parse the output myself. I can not use docker APIs.

like image 694
ffConundrums Avatar asked May 11 '18 04:05

ffConundrums


People also ask

What format does docker uses to export images?

The OCI format is a specification for container images based on the Docker Image Manifest Version 2, Schema 2 format. Container Registry supports pushing and pulling OCI images.

What is the default format of docker inspect output?

By default, docker inspect will render results in a JSON array.

What is a docker image format?

A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template. Docker images also act as the starting point when using Docker. An image is comparable to a snapshot in virtual machine (VM) environments.


1 Answers

The docker images command can limit images to a specific repository. This page also shows formatting tips.

Eg,

docker images repo1 --format "{{json . }}"

Note the format is evaluated once per image, not as a collection of images.

For completeness, see Go's template formatting syntax.

This style of formatting has been in place for a while, but you'd probably want to double check past versions of docker behave as you expect.


See also format docs.

like image 70
Mark Avatar answered Oct 20 '22 13:10

Mark