Raymond Chenon
Raymond Chenon

Reputation: 12732

Get the last Docker image built

This command gives a list of image and container IDs ordered from top to bottom by last build time:

docker images

All my docker images are appended with the hash git head commit.

The results

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
username/play-table-of-contents-1474f94    latest              6141b8177c2f        34 minutes ago      149MB
username/play-table-of-contents-2616f5f    latest              2b5422dd91ba        About an hour ago   149MB

Is there a way to get only the last image by name ? ( ie: case 6141b8177c2f )

I tried with

docker images --format "{{.Names}}"

My end goal is to run the last docker image built. To do this, I need to

  1. get the last image name in bash script variable.
  2. docker run ... $last_image ...

Upvotes: 27

Views: 26632

Answers (7)

Adán Escobar
Adán Escobar

Reputation: 4793

if you need get last build from an specific image name.. you can do:

IMG_NAME="my-image-name"
IMG_LAST_BUILD=$(docker images | grep $IMG_NAME | awk 'NR==1{printf("%s\:%s",$1,$2)}')

echo $IMG_LAST_BUILD
#my-image-name:tag_version

Upvotes: 0

Thomas
Thomas

Reputation: 9

If you want to enter the last docker image you ran :

docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') /bin/sh

OR

docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') bash

Upvotes: 0

Ash Singh
Ash Singh

Reputation: 4857

This returns the IMAGEID of the latest built docker image:

docker images -q --format='{{.ID}}' | head -1

You can even collect it in a variable and use it as you like:

IMAGE_ID=$(docker images -q --format='{{.ID}}' | head -1)

Upvotes: 4

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

All the other answers' solution relies on the fact docker image sorts the output by date already. This may not be true. A consistent solution would be to sort them by the creation date and get the latest one. I used the following command, this is consistent.

docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}'

This command sorts the output of the docker images command by CreatedAt column and print the id of the latest image

Upvotes: 12

Tolga
Tolga

Reputation: 3715

Powershell

docker images --format "{{.ID}}" | select -first 1

example use with docker run:

docker run -it (docker images --format "{{.ID}}" | select -first 1)

Bash

docker images --format='{{.ID}}' | head -1

example use with docker run:

docker run -it $(docker images --format='{{.ID}}' | head -1)

Upvotes: 4

Arif A.
Arif A.

Reputation: 993

Docker command docker images list out most recently created images.

The following command list out the first image from the above list. I believe you are looking for this command.

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

You would probably deploy a container of the image from above command

docker run $(docker images | awk '{print $1}' | awk 'NR==2')

Upvotes: 22

6be709c0
6be709c0

Reputation: 8461

Short Answer

docker run ... $(docker ps -a --format "{{.Names}}" | head -1) ...

docker ps -a return the stopped and running containers in the order "Last to First".

Upvotes: 5

Related Questions