find docker containers created using a docker image - docker

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

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!

How to docker remove all containers based on image name

My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"

Can I make docker do a pull when performing a run?

We regularly push new versions of our containers to our private repository. We also have a set of containers we start when we need them like so:
docker run -e "env=val" -p 9001:80 --name blah --rm our_repo/the_image:latest
The thing we run into is when we push a new version of the_image:latest to our repository, our machines will already have a the_image:latest cached locally and it seems that the run command does not perform a pull of the image.
Is there a way to make it do that other than always having to manually issue a docker pull our_repo/the_image:latest command?
Thanks in advance
docker run --pull=always
is merged here github
will ship as part of Docker 19.09 but you can download nightly
builds with that change
commit
As mentioned by #Linpy you can try the nightly builds, but if you do not want to update you can try the below command. It will pull the image on every run.
docker run -it $(docker pull alpine | grep Status | awk 'NF>1{print $NF}')
Or
docker run -e "env=val" -p 9001:80 --name blah --rm $(docker pull our_repo/the_image:latest | grep Status | awk 'NF>1{print $NF}')
You can also use AWK with out grep
docker run -it $(docker pull alpine | awk 'END{print}' | awk 'NF>1{print $NF}')
Bash Script
#!/bin/bash
image_name="${1}"
docker run -it $(docker pull $image_name | awk 'END{print}' | awk 'NF>1{print $NF}')
$ ./test.sh alpine

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

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

Resources