/ devops

Removing docker containers and images

Docker makes the life of a developer and DevOPS team much easier. However, a couple of handy commands always saves the day.

Listing all the running containers

docker ps

Listing all the containers include the stopped ones.

docker ps -a

Removing a running container

docker stop <DOCKER_ID>
docker rm <DOCKER_ID>

Removing all stopped containers

Linux:

docker rm $(docker ps -q -f status=exited)
docker container prune

Windows:

FOR /f "tokens=*" %i IN ('docker ps -q -f status=exited') DO docker rm %i
docker container prune

Removing all the containers

Linux:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Windows:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

Removing docker images

docker rmi <IMG_NAME>

Removing all docker images

Linux:

docker rmi $(docker images -a)

Windows:

FOR /f "tokens=*" %i IN ('docker images -a') DO docker rmi %i

Removing dangling docker images

Linux:

docker rmi $(docker images -q --filter "dangling=true")

Windows:

FOR /f "tokens=*" %i IN ('docker images -q --filter "dangling=true"') DO docker rmi %i

Pruning Commands

Removing all the stopped containers, volumes, images, networks etc.

docker container prune   # Remove all stopped containers
docker volume prune      # Remove all unused volumes
docker image prune       # Remove unused images
docker system prune      # All of the above, in this order: containers, volumes, images
docker network prune
Love Hasija

Love Hasija

Full Stack Research Engineer, Software Architect | Helped build next generation software systems | Distributed Systems Fanatic | Open Source Hacker.

Read More