Skip to main content

Docker Important Commands - All-in-one

Docker Important Commands:

image source: itzgeeks.com


1. To show the information about the docker

docker info

2. To show all the management and other command used in docker

docker --help

3. To show only running container

docker container ls/list/ps

4. To show all containers (Running, Stop, Exited)

docker container ls -a

5. To run/deploy a container

docker container run ubuntu

6. To run/deploy a container with sleep 60s

docker container run ubuntu sleep 60

7. To run/deploy a container with sleep 60s with busy the terminal (detach mode)

docker container run -d ubuntu sleep 60

8. To run/deploy a container in background ( detach | interactive)

docker container run -d -it ubuntu sleep 60

9. To stop a container with container id or container name (gracefully)

docker container stop <CONTAINER_ID/NAME>

10. To start a container with container id or container name

docker container start <CONTAINER_ID/NAME>

11. To restart a container with container id or container name

docker container restart <CONTAINER_ID/NAME>

12. To remove a stop/exited container with container id or container name

docker container rm <CONTAINER_ID/NAME>

13. To remove a running container with container id or container name forcefully

docker container rm <CONTAINER_ID/NAME> -f

14. To remove multiple container with container id or container name

docker container rm <CONTAINER_ID/NAME> <CONTAINER_ID/NAME>

15. To run a  container and get the container shell (inside a container)

docker container rm <CONTAINER_ID/NAME>

16. To get the information about a container (inspect a container)

docker container inspect <CONTAINER_ID/NAME>

17. To check the CPU, Memory and other resource usage statistics a single/multiple container

docker container stats <CONTAINER_ID/NAME>

18. To see the running processes of a container

docker container top <CONTAINER_ID/NAME>

19. To map container to a port to access it outside docker host 

docker container run -it -p 3600:80 ubuntu /bin/bash

20. To rename a container (if existing container running on default name change as our need

docker container rename <CONTAINER_ID/NAME> <NEW_NAME>

21. To create a container with a name of your choice

docker container run -it --name <CONTAINER_NAME> ubuntu

22. To enter into a running container

docker container attach <CONTAINER_ID/NAME>

23. To pause a running container

docker container pause <CONTAINER_ID/NAME>

24. To un-pause a pause container

docker container unpause <CONTAINER_ID/NAME>

25. To stop a running container forcefully

docker container kill <CONTAINER_ID/NAME>

26. To copy file from docker host to a running container (e.g. in /tmp directory)

docker container cp <FILE_NAME> <CONTAINER_ID/NAME>:/tmp/

27. To export a container as file and share with anyone

docker container export <CONTAINER_ID/NAME> > <FILENAME.tar>

28. To export a container as file and share with anyone

docker container export <CONTAINER_ID/NAME> -o <FILENAME.tar>

29. To import an exported container file and create an image out of it

docker image import <FILENAME.tar> <NEW_NAME_AS_YOUR_CHOICE>

Now run a new container from the exported image

docker container run -it <IMAGE_NAME> /bin/bash

30. To create an image from a running container

docker container commit <CONTAINER_ID/NAME> <IMAGE_NAME_OF_YOUR_CHOICE>

31. To inspect changes on container filesystem

docker container diff <CONTAINER_ID/NAME>

32. To run a command into a running container

docker container exec <CONTAINER_ID/NAME>

33. To fetch logs from a container 

docker container logs <CONTAINER_ID/NAME>

34. To list port mapping for the container

docker container port <CONTAINER_ID/NAME>

35. To remove all stopped container

docker container prune

36. To update the configuration of a container

docker container update <OPTION> <CONTAINER_ID/NAME>

37. To block until one or more container stop, then print their exit code

docker container wait <CONTAINER_ID/NAME>

38. To create a MySQL container with environment Variable (Empty Password)

docker container run -it --name mysql-server -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

39. How to remove all stopped container at once (trick)

docker container rm $(docker container ls -aq)

Note: "q" will print all the container id

Docker Create Image, Pull, Push and Docker Hub

1. Docker Hub login

docker login

Login with your username and password for the Docker Hub account

2. How to tag an existing image for pushing to the docker hub account

docker tag <EXISTING_REPOSITORY_NAME> <gautamthakur1983/IMAGE_NAME_OF_YOUR_CHOICE>

3. To push your local image to Docker Hub Repository

docker image push <gautamthakur1983/IMAGE_NAME>

4. To create an image from a Dockerfile

docker image build -t <IMAGE_NAME_OF_YOUR_CHOICE> .
Note: "." represent the path of "Dockerfile" as currently Dockerfile is from where we are building the docker image.

5. To list all the available images

docker image ls

6. How to remove an image 

docker rmi <IMAGE_NAME>


Docker Volume, Mount

1. To create a Docker Volume

docker volume creare <VOLUME_NAME>

2. To get information about the Docker Volume

docker volume inspect <VOLUME_NAME>

3. To create a container with available docker volume

docker container run -it -v <VOLUME_NAME>:/tmp --name <CONTAINER_NAME> ubuntu /bin/bash
/tmp where we are mounting a volume while creating a new container




Thanks for visiting on this page, I have create this page for my own Docker practice. Stay tune for the latest post on other topics.


Popular posts from this blog

WordPress Site is not loading properly behind Google Cloud/AWS Load Balancer

Hello Guys, Today we are going to understand how can we fix a WordPress loading issue (CSS and JS loading issue) behind the Google Cloud Load Balancer or AWS Load Balancer. Generally, When we host a WordPress site directly with Google Cloud Compute Engine VM instance or AWS EC2 instance. It's loading perfectly fine. But once we added this WordPress server behind any Load Balancer either from GCP Load Balancer or AWS Load Balancer, you site will completely broken, means the CSS and JS of your site not loaded properly. The reason for this is - When you put a load balancer in front of WordPress, you need to modify wp-config.php to process the HTTP header HTTP_X_FORWARDED_PROTO to detect the protocol that the user is using and not the protocol the load balancer is using to connect to your backend. To fix this issue, we have to make following changes in the  wp-config.php  file and add the below code snippet on the top of  wp-config.php file - Google Cloud Platform Load Bal...

Git and GitHub Commands Mastery

1. Setup Git and GitHub Global Configuration  git config --global user.email "gautamthakur1983@gmail.com" git config --global user.name "Gautam Thakur" git config --global list git config --list 2. Git Lifecycle | Initilize, Status, Add, Commit git status git init git add git commit -m "Commit Message" git log git log --oneline 3. Git Difference between last commit changes and current version changes git diff 4. Git Compare between 2 different Git Commits git diff eac4c5b 82485b1 5. Git Statsh - To save some changes for temporary purpose ## Pop take out stash and clear but apply take out stash but not clear git stash (To save current changes for temporary) git stash pop (To take out all stash contents) git stash list git stash clear (To clear all the stash changes) git stash save "NAME"  git stash save "NAME1" (Working with multiple stash) git stash sapply 0 or 1 (0 for name, 1 for about) after that run git stash clear git stash clear 6...

How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS

  Hello Friends, In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL). Requirements: Master Node: No. of VMs 1 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access  Worker Node: No. of VMs 2 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet),  sudo access with admin access  Disable Swap and comment fstab entry: First, Disable Swap and remove or comment the Swap entries from fstab file : sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab Next...