0% found this document useful (0 votes)
9 views

Docker Commands Cheatsheet With Examples

This document is a comprehensive cheat sheet for Docker commands, providing a list of essential commands along with examples for each. It covers basic operations such as running, stopping, and removing containers, as well as managing images, volumes, and networks. Additionally, it includes commands for Docker Swarm management and context handling.

Uploaded by

Sai Rajeev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Docker Commands Cheatsheet With Examples

This document is a comprehensive cheat sheet for Docker commands, providing a list of essential commands along with examples for each. It covers basic operations such as running, stopping, and removing containers, as well as managing images, volumes, and networks. Additionally, it includes commands for Docker Swarm management and context handling.

Uploaded by

Sai Rajeev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Mayank_Singh

Docker Commands Cheat Sheet with Examples

Basic Docker Commands

1. Check Docker Version

o Command: docker --version

o Example: docker --version → Outputs: Docker version 20.10.7

2. Run a Container

o Command: docker run <image-name>

o Example: docker run nginx → Runs an NGINX container.

3. List Running Containers

o Command: docker ps

o Example: docker ps → Shows all running containers.

4. List All Containers

o Command: docker ps -a

o Example: docker ps -a → Shows running and stopped containers.

5. Stop a Container

o Command: docker stop <container-id>

o Example: docker stop 12345abc → Stops the specified container.

6. Remove a Container

o Command: docker rm <container-id>

o Example: docker rm 12345abc → Removes the specified container.


Mayank_Singh

7. Pull an Image

o Command: docker pull <image-name>

o Example: docker pull ubuntu → Downloads the Ubuntu image.

8. List Docker Images

o Command: docker images

o Example: docker images → Lists all Docker images on the host.

9. Remove an Image

o Command: docker rmi <image-id>

o Example: docker rmi abc123 → Removes the specified image.

10. Build an Image from a Dockerfile

o Command: docker build -t <image-name> .

o Example: docker build -t my-app . → Builds an image named "my-app."

11. View Logs of a Container

o Command: docker logs <container-id>

o Example: docker logs 12345abc → Shows logs of the specified container.

12. Run a Command in a Running Container

o Command: docker exec <container-id> <command>

o Example: docker exec 12345abc ls /app → Lists files in /app.

13. Start a Stopped Container

o Command: docker start <container-id>

o Example: docker start 12345abc → Restarts a stopped container.

14. Restart a Container

o Command: docker restart <container-id>

o Example: docker restart 12345abc → Restarts the specified container.

15. Inspect a Container

o Command: docker inspect <container-id>

o Example: docker inspect 12345abc → Displays detailed information about the


container.

16. Inspect an Image

o Command: docker inspect <image-name>

o Example: docker inspect ubuntu → Shows details about the Ubuntu image.
Mayank_Singh

17. Export a Container to a Tar File

o Command: docker export <container-id> > <file-name>.tar

o Example: docker export 12345abc > container.tar → Exports container filesystem.

18. Import a Tar File as an Image

o Command: docker import <file-name>.tar

o Example: docker import container.tar → Creates an image from the tar file.

19. Run a Container in Detached Mode

o Command: docker run -d <image-name>

o Example: docker run -d nginx → Runs NGINX in the background.

20. Run a Container with Port Mapping

o Command: docker run -p <host-port>:<container-port> <image-name>

o Example: docker run -p 8080:80 nginx → Maps host port 8080 to container port 80.

21. Check Docker System Information

o Command: docker info

o Example: docker info → Displays Docker environment details.

22. Save an Image as a Tar File

o Command: docker save -o <file-name>.tar <image-name>

o Example: docker save -o my-app.tar my-app → Saves "my-app" image as a tar file.

23. Load an Image from a Tar File

o Command: docker load < <file-name>.tar

o Example: docker load < my-app.tar → Loads "my-app" image from the tar file.

24. Tag an Image

o Command: docker tag <image-id> <repository>:<tag>

o Example: docker tag abc123 my-app:latest → Tags image as "my-app:latest."

25. Push an Image to Docker Hub

o Command: docker push <repository>:<tag>

o Example: docker push my-app:latest → Pushes "my-app:latest" to Docker Hub.

26. Create a Volume

o Command: docker volume create <volume-name>

o Example: docker volume create my-volume → Creates a volume named "my-


volume."
Mayank_Singh

27. List Volumes

o Command: docker volume ls

o Example: docker volume ls → Lists all volumes.

28. Remove a Volume

o Command: docker volume rm <volume-name>

o Example: docker volume rm my-volume → Removes "my-volume."

29. Create a Network

o Command: docker network create <network-name>

o Example: docker network create my-network → Creates a network named "my-


network."

30. List Networks

o Command: docker network ls

o Example: docker network ls → Lists all Docker networks.

31. Connect a Container to a Network

o Command: docker network connect <network-name> <container-id>

o Example: docker network connect my-network 12345abc → Connects container to


"my-network."

32. Disconnect a Container from a Network

o Command: docker network disconnect <network-name> <container-id>

o Example: docker network disconnect my-network 12345abc → Disconnects


container from "my-network."

33. Remove a Network

o Command: docker network rm <network-name>

o Example: docker network rm my-network → Removes "my-network."

34. Create a Detached Container with a Name

