Sitemap

Docker commands Cheat-sheet

7 min readAug 20, 2024

--

These days, Docker is the most important tool for software development, integration, and deployment. Docker is responsible for creating, running, and deploying containers, and when software developer or tester team use containers, Docker ensures that we won’t see a difference between our production and local development or test environments.

Get Sanjeev Tyagi’s stories in your inbox

Join Medium for free to get updates from this writer.

I have divided all command into headings. So, first starts with :

Registry & Repository:

The official Docker repository, known as DockerHub, has a ton of pre-built Docker Images. Actually, DockerHub allows you to push your own personalized image. However, it is essential to validate these registered images prior to usage to prevent potential issues.

## To log in to a docker hub registry.
docker login -u <user name> -e <email>

## To log out from a registry.
docker logout

## It will search the registry for the image.
docker search <image name>

## It will pull an image from the registry to the local machine.
docker pull <image name>

## It will push an image to the registry from the local machine.
docker push <image name> <repo name>

Build, Pull, Export or Import Images:

Docker images are read-only templates with container creation instructions. A Docker image is a blueprint or snapshot of the libraries and dependencies needed for an application to function inside of a container. Thus, Docker Images can be thought of as templates for building containers.

## It will display all images as list.
docker images

## Create an image from Dockerfile
## the dot in last of command tells us that Dockerfile is exists in current path
docker build -t <image name> .

## To display the history of an image
docker history <image name>

## this command associates the tag name with image name
## by default tag name is "latest"
docker image tag <image name> <image name>:<tag name>

## if images are not required or obsolete then these images need to be deleted
docker image rm <image name>
docker rmi <image id>

## Create a new docker image from a container .
docker commit <container ID> <new image name>

## Image from a tarball.
docker image import

## It will load an image from the mentioned file along with its history.
docker image load < my_image.tar.gz>

## It will export the container.
docker export my_container | gzip > my_container.tar.gz

## It will load an image from a tar archive as STDIN, including images and tags.
docker image load -i <image.tar>

## It will save an image to a tar archive stream to STDOUT with all parent layers, tags, & versions.
docker save <image name> > <image name.tar>


## created zip as well
docker save <my_image:my_tag> | gzip > my_image.tar.gz

Run, Start, stop or remove a Containers:

A Docker container image is an executable software package that is tiny and standalone. It contains code, runtime, system tools, system libraries, and settings.

## It will create a container without starting it.
docker create

## if container is stopped then Start a docker container
docker start <container id >

## It will create and start the container in one task.
docker run --name <container name> <image name >

## To rename the container.
docker rename <container name> <new container name>


## It will keep the container running, -t will allocate a pseudo-TTY session,
## and -d will detach (in background) the container automatically.
docker run -td container_id

## Container will be removed, once it is stoped.
docker run –rm --name <container name> <image name>

## It will map a directory on the host to a docker container.
docker run -v <host dir>:<dir inside container> --name <container name> <image name>

## Docker 1.10 comes with the logging driver for each container, and it will run docker with a custom log driver.
docker run --log-driver=syslog --name <container name> <image name>

## To execute a command in a container.
docker exec -it <image name> <command to exeute inside container>

## It will connect to a running container.
docker attach <container name>

## Run container in background
docker detach <container name>

## If container is running and user need to stop that containter
docker stop <container id>

## To remove the container. If container is in running stat then it will
## not be removed. So, first container need to be stopped
docker rm <container id>

## It will remove the volumes associated with the container.
docker rm -v <container id>

Container Resource management:

It is essential to understand how Docker containers distribute and consume system resources in order to optimize and manage them efficiently. By wrapping processes in containers, Docker offers techniques for resource management that enable the restriction and separation of CPU, memory, disk I/O (storage/volume), and network resources.

## limit the CPU time, either using a percentage of all CPUs or by using specific cores. 
## 1024 means 100% of the CPU, so if you want the container to take 50% of all CPU cores, you should specify 512.
docker run -td --cpu-shares=512 --name mycontainer ubuntu

