화. 8월 12th, 2025

D: 🚀 Whether you’re a beginner or an experienced developer, this Docker cheat sheet will be your ultimate guide! Bookmark this page for quick reference to the most essential Docker commands with real-world examples.


🔧 1. Container Lifecycle Management

▶️ Run a Container

docker run [OPTIONS] IMAGE [COMMAND]
  • Example:
    docker run -d -p 8080:80 --name my_nginx nginx
    • -d: Detached mode (runs in background)
    • -p 8080:80: Maps host port 8080 → container port 80
    • --name: Assigns a custom name

⏹️ Stop/Kill Containers

docker stop CONTAINER_ID   # Graceful shutdown
docker kill CONTAINER_ID   # Force immediate stop

🔄 Restart/Remove

docker restart CONTAINER_ID
docker rm CONTAINER_ID     # Remove stopped container
docker rm -f CONTAINER_ID  # Force remove running container

📦 2. Image Management

🔍 List Images

docker images
docker image ls  # Alternative syntax

🏗️ Build an Image

docker build -t my_app:1.0 .
  • -t: Tags the image (format: name:tag)
  • .: Build context (Dockerfile location)

🗑️ Delete Images

docker rmi IMAGE_ID
docker image prune  # Remove unused images

👀 3. Monitoring & Debugging

📊 List Containers

docker ps          # Running containers
docker ps -a       # All containers (including stopped)

📝 View Logs

docker logs CONTAINER_ID
docker logs -f CONTAINER_ID  # Follow live logs (like tail -f)

🔍 Inspect Containers/Images

docker inspect CONTAINER_ID  # Detailed JSON config
docker stats                 # Live resource usage (CPU/MEM)

🌐 4. Networking

🌉 Create a Network

docker network create my_network

🔗 Connect Containers

docker run --net=my_network --name app1 my_image
docker run --net=my_network --name app2 my_image
  • Containers app1 and app2 can now communicate via hostnames!

Port Forwarding

docker run -p 3306:3306 mysql  # Host:Container port mapping

💾 5. Data Persistence (Volumes)

📂 Create a Volume

docker volume create my_volume

📌 Mount a Volume

docker run -v my_volume:/app/data my_image
  • Persists data even after container deletion.

📋 List Volumes

docker volume ls

🛠️ Bonus: Pro Tips

  1. Clean Up Everything 🧹:

    docker system prune -a --volumes
    • Removes stopped containers, unused networks, dangling images, and volumes.
  2. Run Interactive Shell 💻:

    docker exec -it CONTAINER_ID /bin/bash
    • Debug containers by accessing their shell.
  3. Update All Images 🔄:

    docker images | awk 'NR>1 {print $1}' | xargs -L1 docker pull

🎉 Now you’re a Docker pro! Bookmark this cheat sheet and share it with your team. For more details, check the official Docker docs.

💬 Got questions? Drop them in the comments below! 👇

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다