o Command: docker run -d --name <container-name> <image-name>

o Example: docker run -d --name my-nginx nginx → Runs NGINX container named "my-
nginx."

35. Rename a Container

o Command: docker rename <old-name> <new-name>

o Example: docker rename old-container new-container → Renames a container.

36. Prune Unused Docker Objects


Mayank_Singh

o Command: docker system prune

o Example: docker system prune → Cleans up unused containers, images, and


volumes.

37. Inspect a Volume

o Command: docker volume inspect <volume-name>

o Example: docker volume inspect my-volume → Displays details about "my-volume."

38. Inspect a Network

o Command: docker network inspect <network-name>

o Example: docker network inspect my-network → Shows details of "my-network."

39. Limit CPU Usage for a Container

o Command: docker run --cpus="<value>" <image-name>

o Example: docker run --cpus="1.5" nginx → Limits the container to 1.5 CPUs.

40. Limit Memory Usage for a Container

o Command: docker run -m <value> <image-name>

o Example: docker run -m 512m nginx → Limits memory to 512 MB.

41. Run an Interactive Container

o Command: docker run -it <image-name>

o Example: docker run -it ubuntu bash → Starts an interactive Ubuntu shell.

42. Kill a Running Container

o Command: docker kill <container-id>

o Example: docker kill 12345abc → Forcefully stops the container.

43. Create a Dockerfile

o Command: touch Dockerfile

o Example: touch Dockerfile → Creates a new Dockerfile in the current directory.

44. Login to Docker Hub

o Command: docker login

o Example: docker login → Prompts for Docker Hub credentials.

45. Logout from Docker Hub

o Command: docker logout

o Example: docker logout → Logs out from Docker Hub.

46. Run a Container with Environment Variables


Mayank_Singh

o Command: docker run -e <env-var>=<value> <image-name>

o Example: docker run -e ENV=production nginx → Sets "ENV" variable to


"production."

47. View Container Stats

o Command: docker stats

o Example: docker stats → Displays resource usage statistics of running containers.

48. Change Restart Policy of a Container

o Command: docker update --restart=<policy> <container-id>

o Example: docker update --restart=always 12345abc → Updates the restart policy.

49. Pause a Running Container

o Command: docker pause <container-id>

o Example: docker pause 12345abc → Pauses the container.

50. Unpause a Paused Container

o Command: docker unpause <container-id>

o Example: docker unpause 12345abc → Resumes the paused container.

51. List All Swarm Nodes


o Command: docker node ls
o Example: docker node ls → Lists all nodes in a Docker Swarm cluster.
52. Create a Service in Swarm Mode
o Command: docker service create --name <service-name> <image-
name>
o Example: docker service create --name web nginx → Creates a service
named "web" running NGINX.
53. List Services in Swarm Mode
o Command: docker service ls
o Example: docker service ls → Lists all services in a Swarm cluster.
54. Inspect a Docker Service
o Command: docker service inspect <service-name>
o Example: docker service inspect web → Displays details of the "web"
service.
55. Scale a Service
o Command: docker service scale <service-name>=<replicas>
o Example: docker service scale web=5 → Scales the "web" service to 5
replicas.
56. Update a Service
o Command: docker service update --image <new-image> <service-
name>
o Example: docker service update --image nginx:latest web →
Updates the "web" service with a new image.
57. Remove a Service
o Command: docker service rm <service-name>
Mayank_Singh

o Example: docker service rm web → Removes the "web" service.


58. Initialize Docker Swarm
o Command: docker swarm init
o Example: docker swarm init → Initializes a Swarm cluster.
59. Join a Node to a Swarm
o Command: docker swarm join --token <token> <manager-IP>
o Example: docker swarm join --token abc123 192.168.1.10:2377 →
Joins a node to a Swarm cluster.
60. Leave a Swarm Cluster
o Command: docker swarm leave
o Example: docker swarm leave → Removes a node from the Swarm cluster.
61. Promote a Node to Manager
o Command: docker node promote <node-id>
o Example: docker node promote worker1 → Promotes "worker1" to a
manager.
62. Demote a Manager Node
o Command: docker node demote <node-id>
o Example: docker node demote manager1 → Demotes "manager1" to a
worker.
63. List Tasks in Swarm Mode
o Command: docker service ps <service-name>
o Example: docker service ps web → Lists tasks associated with the "web"
service.
64. Display Events from Docker Daemon
o Command: docker events
o Example: docker events → Displays real-time events from Docker daemon.
65. Display Disk Usage by Docker
o Command: docker system df
o Example: docker system df → Shows disk usage by images, containers, and
volumes.
66. Enable BuildKit
o Command: DOCKER_BUILDKIT=1 docker build -t <image-name> .
o Example: DOCKER_BUILDKIT=1 docker build -t my-app . → Builds
image using BuildKit.
67. Create a Context
o Command: docker context create <context-name>
o Example: docker context create my-context → Creates a new Docker
context.
68. List Docker Contexts
o Command: docker context ls
o Example: docker context ls → Lists all Docker contexts.
69. Use a Docker Context
o Command: docker context use <context-name>
o Example: docker context use my-context → Switches to "my-context."
70. Remove a Docker Context
o Command: docker context rm <context-name>
o Example: docker context rm my-context → Removes "my-context."

You might also like