DEV Community

Santosh Yadav
Santosh Yadav

Posted on

Docker Cheat Sheet

🐳 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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 5. Inspect & Logs

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

πŸ”Ή 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"]
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Bind mount example:

docker run -v $(pwd):/app myapp
Enter fullscreen mode Exit fullscreen mode

(Mounts current folder into container).


πŸ”Ή 8. Networking

docker network ls                # List networks
docker network create mynetwork  # Create custom network
docker run --network=mynetwork ...
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

Run with:

docker compose up -d
docker compose down
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
juliashevchenko profile image
Julia Shevchenko

Very handy!
I have something to add. When docker has eaten a lot of the memory, you can use docker system prune to clean up the stopped containers, dangling images and build cache. You could add -a and --volumes flags. But be cautious with them!