Get the last Docker image built - 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
get the last image name in bash script variable.
docker run ... $last_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')

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

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

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)

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)

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

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

Related

Docker: Filter containers by image, ignoring the tag, then the images

I need to remove a series of open containers which are derived from an image which I have tagged many times so I have plenty of versions around. I also have plenty of containers running (which remained hanging from various tests I did with the various versions of the container).
What are the commands to quickly erase all of them? (I.E based on image name or whatever)
I found that you can do
docker container stop $(docker container ls -aq --filter "ancestor=imagename")
But this doesn't work with the various tagged versions (like imagename:1.0, imagename:2.0)
so to get all of them I had to use
docker container stop $(docker container ls -aq --limit 30)
Which gets the container created up to 1 month ago.
Prune just cleaned a few of them.
I won't list every and all the commands I tried.
Please tell me what is the quickest and cleanest way to clean an image and all the successive tagged versions, and the relative containers which are running or exited related to them.
Thanks
Can't you just use some linux magic? For example
docker ps | grep <image-name> | awk '{ print $1 }' | xargs docker stop
Explanation:
docker ps show running containers
grep <image-name> search for image-name in the previous command output
awk '{ print $1 }' take the first 'column' (in this case the container id)
xargs docker stop feed the ids to docker stop
Similarly, you could just use other bash commands to delete the containers and remove the images:
docker ps -a | grep <image-name> | awk '{ print $1 }' | xargs docker container rm
docker image ls | grep <image-name> | awk '{ print $3 }' | xargs docker rmi
A little warning: grep is not specific to docker and will filter just based on the contents of the string so if your image name appears in the container names or anywhere else, then this method will stop/remove those containers as well!

Shell command to Get container id from "docker ps"

I am basically looking to achieve this 2 steps:
1. Run the docker image:
docker run -p 80:80 some-image-name:25
2. Now "docker ps" returns whole data about the container but I was looking for just the container ID to
3. run some test on it.. ex.
docker exec -it /usr/bin/npm run test
So my question is how I can get just the container id from step 2.
Note: I need this flow for my pipeline script in Jenkins.
docker ps -a -q
This will give you only container's id
You could use awk to get the container ID's as follows:
docker ps | awk 'NR > 1 {print $1}'
This one-liner outputs all the container ID's printed by docker ps. To get only the first one you would use:
docker ps | awk 'NR > 1 {print $1; exit}'
Even though that answers your question I recommend that you use container names instead of relying on container ID's.
P.S.: This solution is on average 1 millisecond slower than docker ps -q, but it is significantly more flexible.
docker ps --format {{.ID}}
Will return only the ids of running containers.
you can use docker functionality to get this done:
docker ps --filter volume=remote-volume --format "table {{.ID}}\t{{.Mounts}}"
with --format "{{.ID}}" you'd get the ids only. You can also filter. Read the documentation of docker ps for more details
All the below command give you container id's
docker ps -aqf "name=containername"
docker ps --no-trunc -aqf name=containername
docker container ls | grep 'container-name' | awk '{print $1}'```
You can get container ID using following command:
docker ps -q

find docker containers created using a docker image

how to find which docker container is using/referencing a particular image?
To give more detail, say I have some 10 docker images and there are some 30 docker containers.. How can find which containers are created using docker image ID XXXXX using a simple command?
You need to dig through the docker history output for other images to see what is linked back. There's an image out there that automates much of this that you can run with the following:
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock \
nate/dockviz images -t -l
More details on the above command can be found on this github repo.
Here you go:
docker ps -a | awk '{ print $1,$2 }' | grep $(docker images | grep *image-id* | awk '{ print $1}')

Is there a way to list all the running docker containers by name?

I'm aware of how to get a list of "container ID's" of all running docker containers.
$ docker ps -q
This should do it. Apologies for the slash at the start.
$ docker inspect -f {{.Name}} $(docker ps -q)
/test
/test2
maybe docker ps | awk 'NR>1 {print $2}' the NR>1 avoids print ID for the first line
I haven't found a solution using docker ps, but you can do it using docker-compose (formerly fig):
docker-compose ps | awk '{print $1}' will return something like this:
Name
-------------------------------------------------------------------------------
src_bus_1
src_db_1
src_images_1
src_nginx_1
src_python_1
docker ps | awk 'NR>1 {print $(NF)}'
does it
NR>1 avoids printing the title row and print $(NF) prints the last column of the output.

Find the docker containers using an image?

If I have the ID of an image how can I find out which containers are using this image? When removing an image that is still used you get an error message:
$ docker rmi 77b0318b76b3
Error response from daemon: Conflict, cannot delete 77b0318b76b3 because the container 21ee2cbc7cec is using it, use -f to force
But how could I find this out in an automated way without trying to remove it?
You can try this:
docker ps -a | grep `docker images | grep IMAGE_ID | awk '{print $1":"$2}'` | awk '{print $1}'
With docker ps -a you get the list of all the containers including
the ones you are interested in.
The problem is that you have the IMAGE NAME there but you need the IMAGE ID.
You can use docker images to get the IMAGE ID for a given IMAGE NAME and that is what you use
in your grep to filter by your IMAGE ID.
Finally you get the first column to show only the CONTAINER IDs.
Example:
docker ps -a \
| grep `docker images | grep 3a041c1b0a05 | awk '{print $1":"$2}'` \
| awk '{print $1}'
Output:
4d6fb8a7149f
2baa726b1aa5
Hope this helps.
This will list all containers using your $IMAGE_ID.
$IMAGE_ID can be 77b0318b76b3 or namespace/repo:tag.
E.g.: $IMAGE_ID="77b0318b76b3"
docker container ls --all --filter=ancestor=$IMAGE_ID --format "{{.ID}}"
f you want to list the images of the active containers docker inspect -f '{{ .Config.Image}}' $(docker ps -q) and for all the containers docker inspect -f '{{ .Config.Image}}' $(docker ps -qa)

Resources