Docker Cheatsheet

Written by

in

,

Container runtime essentials – build, run, manage, and orchestrate
All commands assume Docker Engine is installed and running.
Use sudo if not in docker group: sudo usermod -aG docker $USER → relogin.


1. Setup & Info

TaskCommand
Check versiondocker --version
Docker infodocker info
Test installationdocker run hello-world
Enable/start Dockersudo systemctl enable --now docker
Add user to docker groupsudo usermod -aG docker $USER
View Docker disk usagedocker system df

2. Images

ActionCommand
Search imagedocker search nginx
Pull imagedocker pull nginx:latest
List imagesdocker images or docker image ls
Remove imagedocker rmi nginx
Remove danglingdocker image prune
Remove all unuseddocker image prune -a
Build from Dockerfiledocker build -t myapp:1.0 .
Tag imagedocker tag myapp:1.0 username/myapp:1.0
Push to registrydocker push username/myapp:1.0
Inspect imagedocker image inspect nginx

3. Containers

ActionCommand
Run containerdocker run nginx
Run detacheddocker run -d --name web nginx
Run with portdocker run -d -p 8080:80 --name web nginx
Run interactivedocker run -it ubuntu bash
List runningdocker ps
List all (incl. stopped)docker ps -a
Stop containerdocker stop web
Start stoppeddocker start web
Restartdocker restart web
Remove containerdocker rm web
Remove all stoppeddocker container prune
Attach to runningdocker attach web
Exec into containerdocker exec -it web bash
View logsdocker logs web
Follow logsdocker logs -f web
Stats (CPU/RAM)docker stats
Inspect containerdocker inspect web

4. Volumes & Bind Mounts

TypeCommand
Volume (managed)docker run -v myvol:/data ubuntu
Create volumedocker volume create myvol
List volumesdocker volume ls
Inspect volumedocker volume inspect myvol
Remove volumedocker volume rm myvol
Bind mountdocker run -v /host/path:/container/path ubuntu
Read-onlydocker run -v /data:/app:ro nginx

5. Networks

ActionCommand
List networksdocker network ls
Create bridgedocker network create mynet
Run in networkdocker run --network mynet --name db -d mysql
Connect containerdocker network connect mynet web
Disconnectdocker network disconnect mynet web
Inspectdocker network inspect bridge

6. Dockerfile Essentials

# syntax: docker/dockerfile:1
FROM ubuntu:22.04
LABEL maintainer="you@example.com"

# Update & install
RUN apt update && apt install -y curl vim

# Copy files
COPY app.py /app/
ADD https://example.com/file.tar.gz /tmp/

# Set workdir
WORKDIR /app

# Expose port
EXPOSE 8000

# Environment
ENV DEBUG=true

# Run command
CMD ["python3", "app.py"]

Build: docker build -t myapp .
Run: docker run -p 8000:8000 myapp


7. Docker Compose (v2+)

Install: sudo apt install docker-compose-plugin or download binary

# docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    depends_on:
      - db
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - db-data:/var/lib/mysql
volumes:
  db-data:
CommandAction
docker compose up -dStart in background
docker compose downStop & remove
docker compose logs -fFollow logs
docker compose psList services
docker compose buildBuild images
docker compose exec web bashShell into service

8. Resource Management

TaskCommand
Limit CPUdocker run --cpus="1.5" ubuntu
Limit memorydocker run -m 512m ubuntu
Restart policydocker run --restart=unless-stopped nginx
HealthcheckAdd HEALTHCHECK CMD curl -f http://localhost/ || exit 1 in Dockerfile

9. Cleanup & Prune

# Remove all stopped containers, unused networks, dangling images
docker system prune -a

# Remove all containers
docker rm -f $(docker ps -aq)

# Remove all images
docker rmi -f $(docker images -q)

# Remove all volumes
docker volume prune

10. Docker Hub & Registry

ActionCommand
Logindocker login
Logoutdocker logout
Pushdocker push username/app:tag
Run from privatedocker run username/app:tag

11. Debug & Inspect

ToolCommand
Container processesdocker top web
Resource usagedocker stats
Events streamdocker events
Image historydocker history nginx
Diff (changed files)docker diff web
Export containerdocker export web > web.tar
Import as imagecat web.tar | docker import - newimage

12. One-Liners

# Run Ubuntu with bash
docker run -it --rm ubuntu bash

# Quick web server
docker run -d -p 80:80 --name web --restart always nginx

# Shell into running container
docker exec -it web sh

# Build and run in one line
docker build -t myapp . && docker run -p 5000:5000 myapp

# Cleanup everything
docker system prune -a --volumes

# Monitor logs + stats
docker stats & docker logs -f web

13. Best Practices

  • Use official images when possible
  • Use specific tags (nginx:1.25), avoid :latest in prod
  • Multi-stage builds for smaller images
  • .dockerignore to speed up builds
  • Non-root user in Dockerfile:
  RUN useradd -m appuser
  USER appuser
  • Scan images: trivy image nginx

14. Useful Tools

# Install
sudo apt install docker-compose-plugin trivy

# Scan for vulnerabilities
trivy image python:3.11

# Dive into image layers
dive nginx:latest

15. Docker Desktop (GUI)

  • Linux: Install via docs.docker.com
  • Features: GUI, Kubernetes, Compose, Volume management

16. Kubernetes Quick Start (kind)

# Install kind
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x kind && sudo mv kind /usr/local/bin/

# Create cluster
kind create cluster --name mycluster

# Use kubectl
kubectl get nodes

Resources