100% found this document useful (1 vote)
292 views11 pages

Docker Cheat Sheet: by Devops G

This cheat sheet summarizes common Docker commands. It describes commands for listing and managing containers and images, running containers with various options, exporting and importing containers, managing volumes, and building images from Dockerfiles. Key commands include docker ps to list running containers, docker pull to pull images, docker run to run containers, and docker build to build images from Dockerfiles. It also provides explanations of concepts like port mapping and differences between commands like docker volume rm and docker volume prune.

Uploaded by

Nik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
292 views11 pages

Docker Cheat Sheet: by Devops G

This cheat sheet summarizes common Docker commands. It describes commands for listing and managing containers and images, running containers with various options, exporting and importing containers, managing volumes, and building images from Dockerfiles. Key commands include docker ps to list running containers, docker pull to pull images, docker run to run containers, and docker build to build images from Dockerfiles. It also provides explanations of concepts like port mapping and differences between commands like docker volume rm and docker volume prune.

Uploaded by

Nik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Docker Cheat Sheet by DevOps G

→ docker version ( Provides docker version information)

➔ docker ps (Lists the running container)

https://www.facebook.com/groups/devopsg/
→ docker ps -a (this lists all containers either stopped or in running condition)

→ docker ps -l (this is used to show the latest docker container created or modified)

→ docker images (Lists all available image in host OS in which docker engine is installed)

→ docker pull image_name (Pulls an image from Docker Hub)

-→ docker run -it -d image_name (Runs a command in an isolated container [ i=interactive, t=tty means for
terminal , d= for daemon or background process or in detached mode])

https://www.facebook.com/groups/devopsg/
→ docker start container_id (Starts the stopped containers)

→ docker stop container_id (Stops the running containers)

→ docker kill container_id (this will kill the container)

→ docker attach (Attaches your local input/output/error stream to a running container)

→ docker commit (Creates a new image from the current changed state of the container)

→ docker exec (to go inside the container)

→ docker history image_name (Displays the history of an image)

https://www.facebook.com/groups/devopsg/
→ docker info (Shows system-wide information)

→ docker inspect (Finds system-level information about docker containers and images)

→ docker login (Logins to local registry or Docker Hub)

→ docker restart container_id (Stops and starts a container)

https://www.facebook.com/groups/devopsg/
→ docker rm container_id ( Remove stopped containers)

→ docker rmi image_name (Remove images)

→ docker rmi $(docker images -q) (this deletes all docker images for which no container is running)

→ docker rmi -f $(docker images -q) (this deletes all docker images forcefully)

→ docker rm -f container_id (this will remove running or stopped container forcefully)

https://www.facebook.com/groups/devopsg/
➔ docker rm -f $(docker ps -a -q)

→ docker search image_name (Searches the Docker Hub for images)

→ docker save image_name -o new_name.tar ( the new image name extension should be tar and this saves any
image to the tar format only the condition is image should be available on host os)

→ docker load -i new_name.tar (this will again create the image which we saved in last command, this mainly used
for sharing a docker image from one host to another host)

Similar to image we can use export and import command for saving container as .tar file and converting that .tar
file to docker image as shown below: -

→ docker export my_container -o my_container.tar (this will save our container in .tar file)

https://www.facebook.com/groups/devopsg/
→ docker import my_container.tar (this will create the image which was saved as tar in last command but this image
will be treated as filesystem as this doesn’t contain all the layers or history which we had seen in earlier command of
docker load)

What is difference between docker save and docker export?

Ans→ The main difference is docker save/load with image does preserve the image history. Whereas
docker export/import with container flattens the image by removing all the history of the container.

https://www.facebook.com/groups/devopsg/
→ docker logs container_id (this will give the logs of the container)

→ docker stats container_id (this will give the resource usage (CPU, RAM,network i/o) of mentioned container)

➔ docker stats (this will give resource usage of all available container in host OS)

→ docker stats container_id --no-stream (this will show the resource without streaming)

→ docker top container_id (this will display top process running in a container)

→ docker build -t myimage:version dockerfile_path (this will build the docker image from docker file)

→ docker tag existing_image:tag_name new_image_name:new_tag_name ( this will create an image with the
desired image name and tag name)

https://www.facebook.com/groups/devopsg/
Note:- for port mapping -p is for user defined port no. and -P is for random port no. which will be assigned by OS

But when we uses -P docker randomly assigns any free port to the port used by nginx i.e 80:-

Docker volume:-

https://www.facebook.com/groups/devopsg/
In below snap we can see by default no docker volume is existing:-

Now will create a docker volume name devopsg :-

https://www.facebook.com/groups/devopsg/
docker volume –help (this command gives all the available options to be applied with docker volume)

**> Difference between docker volume rm and docker volume prune?

Ans→ docker volume rm removes only docker volume of mentioned name whereas docker prune deletes all existing
docker volume .

For more live training, sessions, discussions on docker and all devops tools join us at our facebook page “DevOps G”

Facebook link:- https://www.facebook.com/groups/devopsg/

Youtube link:- https://www.youtube.com/channel/UCw8GNopnFTLRnEGiv0fOQIg/videos

Telegram Group Name:- DevOps G

https://www.facebook.com/groups/devopsg/

You might also like