Docker Cheatsheet

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It achieves this by packaging an application and its dependencies into an isolated, lightweight unit called a container.

Unlike traditional virtual machines that require a full operating system for each application, containers share the host machine's OS kernel. This makes them incredibly fast to start, highly resource-efficient, and easily portable across different environments.

Why Use Docker?

Consistency It solves the classic "it works on my machine" problem. A container behaves exactly the same way in development, testing, and production.
Efficiency Containers are lightweight. They boot up in seconds and consume a fraction of the memory compared to traditional VMs.
Isolation Each container operates independently. You can run different applications with conflicting dependencies on the same host without issues.
CI/CD Ready Docker fits perfectly into continuous integration and delivery pipelines, making automated testing and deployment much smoother.

Commands Reference

Command Description
docker --version
Check installed Docker version
docker info
Display detailed system-wide Docker info
Command Description
docker login
Authenticate with Docker Hub (enter username & password)
docker tag <image_name> <username>/<repo>:<tag>
Tag local image for Docker Hub repository
docker push <username>/<repo>:<tag>
Push tagged image to Docker Hub
docker logout
Log out from Docker Hub
Command Description
docker build -t <image_name> .
Build Docker image from Dockerfile
docker images
List all local Docker images
docker rmi <image_name>
Remove Docker image
docker pull <image_name>
Download image from Docker Hub
docker push <image_name>
Upload image to Docker Hub
Command Description
docker run <image>
Run a container from an image
docker run -it <image>
Run container in interactive mode (terminal access)
docker run -d <image>
Run container in detached (background) mode
docker run -p <host_port>:<container_port> <image>
Bind container port to host port
docker ps
List running containers
docker ps -a
List all containers (including stopped ones)
docker stop <container_id>
Stop a running container
docker start <container_id>
Start a stopped container
docker restart <container_id>
Restart a container
docker rm <container_id>
Remove a stopped container
docker logs <container_id>
View logs from container
docker exec -it <container_id> bash
Execute bash shell inside a running container
docker exec -it <container_id_or_name> sh
Execute shell (sh) in a running container
Command Description
docker volume create <volume_name>
Create a named volume
docker volume ls
List all volumes
docker volume inspect <volume_name>
View detailed info about a volume
docker volume rm <volume_name>
Remove a named volume
Command Description
docker network ls
List available Docker networks
docker network create <network_name>
Create a custom network
docker network inspect <network_name>
Inspect network configuration/details
docker network rm <network_name>
Remove a Docker network
Command Description
docker-compose up
Start all services in docker-compose.yml
docker-compose up -d
Start services in detached (background) mode
docker-compose up --build
Build images and then start services (recommended after Dockerfile change)
docker-compose down
Stop and remove containers, networks, volumes created by Compose
docker-compose build
Only build images defined in docker-compose.yml
docker-compose ps
List running containers from Compose
docker-compose logs
View logs for all services in Compose
docker-compose logs <service>
View logs for a specific service
docker-compose exec <service> sh
Exec into a running service's shell
Command Description
docker system prune
Remove all unused containers, networks, images
docker container prune
Remove all stopped containers
docker image prune
Remove unused Docker images
docker volume prune
Remove unused volumes
docker stop $(docker ps -q)
Stop all containers
docker rmi $(docker images -q)
Remove all images
docker rm $(docker ps -aq)
Remove all containers

When to Use What?

Use docker build when:

  • You are working with a single service app.
  • You are not using docker-compose.
  • You want to manually build a Docker image from a Dockerfile.

Example:

docker build -t my-app-image .
docker run -p 3000:3000 my-app-image

Use docker-compose up --build when:

  • You are managing multiple services (e.g., Node.js, MongoDB, Redis, etc.).
  • You have a docker-compose.yml file and a Dockerfile.
  • You made changes to your Dockerfile or environment variables and need to rebuild.

Recommended Command:

docker-compose up --build

Use this to:

  • Run all services defined in docker-compose.yml in the background (detached mode).
  • You already have images built or don't need to rebuild.

Example:

docker-compose up -d

Use this when:

  • You want to stop and clean up all services, networks, and volumes created by Docker Compose.

Example:

docker-compose down