Languages
[Edit]
EN

Docker remove all containers except one

5 points
Created by:
AnnLen
13570

In this article we whould like to show how to delete all docker containers except one.

Quick solution:

docker rm $(docker ps -a | grep -v "docker_container_ID_here" | awk 'NR>1 {print $1}')

Example:

docker rm $(docker ps -a | grep -v "a684a1b7dbef" | awk 'NR>1 {print $1}')

To get docker container ID we can run command:

docker ps -a

Alternative solution

The alternative solution bases on cut that cuts the first column from ps -> grep result.

docker container rm $(docker container ls | grep -v "docker_container_ID_here" | cut -f 1 -d ' ')

How it works:

  • docker container ls prints all containers,
  • grep -v "docker_container_ID_here" prints the lines that don't, match docker_container_ID_here,
  • cut -f 1 -d ' ' prints only first column,
  • docker rm $(...) removes all containers.

Docker - useful wiki posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join