Answers: <p>This will do want I want: killing all containers <em>directly</em> based on <code>mariadb</code> regardless of their tag--and without killing containers having <code>mariadb</code> somewhere higher in their ancestor list.</p>
  
  <pre><code>sudo docker kill $(
    sudo docker ps --format '{{.ID}} {{.Image}}' | awk '$2 ~ /^mariadb:/ { print $1}'
  )
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-29T21:40:53.013Z
Answers: My vote for piping grep and cut where awk is the perfect tool for the task :)
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2019-01-31T06:51:48.880Z
Answers: i was wondering why you did the `--format` as your awk works without it then i realised that it makes things future proof to changes in the default `docker ps` output.
Answers: simbo1905
Answers: CC BY-SA 4.0
Answers: 2019-01-31T12:15:08.770Z
Answers: Exactly @simbo1905. You can't safely rely on the default format: without an explicit format, the output of `docker ps` is not only version-dependant (I assume) but also site-specific since it can be configured system-wide using the `psFormat` configuration option (https://docs.docker.com/engine/reference/commandline/cli/#configuration-files).
Answers: Sylvain Leroux
Answers: CC BY-SA 4.0
Answers: 2019-01-29T17:24:22.493Z
Answers: 2019-01-29T22:28:16.733Z
Answers: 2019-01-29T22:28:16.733Z
Answers: Sylvain Leroux
Body: <p>How to kill all Docker containers based on a given image?</p>
  
  <p>As an example:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I would like to kill all containers based <em>directly</em> on the <code>mariadb</code> image (ideally, both 10.4 and 10.3).</p>
  
  <p>The following solution does not work since it will kill all containers having <code>mariadb:10.3</code> as an ancestor--and not only those directly based on that image:</p>
  
  <pre><code>sudo docker kill $( sudo docker ps --filter ancestor=mariadb:10.3 )
  </code></pre>
  
Tags: <docker>
Title: How to kill all Docker containers based on a given image?
UserDisplayName: Sylvain Leroux
ThreadText: # How to kill all Docker containers based on a given image?
  
  (Asked by: username_0 on 2019-01-29)
  
  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 )
  
  ```
  
  **Tags:** docker
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-01-29. Score: 4 | Accepted answer
  
  This will do want I want: killing all containers *directly* based on `mariadb` regardless of their tag--and without killing containers having `mariadb` somewhere higher in their ancestor list.
  
  ```
  sudo docker kill $(
    sudo docker ps --format '{{.ID}} {{.Image}}' | awk '$2 ~ /^mariadb:/ { print $1}'
  )
  
  ```
  
  > **Comments:**
  > **username_1**: My vote for piping grep and cut where awk is the perfect tool for the task :)  
  > **username_2**: i was wondering why you did the `--format` as your awk works without it then i realised that it makes things future proof to changes in the default `docker ps` output.  
  > **username_0**: Exactly @username_2. You can't safely rely on the default format: without an explicit format, the output of `docker ps` is not only version-dependant (I assume) but also site-specific since it can be configured system-wide using the `psFormat` configuration option (https://docs.docker.com/engine/reference/commandline/cli/#configuration-files).  
  
  ---
  
  