Docker Cheat Sheet



This content originally appeared on DEV Community and was authored by Santosh Yadav

🐳 Docker Cheat Sheet: From Beginner to Expert

Docker makes it easy to build, ship, and run applications in containers. This cheat sheet is a progressive guide — starting from the basics and moving into advanced concepts.

🔹 1. Basics

Docker

A platform to run applications in isolated environments called containers. Think of it as lightweight virtual machines.

Image

A blueprint (snapshot) of your app — contains code, dependencies, and environment setup.

Container

A running instance of an image. You can start/stop/delete it.

Docker Hub

A public registry of prebuilt images (nginx, mysql, node, etc.).

🔹 2. Installation & Setup

docker --version         # Check Docker version
docker info              # System info (containers, images, storage driver, etc.)
docker login             # Login to Docker Hub

🔹 3. Working with Images

docker pull nginx        # Download image from Docker Hub
docker images            # List images
docker rmi nginx         # Remove image
docker build -t myapp .  # Build image from Dockerfile
  • -t myapp → Tags the image with a name.
  • . → Uses current directory (Dockerfile must be here).

🔹 4. Working with Containers

docker run hello-world         # Run a test image
docker ps                      # List running containers
docker ps -a                   # List all containers (even stopped)
docker stop <id>               # Stop a container
docker rm <id>                 # Remove a container

👉 Useful Flags for docker run:

  • -d → Run in background (detached).
  • -p 8080:80 → Map host port to container port.
  • --name mycontainer → Give custom name.

Example:

docker run -d -p 8080:80 nginx

🔹 5. Inspect & Logs

docker logs <id>               # View container logs
docker exec -it <id> bash      # Access shell inside container
docker inspect <id>            # Detailed info (JSON)

🔹 6. Dockerfile (Build Custom Images)

A Dockerfile defines how to build your image. Example:

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "server.js"]

👉 Commands used:

  • FROM → Base image.
  • WORKDIR → Working directory inside container.
  • COPY → Copy files from host to container.
  • RUN → Execute commands (install, setup).
  • CMD → Default command when container starts.

🔹 7. Volumes (Persistent Storage)

Containers are ephemeral (data is lost if removed). Volumes persist data.

docker volume create mydata
docker run -v mydata:/var/lib/mysql mysql

👉 Bind mount example:

docker run -v $(pwd):/app myapp

(Mounts current folder into container).

🔹 8. Networking

docker network ls                # List networks
docker network create mynetwork  # Create custom network
docker run --network=mynetwork ...

👉 Allows containers to communicate with each other.

🔹 9. Docker Compose (Multi-Container Apps)

Instead of running multiple containers manually, use docker-compose.yml:

version: "3"
services:
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: root
  web:
    build: .
    ports:
      - "8080:80"
    depends_on:
      - db

Run with:

docker compose up -d
docker compose down

🔹 10. Advanced Commands

docker stats                   # Real-time CPU/memory usage
docker system prune            # Clean up unused data
docker save -o myimage.tar app # Save image to tar file
docker load -i myimage.tar     # Load image from tar file
docker cp <id>:/file .         # Copy file from container


This content originally appeared on DEV Community and was authored by Santosh Yadav