Reputation: 5775
I have script that stops containers and then removes them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
But I don't want to remove the docker container with name "my_docker".
How can I remove all containers except this one?
Upvotes: 58
Views: 49451
Reputation: 31
Delete all images but ignore a few of the images, use this:
docker rmi $(docker images -a -q | grep -v 'image 1' | grep -v 'image 2')
Upvotes: 1
Reputation: 1941
Deletes all the images except node
image
sudo docker rmi -f $(sudo docker image ls -a | grep -v "node" | awk 'NR>1 {print $3}')
Upvotes: 3
Reputation: 123
Using docker ps
with --filter name=<my_docker>
docker rm $(docker ps -a -q | grep -v `docker ps -a -q --filter "name=my_docker"`)
Upvotes: 2
Reputation: 902
I know its little late response but it can help any windows user. Here is the command I prepared:
docker rmi $(
docker image list -a --no-trunc --format "table {{.ID}},{{.Repository}}" |
Where-Object {
$_ -NOTMATCH "mcr.microsoft.com/dotnet/core/aspnet" -and
$_ -NOTMATCH "ubuntu" -and
$_ -NOTMATCH "busybox" -and
$_ -NOTMATCH "alpine"
} | Select-Object -Skip 1 | %{ $_.Split(',')[0];}
)
Upvotes: 0
Reputation: 193
Maybe you can start all the containers you don't want to prune. Then, run:
docker container prune -a
Upvotes: -4
Reputation: 63
For those, who want to exclude more than 1 container just add
grep -v "container_name2" |
after the grep -v "container_name1" command. The final command might look like
docker rm $(docker ps -a | grep -v "my_docker1" | grep -v "my_docker2" | cut -d ' ' -f1)
Upvotes: 2
Reputation: 381
I achieved this by the following command:
docker image rm -f $(docker images -a | grep -v "image_repository_name" | awk 'NR>1 {print $1}')
Upvotes: 2
Reputation: 771
To stop
docker stop $(docker ps -a -q | grep -v "my_container_id")
To remove
docker rm $(docker ps -a -q | grep -v "my_container_id")
Upvotes: 0
Reputation: 54457
You can try this, which will
grep -v
), and thenRun this command:
docker rm $(docker ps -a | grep -v "my_docker" | awk 'NR>1 {print $1}')
To use cut
instead of awk
, try this:
docker rm $(docker ps -a | grep -v "my_docker" | cut -d ' ' -f1)
Examples for awk
/cut
usage here: bash: shortest way to get n-th column of output
Upvotes: 67
Reputation: 894
Old question, but I like reviving posts.
For such case you could use Spotify's Docker GC: https://github.com/spotify/docker-gc#excluding-containers-from-garbage-collection
You could do:
echo "my_docker" >> /tmp/docker-gc-exclude-containers
echo '*' > /tmp/docker-gc-exclude
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -v /tmp/docker-gc-exclude-containers:/etc/docker-gc-exclude-containers:ro -v /tmp/docker-gc-exclude:/etc/docker-gc-exclude:ro spotify/docker-gc
(if you would like to get your images cleaned off too, you can avoid mounting the docker-gc-exclude
file)
Upvotes: 0
Reputation: 13334
The title of the question asks for images, not containers. For those stumbling across this question looking to remove all images except one, you can use docker prune along with filter flags:
docker image prune -a --force --filter "label!=image_name"
replacing image_name with the name of your image.
You can also use the "until=" flag to prune your images by date.
Upvotes: 32
Reputation: 16465
This is what's actually happening docker rm $(List of container Ids)
. So it's just a matter of how you can filter the List of Container Ids.
For example: If you are looking to delete all the container but one with a specific container Id, then this docker rm $(docker ps -a -q | grep -v "my_container_id")
will do the trick.
Upvotes: 10
Reputation: 32166
I would prefer to test the container name using something along the lines of (untested)
docker inspect --format '{{ .Name }}' $(docker ps -aq)
this will give the names of the (running or not) containers, and you can filter and
docker rm
using this information
Upvotes: 1