Get the last Docker image built

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


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')

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)

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".

Tags:

Docker