Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last Docker image built

Tags:

docker

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 ...
like image 336
Raymond Chenon Avatar asked Jun 10 '18 19:06

Raymond Chenon


People also ask

Where is the image after docker build?

If you use the default storage driver overlay2, then your Docker images are stored in /var/lib/docker/overlay2 . There, you can find different files that represent read-only layers of a Docker image and a layer on top of it that contains your changes.


1 Answers

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

like image 177
Shiplu Mokaddim Avatar answered Nov 16 '22 02:11

Shiplu Mokaddim