EN
Docker remove all containers except one
8
points
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 lsprints all containers,grep -v "docker_container_ID_here"prints the lines that don't, matchdocker_container_ID_here,cut -f 1 -d ' 'prints only first column,docker rm $(...)removes all containers.