Cómo eliminar imágenes docker basado en el nombre?


Quiero eliminar todas las versiones de imágenes de docker con nombres que contengan una cadena dada (imagename).
He intentado lo siguiente, pero no parece funcionar:

docker images | grep 'imagename' | xargs -I {} docker rmi

 34
Author: RJFalconer, 2016-10-17

9 answers

Intente lo siguiente:

docker rmi $(docker images |grep 'imagename')
 28
Author: Rambler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-17 12:33:43

Versión ligeramente más exacta-greping solo en el nombre del repositorio:

docker rmi $(docker images --format '{{.Repository}}' | grep 'imagename')
 27
Author: Elton Stoneman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-17 12:33:37
docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' | grep 'imagename')
 12
Author: Avamore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-28 18:42:07

Simplemente puede agregar --force al final del comando. Como:

sudo docker rmi <docker_image_id> --force

Para hacerlo más inteligente puede detener cualquier contenedor en ejecución antes de eliminar la imagen:

sudo docker stop $(docker ps | grep <your_container_name> | awk '{print $1}')

sudo docker rm $(docker ps | grep <your_container_name> | awk '{print $1}')

sudo docker rmi $(docker images | grep <your_image_name> | awk '{print $3}') --force

En docker ps, $1 es la 1a columna, es decir, id de contenedor de docker y $3 es la 3a columna, es decir, docker id de imagen

 4
Author: kushalbhattad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-11 01:58:26

También obtuve otra respuesta concisa. El único cambio fue eliminar la bandera -I {} innecesaria.

docker images | grep 'imagename' | xargs docker rmi

 2
Author: Aditya Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-17 12:32:47

docker images en realidad utiliza el primer argumento posicional como un nombre de imagen para filtrar. No se requieren grep y awk. La opción -q devolverá solo los ID de imágenes coincidentes que se pueden alimentar a docker rmi.

docker rmi --force $(docker images -q imagename | uniq)
 2
Author: udondan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-11 12:29:45
docker rmi `docker images | awk '$1 ~ /imageName/ {print $3}'`

Esto eliminará todas las imágenes por nombre "imageName". En algunos casos esto puede dar un error como "la imagen se hace referencia en uno o más repositorios". En este caso usen la fuerza borrar.

docker rmi -f `docker images | awk '$1 ~ /imageName/ {print $3}'`

Otra forma puede ser:

docker images | awk '{ OFS = ":" }$1 ~ /imageName/ {print $1,$2}'
 1
Author: zaman sakib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-18 00:56:08

Por alguna razón no pude usar ninguna de las respuestas dadas aquí. Esto es lo que funcionó para mí.

docker images | grep \"gcr.io/kubernetes-learn-197422/last-week-weather-service\" | awk '{print $3}' | xargs docker rmi

awk '{print $3}' es una parte importante. Extrae id de la 3a columna.

 1
Author: sergeyz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-13 00:23:25

Por ejemplo, el nombre de tu imagen es python_image.

Entonces tu código debe ser :

sudo docker rmi $(sudo docker images --filter=reference='python_image' --format "{{.ID}}")

Espero que esto ayude.

 0
Author: Reamon C. Sumapig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-17 09:59:41