Essential Docker Commands Cheat Sheet: Streamline Your Container Management

Aditya Bhuyan
5 min read4 days ago

--

Photo by Bernd 📷 Dittrich on Unsplash

Docker Commands Cheat Sheet

Docker simplifies the process of creating, deploying, and managing applications using containers. This cheat sheet provides quick access to essential Docker commands for various tasks, from managing images and containers to networking, volumes, and Docker swarm operations.

Whether you’re new to Docker or looking to streamline your workflow, these commands will help you navigate and leverage Docker’s powerful capabilities efficiently.

1. Pull an image from Docker Hub

docker pull ubuntu:latest

2. List all Docker images

docker images

3. Run a container from an image

docker run -it ubuntu:latest /bin/bash

4. List all running containers

docker ps

5. List all containers (running and stopped)

docker ps -a

6. Stop a running container

docker stop <container_id>

7. Start a stopped container

docker start <container_id>

8. Restart a running container

docker restart <container_id>

9. Remove a container

docker rm <container_id>

10. Remove an image

docker rmi <image_id>

11. Run a container in detached mode

docker run -d ubuntu:latest

12. Run a container with a custom name

docker run --name mycontainer ubuntu:latest

13. Run a container and map host port to container port

docker run -p 8080:80 nginx

14. Run a container with environment variables

docker run -e MYVAR=myvalue ubuntu env

15. View container logs

docker logs <container_id>

16. Follow container logs in real-time

docker logs -f <container_id>

17. Inspect a container’s details

docker inspect <container_id>

18. Execute a command inside a running container

docker exec -it <container_id> /bin/bash

19. Copy a file from the host to the container

docker cp hostfile.txt <container_id>:/containerpath

20. Copy a file from the container to the host

docker cp <container_id>:/containerpath hostfile.txt

21. Commit changes to a container to create a new image

docker commit <container_id> newimagename

22. Build an image from a Dockerfile

docker build -t myimage:latest .

23. Tag an image

docker tag myimage:latest myrepo/myimage:latest

24. Push an image to Docker Hub

docker push myrepo/myimage:latest

25. Pull an image from a private registry

docker pull myregistry.com/myimage:latest

26. Login to a Docker registry

docker login myregistry.com

27. Logout from a Docker registry

docker logout myregistry.com

28. List all Docker networks

docker network ls

29. Create a new Docker network

docker network create mynetwork

30. Connect a container to a network

docker network connect mynetwork <container_id>

31. Disconnect a container from a network

docker network disconnect mynetwork <container_id>

32. Remove a Docker network

docker network rm mynetwork

33. Inspect a Docker network

docker network inspect mynetwork

34. Run a container with a specific network

docker run --network mynetwork ubuntu

35. Create a volume

docker volume create myvolume

36. List all Docker volumes

docker volume ls

37. Inspect a Docker volume

docker volume inspect myvolume

38. Remove a Docker volume

docker volume rm myvolume

39. Run a container with a volume

docker run -v myvolume:/data ubuntu

40. Run a container with a bind mount

docker run -v /host/path:/container/path ubuntu

41. Run a container with read-only volume

docker run -v myvolume:/data:ro ubuntu

42. Show Docker version

docker version

43. Show Docker system information

docker info

44. Prune unused containers, networks, images, and volumes

docker system prune

45. Prune unused volumes

docker volume prune

46. Prune unused networks

docker network prune

47. Prune unused images

docker image prune

48. Create a Dockerfile

cat <<EOF > Dockerfile
FROM ubuntu:latest
CMD ["echo", "Hello, World!"]
EOF

49. Build an image with a specific Dockerfile

docker build -t myimage:latest -f MyDockerfile .

50. Run a container with resource limits

docker run -m 512m --cpus=1 ubuntu

51. Run a container with restart policy

docker run --restart unless-stopped ubuntu

52. Pause a running container

docker pause <container_id>

53. Unpause a paused container

docker unpause <container_id>

54. Rename a container

docker rename old_name new_name

55. Show Docker disk usage

docker system df

56. Kill a running container

docker kill <container_id>

57. Create a swarm

docker swarm init

58. Join a swarm as a worker

docker swarm join --token <worker_token> <manager_ip>:2377

59. List swarm nodes

docker node ls

60. Leave a swarm

docker swarm leave

61. Deploy a stack

docker stack deploy -c docker-compose.yml mystack

62. List stacks

docker stack ls

63. List services in a stack

docker stack services mystack

64. Remove a stack

docker stack rm mystack

65. List all services

docker service ls

66. Create a service

docker service create --name myservice nginx

67. Scale a service

docker service scale myservice=3

68. Update a service

docker service update --image nginx:latest myservice

69. Remove a service

docker service rm myservice

70. Inspect a service

docker service inspect myservice

71. List service tasks

docker service ps myservice

72. Create a secret

echo "mysecret" | docker secret create mysecret -

73. List secrets

docker secret ls

74. Inspect a secret

docker secret inspect mysecret

75. Remove a secret

docker secret rm mysecret

76. Create a config

echo "myconfig" | docker config create myconfig -

77. List configs

docker config ls

78. Inspect a config

docker config inspect myconfig

79. Remove a config

docker config rm myconfig

80. Run a container with a specific hostname

docker run --hostname myhostname ubuntu

81. Run a container with a specific domain name

docker run --domainname example.com ubuntu

82. Run a container with a specific user

docker run --user myuser ubuntu

83. Run a container with specific environment variables from a file

docker run --env-file envfile.txt ubuntu

84. Run a container with a specific workdir

docker run --workdir /path/to/dir ubuntu

85. Run a container with a specific label

docker run --label com.example.version=1.0 ubuntu

86. Run a container with specified CPU shares

docker run --cpu-shares 512 ubuntu

87. Run a container with specified CPU quota

docker run --cpu-quota 50000 ubuntu

88. Run a container with specified memory limit

docker run -m 512m ubuntu

89. Run a container with specified memory reservation

docker run -m 512m --memory-reservation 256m ubuntu

90. Run a container with limited capabilities

docker run --cap-drop=NET_RAW ubuntu

91. Run a container in privileged mode

docker run --privileged ubuntu

92. Run a container with devices

docker run --device=/dev/sda:/dev/xvdc ubuntu

93. Run a container with DNS settings

docker run --dns 8.8.8.8 ubuntu

94. Run a container with additional hosts

docker run --add-host example.com:127.0.0.1 ubuntu

95. Run a container with a specific logging driver

docker run --log-driver=syslog ubuntu

96. Run a container with a specific ulimit

docker run --ulimit nofile=1024:2048 ubuntu

97. Run a container with sysctls

docker run --sysctl net.ipv4.ip_forward=1 ubuntu

98. Run a container with runtime settings

docker run --runtime=runc ubuntu

99. Run a container in interactive mode

docker run -it ubuntu /bin/bash

100. Run a container and remove it after exit

docker run --rm ubuntu

--

--

Aditya Bhuyan

I am a cloud practitioner with expertise in many SAAS, PAAS offerings. I have worked as an architect and developer for many paas and saas products.