Useful Docker Commands That I Often Use in My DevOps Practice

Andrey Byhalenko
DevOps Manuals and Technical Notes
3 min readJan 7, 2024

In this short article, I collected Docker commands that I often use in my DevOps practice.

Containers

  • Stop and remove all containers
docker stop $(docker ps -a -q) #stop ALL containers
docker rm -f $(docker ps -a -q) # remove ALL containers
docker rm $(docker ps -q -f status=exited) # remove all exited containers
docker rm -f $(sudo docker ps --before="container_id_here" -q) # can also filter
  • Exec into container
docker exec -it $(docker container ls | grep '<seach_term>' | awk '{print $1}') sh
  • Check container environment variables
docker exec container-name(id) env

Images

  • List images
docker images | grep "search_term_here"
  • Remove image(s) (must remove associated containers first)
docker rmi -f image_id_here # remove image(s)
docker rmi -f $(docker images -q) # remove ALL images!!!
docker rmi -f $(docker images | grep "^<none>" | awk '{print $3}') # remove all <none> images
docker rmi -f $(docker images | grep 'search_term_here' | awk '{print $1}') # i.e. 2 days ago
docker rmi -f $(docker images | grep 'search_1|search_2' | awk '{print $1}')
  • Remove all unused docker images, including child images
docker rmi $(docker images -q) -f

Delete Both Images and Containers

  • Show the list of all images and all containers
docker images && docker ps -a
  • Stop and remove containers and associated images with common grep search term
docker ps -a --no-trunc | grep "search_term_here" | awk "{print $1}" | xargs -r --no-run-if-empty docker stop &&
docker ps -a --no-trunc | grep "search_term_here" | awk "{print $1}" | xargs -r --no-run-if-empty docker rm &&
docker images --no-trunc | grep "search_term_here" | awk "{print $3}" | xargs -r --no-run-if-empty docker rmi
  • Stop only exited containers and delete only non-tagged images
1 docker ps --filter 'status=Exited' -a | xargs docker stop docker images --filter "dangling=true" -q | xargs docker rmi

Delete Networks and Volumes

  • Find all the dangling volumes (those not attached to any containers), and then remove them
docker volume rm $(docker volume ls -qf dangling=true)
  • Clean up orphaned networks
docker network rm $(docker network ls -q)

Send Any Text to Docker Container Output Log

#from the docker container run the following
echo "test log1" >> /proc/1/fd/1

Docker System

Usage: docker system COMMAND

Manage Docker

Commands:
df Show docker disk usage
events Get real time events from the server
info Display system-wide information
prune Remove unused data

Run 'docker system COMMAND --help' for more information on a command.

Clean Up Your Docker Environment by Removing Unused Docker Objects

docker system prune -a

docker system prune: This is the base command used for cleaning up unused Docker objects. By default, it removes:

  • All stopped containers.
  • All networks not used by at least one container.
  • All dangling images. (Dangling images are images that are not tagged and are not referenced by any container.)
  • All build cache.

-a or --all flag: When you add this flag to the command, it extends the cleanup operation. Along with the items cleaned up by the default docker system prune, this flag also includes:

  • All images not referenced by any container, not just the dangling ones. This means that even saved or tagged images that are not currently in use by a container will be removed.

This command is especially useful for freeing up disk space by removing unused Docker objects.

However, it’s important to use it with caution, as it can lead to the removal of images that you might want to keep.

It’s always a good idea to double-check before executing such cleanup commands, especially in a production environment.

If you liked my articles, join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.

As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.

Subscribe here: https://junior-devops-hub.ck.page

--

--

Andrey Byhalenko
DevOps Manuals and Technical Notes

I'm a DevOps Engineer, Photography Enthusiast, and Traveler. I write articles aimed at junior DevOps engineers and those aspiring to become DevOps engineers.