Docker Cheat Sheet

Techie     May 2022


1 . List running containers

$ docker ps


2 . List all containers

$ docker ps -a


3 . Start, restart, stop or kill a docker container

$ docker start container_id

$ docker restart container_id

$ docker stop container_id

$ docker kill container_id


4 . Start a container and keep it running in the background Detached mode runs containers in the background.

$ docker start container_id -d

# Through docker-compose
$ docker-compose up -d


5 . Delete container

$ docker rm container_id


6 . Delete all stopped containers

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


7 . Kill all running containers

$ docker kill $ (docker ps -q)


8 . Show container logs

$ docker logs container_id


9 . Tail/follow container logs

$ docker logs -ft container_id


10 . Tail/follow container logs

$ docker logs -ft container_id


11 . Save a running container as an image

$ docker commit -m “commit message” -a “author” container_id username/image_id: tag


12 . Run a command in a container

$ docker exe -ti container_id command.sh


13 . Get shell access to a docker container

$ docker exec -it container_id sh


14 . Update your current package list

$ apk update


15 . Install a package

$ apk add package_name

# Example
# Install micro
# A terminal based text editor with syntax highlighting

$ apk add micro


16 . Search for packages

$ apk search -v 'package_name'


# Example
$ apk search -v 'node'


17 . Install bash and switch current shell to bash

$ apk add bash

$ bash


18 . View docker networks

$ docker network list


19 . List running images

$ docker images


20 . List all images

$ docker images -a


21 . Delete image

$ docker rmi image_id

# Example
$ docker rmi c430ys54229b


22 . Tag an image

$ docker tag image_id tage_name


23 . Create an image from a container. (A snapshot of the container)

$ docker commit container_id image_id


24 . Delete all images

$ docker rmi $(docker images -q)


25 . Remove an image that is not used in a container

$ docker image_id prune -a


26 . Clean an unused image

$ docker image_id prune


27 . Prune entire system

$ docker system prune


28 . Initialize the swarm mode and listen to a specific interface

$ docker swarm init --advertise-addr 47.51.0.26


29 . Join an existing swarm as a manager node

$ docker swarm join --token manager_token_here 47.51.0.26:2377


30 . Join a swarm as a worker node

$ docker swarm join --token manager_token_here 47.51.0.26:2377


31 . List all nodes in the swarm

$ docker node ls


32 . Create a service from an image on the existing port and deploy five instances

$ docker service create --replicas 5 -p 80:80 name -webnginx


33 . List services running in a swarm

$ docker service ls


34 . Scale a service

$ docker service scale web=5


35 . List the tasks of a service

$ docker service ps web


36 . View all running services

$ docker stack services stack_name


37 . View all service logs

$ docker service logs stack_name service_names


38 . Scale a service across qualified nodes

$ docker service scale stack_name_service_name = replicas


39 . Leave a swarm

$ docker stack rm stack_name


40 . Remove a swarm

$ docker stack rm stack_name


Thanks for reading, see you in the next one!