Languages
[Edit]
EN

Docker remove all containers except one

8 points
Created by:
Jun-L
873

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.
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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