## CPU cores using cpuset-cpus.
docker run -td --cpuset-cpus=0,4,6 --name mycontainer ubuntu

## CPU Quota (--cpu-quota) and CPU Period (--cpu-period):
## The CPU time restriction is controlled by these factors. For instance, a container with a 50000 quota and a 100000 microsecond time limit can utilize half of a single CPU.
docker run -td --cpu-quota=50000 --cpu-period=100000 --name mycontainer ubuntu

## Memory Limit (--memory):
## Define the maximum amout of memory that a container can use.
docker run -td --memory="256m" --name mycontainer ubuntu

## Memory Reservation (--memory-reservation):
## Offers a minimal amount of memory reserve. Docker attempts to maintain the available memory above this threshold even if the system is running short on memory.
docker run --memory-reservation="128m" --name mycontainer ubuntu

## Swap Limit (--memory-swap):
## controls the use of swap space. If developer set it to a value, swap use cannot exceed the RAM limit that has been specified.
docker run --memory="256m" --memory-swap="256m" --name mycontainer ubuntu

## Disk I/O performance can significantly impact container performance, especially for I/O-intensive applications.
## Block I/O Weight (--blkio-weight):
## specifies the relative weight of disk input/output, akin to CPU shares.
docker run --blkio-weight=500 --name mycontainer ubuntu

## Block I/O Device Limitations (--device-read-bps, --device-write-bps):
## Restricts certain devices' read/write rates (measured as bytes per second).
docker run --device-read-bps /dev/sda:1mb --device-write-bps /dev/sda:1mb your_image

## command dynamically updates container configuration.
## The resource limitations of a container will be updated. Generally, if you run a container without any options, it will start and stop immediately.
docker update --cpu-shares=512 --memory="256m" mycontainer

Debug and analyzing containers :

check the attributes of docker container

## It will get the events from the container.
docker events --since '2024-06-20'

## or in Json format
docker events --format '{{json .}}'

## It will display the public-facing port of the container.
docker port <container name>

## It will display the running processes in the container.
docker top <container name>

## It will display the containers' resource usage statistics.
docker stats

## It will display a list of all containers, default shows just running.
docker stats -all

## List the changed files and directories in a container᾿s filesystem
## since the container was created.
## Three different types of change are tracked:
## A: file or directory was added
## D: file or directory was deleted
## C: file or directory was changed
docker diff <container name / container id>

## It will display the running containers.
docker ps
docker ps -a ## It will display the running and stopped containers.

## It will listed the running containers.
docker container ls

## copy files from host to container or vice versa
docker cp ./file CONTAINER:/dir/

## Provide the logs from the container. (You can use a custom log driver, but logs are only available for json-file and journald in 1.10).
docker logs

## It checks all the information on a container (including IP address).
docker inspect

## Get the command/bash prompt of a running container,
## attach a new shell process to a running container.
docker exec -it mycontainer /bin/bash

Container Life Cycle :

Press enter or click to view image in full size
Container Life Cycle
## It will start a container, so it is running.
docker start <container name / container id>

## It will stop a running container.
docker stop <container name / container id>

## It stops and starts a container.
docker restart <container name / container id>

## It will pause a running container, "freezing" it in place.
docker pause <container name / container id>

## It will unpause a running container.
docker unpause <container name / container id>

## It will block until the running container stops.
docker wait <container name / container id>

## It sends a SIGKILL to a running container.
docker kill <container name / container id>

Cleaning up:

To optimize the usage of the resources, you need to clean up the resources frequently to maintain the performance. You can run the following commands to clean up resources.

## It will clean an unused/dangling image and networks (except volume)
docker image prune

## It will remove an image not used in a container.
docker image prune -a

## It will prune the entire system.
docker system prune

## It will v.
docker kills $ (docker ps -q)

## It will delete all stopped containers
docker rm $(docker ps -a -q)

## It will delete all images.
docker rmi $(docker images -q)

--

--