Docker is a tool that every developer will encounter at least once. However, if you're not using it daily, the commands can easily slip your mind when you try to use it again after some time.
I, too, often find myself searching the official documentation or looking for commands whenever that happens, so I created a cheat sheet that summarizes frequently used Docker commands in a simple and clear manner to reduce this repetitive process.

I have included only the essentials so you can pull it out whenever you need. ๐Ÿ˜Š


Docker-themed digital whale illustration

1. Image Related Commands

Command Description
docker build -t <name>:<tag> . Create an image based on the Dockerfile in the current directory
docker images List images available locally
docker rmi <imageID> Delete an image
docker pull <image> Fetch an image from a remote repository
docker push <image> Push an image to a repository (login required)

2. Container Related Commands

Command Description
docker run -d --name <name> -p 8000:8000 <image> Run a container in the background
docker ps List currently running containers
docker ps -a List all containers, including stopped ones
docker exec -it <container_name> /bin/bash Enter a running container
docker logs -f <container_name> View real-time logs
docker stop <container_name> Stop a container
docker rm <container_name> Delete a stopped container
docker restart <container_name> Restart a container

3. Volumes and Networks

Command Description
docker volume ls List volumes
docker volume prune Clean up unused volumes
docker network ls List networks
docker network inspect <network_name> View network details

๐Ÿ”น 4. Docker Compose

Command Description
docker-compose up -d Run services in the background
docker-compose down Stop and remove all services
docker-compose ps List running services
docker-compose logs -f Check real-time logs
docker-compose restart <service_name> Restart a service

๐Ÿ”น 5. Docker Swarm

Command Description
docker swarm init Initialize swarm mode
docker stack deploy -c docker-compose.yml <stack_name> Deploy a stack
docker stack rm <stack_name> Stop and remove a stack
docker service ls List services
docker service ps <service_name> View service status
docker service update --force <service_name> Restart a service (apply new settings)

๐Ÿ”น 6. Other Useful Commands

Command Description
docker system df Check disk usage
docker system prune Clean up unused images/containers/volumes
docker stats Check real-time resource usage of containers
docker inspect <container_name or image_name> View detailed information about an object
docker info Current Docker environment information
journalctl -u docker.service Check logs of the Docker service based on systemd
sudo systemctl status docker Check the status of the Docker daemon
sudo systemctl restart docker Restart the Docker daemon
sudo less /var/log/syslog Navigate Docker-related messages in the overall system log (for Ubuntu)

I hope this is helpful to many developers.

For more information about Docker, there are plenty of resources available on the blog, so feel free to use the search box on the right to look up what you're curious about by keyword.