14

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.

2 Answers 2

34

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.

Sign up to request clarification or add additional context in comments.

4 Comments

does this flag work for all docker commands that write to stdout or just some?
It depends on the command, I believe. See the examples on the docs.
Thanks Mark. Where is the Go template function json defined. It is not part of the base templating (or, at least, not documented)
The "json" template function is defined by docker cli, probably in templates/templates.go.
1

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;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.