# curl https://devtools.krishanchawla.com/raw/docker-cheatsheet.txt DOCKER CHEATSHEET Enterprise-ready Docker commands, patterns, and best practices for development and production environments Category: DevOps · Type: cheatsheet Official docs: https://docs.docker.com/ ──────────────────────────────────────────────────────────── ## What is Docker? **Docker** is an open-source containerization platform that packages applications and their dependencies into lightweight, portable containers. It ensures consistent behavior across development, testing, and production environments. **⚡ Key Features** - Lightweight container runtime - Immutable application images - Environment consistency - Fast startup and scalability **🎯 Use Cases** - Microservices architecture - CI/CD pipelines - Cloud-native deployments - Local dev parity with production ## 🛠️ Interactive Command Builder Build a `docker run` command without memorizing flag order — fill in the fields below and copy the result. ## 📦 Docker - Install & Runtime Info Verify Docker installation and daemon status. ```bash docker --version docker info ``` ## 🐳 Docker - Run Containers Run container in detached mode with port mapping. ```bash docker run -d -p 8080:8080 --name my-app my-app:1.0 ``` Run with environment variables. ```bash docker run -d \ -e SPRING_PROFILES_ACTIVE=prod \ -p 8080:8080 \ my-app:1.0 ``` ## 🔍 Docker - List, Inspect & Logs List containers. ```bash docker ps docker ps -a ``` Inspect container. ```bash docker inspect my-app ``` View logs. ```bash docker logs my-app docker logs -f my-app ``` ## ⏸️ Docker - Start, Stop & Restart ```bash docker stop my-app docker start my-app docker restart my-app ``` Graceful stop. ```bash docker stop -t 30 my-app ``` ## 📦 Docker - Image Lifecycle Build and list images. ```bash docker build -t my-app:1.0 . docker images ``` Remove images. ```bash docker rmi my-app:1.0 docker image prune -a ``` ## 📂 Docker - Volumes & Persistence Create and attach volumes. ```bash docker volume create app-data docker run -d -v app-data:/var/lib/app my-app:1.0 ``` ## 🌐 Docker - Networking Basics ```bash docker network ls docker network create app-network ``` Attach network. ```bash docker run -d --network app-network my-app:1.0 ``` ## 🧱 Dockerfile - Best Practices Minimal production Dockerfile. ```dockerfile FROM eclipse-temurin:17-jre-alpine WORKDIR /app COPY target/app.jar app.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","app.jar"] ``` ## ⚡ Dockerfile - Multi Stage Build ```dockerfile FROM maven:3.9-eclipse-temurin-17 AS build WORKDIR /build COPY . . RUN mvn clean package -DskipTests FROM eclipse-temurin:17-jre-alpine WORKDIR /app COPY --from=build /build/target/app.jar app.jar ENTRYPOINT ["java","-jar","app.jar"] ``` ## 🔒 Docker - Run as Non Root ```dockerfile RUN addgroup -S app && adduser -S app -G app USER app ``` ## 🛡️ Docker - Read Only Containers ```bash docker run -d \ --read-only \ --tmpfs /tmp \ my-app:1.0 ``` ## 🔐 Docker - Secrets Handling ```bash docker run -d \ -e DB_PASSWORD_FILE=/run/secrets/db_password \ my-app:1.0 ``` ## 🔍 Docker - Image Vulnerability Scan ```bash docker scan my-app:1.0 trivy image my-app:1.0 ``` ## 📊 Docker - Resource Monitoring ```bash docker stats ``` ## ⏱️ Docker - CPU & Memory Limits ```bash docker run -d \ --memory=512m \ --cpus=1.5 \ my-app:1.0 ``` ## 🚨 Docker - Restart Policies ```bash docker run -d \ --restart unless-stopped \ my-app:1.0 ``` ## 🚀 Docker - CI Build Pattern ```bash docker build -t my-app:${GIT_COMMIT} . docker tag my-app:${GIT_COMMIT} my-app:latest ``` ## 🔁 Docker - Tagging Strategy ``` my-app:1.2.4 my-app:1.2 my-app:latest my-app:git- ``` ## 🧪 Docker - Test Inside Container ```bash docker run --rm my-app:test mvn test ``` ## 🧰 Docker Compose - Basics ```bash docker compose up -d docker compose down ``` ```yaml services: app: image: my-app ports: - "8080:8080" ``` ## 🧰 Docker Compose - Profiles ```yaml services: app: profiles: ["prod"] mock: profiles: ["dev"] ``` ```bash docker compose --profile dev up ``` ## 🚫 Troubleshoot - Restart Loop ```bash docker logs my-app docker inspect my-app --format='{{.State.ExitCode}}' ``` ## 🚫 Troubleshoot - Disk Full ```bash docker system df docker system prune -a ``` ## 💡 Enterprise Docker Golden Rules - One container = one responsibility - Never store secrets in images - Always define resource limits - Logs go to stdout/stderr - Immutable builds only - Scan images in CI - Delete before debugging ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/docker-cheatsheet More tools: https://devtools.krishanchawla.com