How to kill all Docker containers based on a given image?
As an example:
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58251f04892a mariadb:10.4 "docker-entrypoint.s…" 53 seconds ago Up 52 seconds 3306/tcp optimistic_ramanujan
2a08ee7d9afe mariadb:10.3 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp thirsty_taussig
65abd7a3b3e1 mariadb:10.3 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp angry_lederberg
32076aa4745e mariadb:10.3 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp distracted_kapitsa
I would like to kill all containers based directly on the mariadb image (ideally, both 10.4 and 10.3).
The following solution does not work since it will kill all containers having mariadb:10.3 as an ancestor--and not only those directly based on that image:
sudo docker kill $( sudo docker ps --filter ancestor=mariadb:10.3 )
--formatas your awk works without it then i realised that it makes things future proof to changes in the defaultdocker psoutput. – simbo1905 Jan 31 '19 at 06:51docker psis not only version-dependant (I assume) but also site-specific since it can be configured system-wide using thepsFormatconfiguration option (https://docs.docker.com/engine/reference/commandline/cli/#configuration-files). – Sylvain Leroux Jan 31 '19 at 12:15