ffConundrums
ffConundrums

Reputation: 865

'docker images' output as json using --format

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.

Upvotes: 14

Views: 14261

Answers (2)

NewToPi
NewToPi

Reputation: 313

This is the command that worked for me:

docker images --format=json

This is how to run it in dart programming language:

import 'dart:io';

  ProcessResult result = await Process.run(
    "docker", 
    [
      'images',
      '--format=json',
    ],
    runInShell: true,
  );
  if(result.exitCode == 0){
    var json = result.stdout;
    print(json);
  }else{
    throw result.stderr;
  }

Upvotes: 1

Mark
Mark

Reputation: 7091

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.

Upvotes: 34

Related Questions