How to filter docker process based on image

DockerFiltering

Docker Problem Overview


I have been trying to get the container id of docker instance using docker process command, but when i'm trying with filter by name it works fine for me.

sudo -S docker ps -q --filter="name=romantic_rosalind"

> Results container id : > > 3c7e865f1dfb

But when i filter using image i'm getting all the instance container ids :

sudo -S docker ps -q  --filter="image=docker-mariadb:1.0.1"

> Results Container ids : > > 5570dc09b581 > > 3c7e865f1dfb

But i wish to get only container id of mariadb.

How to get container id of docker process using filter as image ?

Docker Solutions


Solution 1 - Docker

Use "ancestor" instead of "image" that works great. Example:

sudo -S docker ps -q  --filter ancestor=docker-mariadb:1.0.1

The Docker team may have added it in the last versions: http://docs.docker.com/engine/reference/commandline/ps/

Solution 2 - Docker

You can use awk and grep to filter specified container id. For example:

docker ps | grep "docker-mariadb:1.0.1" | awk '{ print $1 }'

This will print id of your container.

Solution 3 - Docker

docker ps -a | awk '{ print $1,$2 }' | grep imagename | awk '{print $1 }'

This is perfect. if you need you can add a filter of running images of a particular stsatus alone, like below

docker ps -a --filter=running | awk '{ print $1,$2 }' | grep rulsoftreg:5000/mypayroll/cisprocessing-printdocsnotifyconsumer:latest | awk '{print $1 }'

Various other filter options can be explored here

https://docs.docker.com/v1.11/engine/reference/commandline/ps/

Solution 4 - Docker

With a command docker container ls for listing containers( which is a replacement for docker ps) solution would be:

docker container ls | grep "docker-mariadb:1.0.1" | awk '{ print $1 }'

you may also use * sign(if needed) like this:

docker container ls | grep "docker-mariadb:*" | awk '{ print $1 }'

See https://docs.docker.com/engine/reference/commandline/container_ls/

Solution 5 - Docker

Example to command >>> docker container ls -af 'name=mysql' --format '{{.ID}}'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPriya DharshiniView Question on Stackoverflow
Solution 1 - DockerRuben Sancho RamosView Answer on Stackoverflow
Solution 2 - DockerwslView Answer on Stackoverflow
Solution 3 - DockerMohammed RafeeqView Answer on Stackoverflow
Solution 4 - DockerheroinView Answer on Stackoverflow
Solution 5 - DockerJoao Pedro Leite S LisboaView Answer on Stackoverflow