skip to Main Content
[banner@stapp03 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              26828d185a8f        5 seconds ago       125MB
ubuntu              latest              174c8c134b2a        3 weeks ago         77.8MB

there are 2 images – i want to type command in cli and grab latest image id (first row) and use it for other subsequent commands.
Not use mouse click to select image id, how to do this?

2

Answers


  1. We can define the format of the output for every docker command through --format <fmt-string>. Details are explained on the corresponding docs.docker.com page.

    If we want the ids of all images, we can run

    $ docker image ls --format {{.Id}}
    

    If we want the ids of, for example, only the ubuntu image(s), we can run

    $ docker image ls ubuntu --format {{.Id}}
    

    If we want to store the output of the command in a bash array, we can use

    $ mapfile -t foo < <(docker image ls --format {{.Id}})
    

    For example, a subsequent

    $ echo "${#foo[@]}"
    

    will show us the size of the array.

    Login or Signup to reply.
  2. If you want to print image id of the first image then you may use:

    docker images | awk 'NR == 2 {print $3}'
    

    Here:

    • NR == 2: Selects 2nd row
    • {print $3}: Prints 3rd field i.e. Image Id
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search