If you like immudb, give us a star on Github!

extremely useful docker commands

No matter if you're just getting started with docker or you're already a professional, there are situations when you can't remember the correct command or you're just confused by the order of parameters.

Therefore we decided to create our own docker commands cheat sheet and share it with you.

Whenever you build your own container or you want to timestamp the container you're using and worked with your applications - check the free timestamping service: Timestamp service for code and container

Top 16 docker commands

  1. docker ps  list running containers. 
  2. docker ps -a list all container including stopped container
  3. docker pull  download a image from Docker Hub registry. Link to the docker image is always shown on the right at dockerhub.
  4. docker build  is used to build your own container based on a Dockerfile. Common use is docker build . to build a container based on the Dockerfile in the current directory (the dot). docker build -t "myimage:latest" . creates a container and stores the image under the given name
  5. docker images or docker image ls shows all local storage images
  6. docker run  Run a docker container based on an image, i. e. docker run myimage -it bash. If no local image can be found docker run automatically tries to download the image from Docker hub.
  7. docker logs display the logs of a container, you specified. To continue showing log updates just use docker logs -f mycontainer
  8. docker volume ls  lists the volumes, which are commonly used for persisting data of Docker containers.
  9. docker network ls - list all networks available for docker container
  10. docker network connect adds the container to the given container network. That enables container communication by simple container name instead of IP.
  11. docker rm   removes one or more containers. docker rm mycontainer, but make sure the container is not running
  12. docker rmi  removes one or more images. docker rmi myimage, but make sure no running container is based on that image
  13. docker stop   stops one or more containers. docker stop mycontainer stops one container, while docker stop $(docker ps -a -q) stops all running containers. 
  14. docker start - starts a stopped container using the last state
  15. docker update --restart=no updates container policies, that is especially helpful when your container is stuck in a crash loop
  16. docker cp to copy files from a running container to the host or the way around. docker cp :/etc/file . to copy /etc/file to your current directory.

Some combinations that help a lot:

  • kill all running containers with docker kill $(docker ps -q)
  • delete all stopped containers with docker rm $(docker ps -a -q)
  • delete all images with docker rmi $(docker images -q)
  • update and stop a container that is in a crash-loop with docker update --restart=no && docker stop
  • bash shell into container docker exec -i -t /bin/bash - if bash is not available use /bin/sh
  • bash shell with root if container is running in a different user context docker exec -i -t -u root /bin/bash

What about resource usage 

Get docker container including size: docker ps -s

docker commands

Get docker container disk utilization: docker system df

docker system df

What about wasted resources?

The first command is used to remove local container images that are not used by any container and are not tagged. Its a pretty safe command to clean up your local filesystem

docker image prune

If you want to clean up and delete all unused container images, just add -a

docker image prune -a

only clean up container images that have been created over 24 hours ago run

docker image prune -a --filter "until=24h"

same can be done with container, that are not running anymore

docker container prune

Here comes a bonus command:

truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

That's it for now - please let us know if you like to add a command you feel is extremely important. We're doing our best to add these in future blog posts are just append to the current posts. 

Btw. as we covered the Podman project some days ago, most of these commands work with podman instead of docker as well.