Skip to main content

Command Palette

Search for a command to run...

Ultimate Docker Cheat Sheet: Installation, Basic, and Advanced Commands

Published
3 min read
Ultimate Docker Cheat Sheet: Installation, Basic, and Advanced Commands

Docker streamlines application deployment and management through containerization. This cheat sheet provides an essential guide for Docker installation on Ubuntu, as well as basic and advanced commands for efficient container management.


Docker Installation on Ubuntu

1. Update the Package Index

sudo apt-get update

2. Upgrade the Package Index Again

sudo apt-get upgrade

3. Install Docker Engine

sudo apt-get install docker.io

4. Start Docker Service

sudo systemctl start docker

5. Enable Docker to Start at Boot

sudo systemctl enable docker

6. Add Your User to the Docker Group

sudo usermod -aG docker $USER

Log out and log back in for group changes to take effect.


Basic Docker Commands

1. Check Docker Version

docker --version

2. Pull an Image

docker pull <image>:<tag>

3. List Docker Images

docker images

4. Remove an Image

docker rmi <image>:<tag>

5. Run a Container

docker run [OPTIONS] <image>:<tag>

6. List Running Containers

docker ps

7. List All Containers (including stopped)

docker ps -a

8. Stop a Container

docker stop <container_id>

9. Remove a Container

docker rm <container_id>

10. View Container Logs

docker logs <container_id>

11. Execute a Command in a Running Container

docker exec -it <container_id> <command>

12. Inspect a Container or Image

docker inspect <container_id_or_image>

13. Create and Start a Container (combination)

docker run -d --name <container_name> <image>:<tag>

14. View Container Stats

docker stats

Intermediate Docker Commands

15. Build an Image from a Dockerfile

docker build -t <image_name>:<tag> .

16. Tag an Image

docker tag <image_id> <repository>:<tag>

17. Push an Image to a Repository

docker push <repository>:<tag>

18. Pull an Image from a Repository

docker pull <repository>:<tag>

19. Remove All Stopped Containers

docker container prune

20. Remove All Unused Images

docker image prune -a

21. Remove All Unused Volumes

docker volume prune

22. Remove All Unused Networks

docker network prune

23. Remove All Unused Resources (containers, images, volumes, networks)

docker system prune

24. Commit Changes to a Container as a New Image

docker commit <container_id> <new_image_name>:<tag>

Advanced Docker Commands

25. Create a Network

docker network create <network_name>

26. List Networks

docker network ls

27. Inspect a Network

docker network inspect <network_name>

28. Connect a Container to a Network

docker network connect <network_name> <container_id>

29. Disconnect a Container from a Network

docker network disconnect <network_name> <container_id>

30. Create a Volume

docker volume create <volume_name>

31. List Volumes

docker volume ls

32. Inspect a Volume

docker volume inspect <volume_name>

33. Remove a Volume

docker volume rm <volume_name>

34. Run a Container with Specific Resource Limits

docker run -d --memory="512m" --cpus="1.0" <image>:<tag>

35. View Docker System Information

docker info

36. Generate a Docker Compose File

docker-compose config

37. Start Services Defined in a Docker Compose File

docker-compose up

38. Stop Services Defined in a Docker Compose File

docker-compose down

39. Scale Services in Docker Compose

docker-compose up --scale <service>=<num>

40. Update Docker Compose File

docker-compose up -d

41. Execute a Command in a Running Docker Compose Service

docker-compose exec <service> <command>

42. View Docker Compose Logs

docker-compose logs

This cheat sheet encompasses a range of Docker commands, from installation on Ubuntu to managing containers and images. Use it to navigate Docker’s functionalities efficiently and enhance your container management workflows.

More from this blog

DevOps journey

34 posts

In this DevOps journey, we’ll explore a range of DevOps tools and related projects.