화. 8월 12th, 2025

G: “

Hello, fellow developers and DevOps enthusiasts! 👋 Have you ever found yourself in the middle of a project, scratching your head trying to remember that one specific Docker command? Or perhaps you’re just starting your containerization journey and feel overwhelmed by the sheer number of commands? You’re not alone!

Docker has become an indispensable tool in modern software development, streamlining deployment, scalability, and environment consistency. But mastering its vast array of commands can take time. That’s why I’ve put together this comprehensive cheat sheet, designed for immediate use in real-world scenarios. We’ll cover everything from basic container operations to advanced image and network management, all with clear examples and practical tips. Let’s dive in and unlock the power of Docker! 🚀


Table of Contents

  1. Getting Started: Docker Basics & Information ℹ️
  2. Image Management: Building & Storing Your Apps 📦
  3. Container Management: Running & Interacting with Your Apps 🏃‍♂️
  4. Network Management: Connecting Your Containers 🔗
  5. Volume Management: Persistent Data Storage 💾
  6. Docker Compose: Orchestrating Multi-Container Applications 🛠️
  7. Cleaning Up: Keeping Your System Tidy 🧹
  8. Pro Tips for Efficient Docker Use

1. Getting Started: Docker Basics & Information ℹ️

Before we dive deep, let’s cover some fundamental commands to get familiar with your Docker environment.

  • docker version: Displays Docker client and server version information.

    • Purpose: Check your Docker installation and its components.
    • Example:
      docker version
    • Tip: Useful for debugging compatibility issues.
  • docker info: Shows detailed information about the Docker daemon.

    • Purpose: Get an overview of your Docker setup, including containers, images, storage driver, and runtime.
    • Example:
      docker info
    • Tip: Great for checking resource usage and daemon configuration.
  • docker login: Log in to a Docker registry (e.g., Docker Hub).

    • Purpose: Authenticate to pull private images or push your own images.
    • Example:
      docker login # Follow prompts for username and password
    • Tip: For private registries, specify the server: docker login my.private.registry.com.
  • docker logout: Log out from a Docker registry.

    • Purpose: End your session with a Docker registry.
    • Example:
      docker logout

2. Image Management: Building & Storing Your Apps 📦

Images are the blueprints of your applications. They contain everything needed to run your software, including the code, runtime, libraries, and dependencies.

  • docker pull [image_name]:[tag]: Downloads an image from a registry.

    • Purpose: Get pre-built images (e.g., official OS images, databases, web servers).
    • Example:
      docker pull ubuntu:latest # Pulls the latest Ubuntu image
      docker pull nginx:1.23.0 # Pulls a specific Nginx version
    • Tip: Always specify a tag! latest can be unpredictable.
  • docker build -t [image_name]:[tag] .: Builds an image from a Dockerfile.

    • Purpose: Create your own custom images based on your application code and configuration.
    • Example: Assuming you have a Dockerfile in your current directory:
      docker build -t my-web-app:1.0 .
    • Tip: The . at the end is crucial; it specifies the build context (where to find the Dockerfile and source code). Use --no-cache to force a fresh build.
  • docker images or docker image ls: Lists all locally stored images.

    • Purpose: See what images you have on your machine.
    • Example:
      docker images
      # Or with more detail:
      docker image ls -a # Lists all, including intermediate images
    • Tip: Use docker images -f "dangling=true" to find unused intermediate images (good candidates for cleanup).
  • docker rmi [image_id or image_name]:[tag]: Removes one or more images.

    • Purpose: Free up disk space by deleting unwanted images.
    • Example:
      docker rmi my-web-app:1.0
      docker rmi 9a4c8f5d0e1b # Remove by image ID
    • Tip: Use -f or --force to remove an image even if it’s being used by a container (be careful!).
  • docker tag [source_image]:[source_tag] [target_image]:[target_tag]: Tags an image.

    • Purpose: Create an alias for an existing image, often used before pushing to a registry.
    • Example:
      docker tag my-web-app:1.0 myregistry.com/my-web-app:production
  • docker push [image_name]:[tag]: Pushes an image to a registry.

    • Purpose: Share your custom images with others or deploy them to production.
    • Example:
      docker push myregistry.com/my-web-app:production
    • Tip: You must be logged in to the target registry for this to work.

3. Container Management: Running & Interacting with Your Apps 🏃‍♂️

Containers are running instances of images. This section covers the most frequently used commands for managing their lifecycle.

  • docker run [options] [image_name]:[tag] [command]: Creates and starts a new container. This is arguably the most important command!

    • Purpose: Bring your image to life as a running application.
    • Key Options:
      • -d (detached): Run in the background.
      • -p [host_port]:[container_port] (publish): Map a port from the container to the host.
      • --name [container_name] (name): Give your container a memorable name.
      • -v [host_path]:[container_path] (volume): Mount a host directory or named volume into the container (for persistent data).
      • -e [ENV_VAR]=[value] (env): Set environment variables inside the container.
      • --rm (remove): Automatically remove the container when it exits.
      • -it (interactive + tty): Run in interactive mode with a pseudo-TTY (for shell access).
    • Examples:
      • Run Nginx in detached mode, mapping port 8080 on host to 80 in container:
        docker run -d -p 8080:80 --name my-nginx nginx:latest
      • Run a one-off Ubuntu container to check its version, then remove it:
        docker run --rm ubuntu:latest cat /etc/os-release
      • Run a container interactively and get a bash shell:
        docker run -it --name my-interactive-ubuntu ubuntu:latest bash
  • docker ps or docker container ls: Lists running containers.

    • Purpose: See which containers are currently active.
    • Example:
      docker ps
      # Show all containers (running and stopped):
      docker ps -a
      # Show container sizes:
      docker ps -s
    • Tip: Use -a frequently to see recently stopped containers that might need cleanup.
  • docker start [container_id or container_name]: Starts one or more stopped containers.

    • Purpose: Resume a container without creating a new one.
    • Example:
      docker start my-nginx
  • docker stop [container_id or container_name]: Gracefully stops one or more running containers.

    • Purpose: Send a SIGTERM signal to the container’s main process, allowing it to shut down cleanly.
    • Example:
      docker stop my-nginx
    • Tip: You can specify a timeout using -t or --time (e.g., docker stop -t 5 my-nginx).
  • docker restart [container_id or container_name]: Stops and then starts one or more containers.

    • Purpose: Quick way to refresh a container’s state.
    • Example:
      docker restart my-nginx
  • docker kill [container_id or container_name]: Forcefully stops one or more running containers.

    • Purpose: Send a SIGKILL signal, immediately terminating the container. Use when docker stop fails.
    • Example:
      docker kill my-nginx
  • docker rm [container_id or container_name]: Removes one or more stopped containers.

    • Purpose: Delete containers to free up resources.
    • Example:
      docker rm my-nginx
      # Force remove a running container (use with caution):
      docker rm -f my-running-container
    • Tip: You can remove multiple containers at once: docker rm container1 container2.
  • docker logs [container_id or container_name]: Fetches the logs of a container.

    • Purpose: Debug issues or monitor container output.
    • Example:
      docker logs my-nginx
      # Follow logs in real-time:
      docker logs -f my-nginx
      # Show last N lines:
      docker logs --tail 100 my-nginx
    • Tip: -f is your best friend for live debugging!
  • docker exec -it [container_id or container_name] [command]: Runs a command inside a running container.

    • Purpose: Access a container’s shell, run diagnostic tools, or modify files temporarily.
    • Example:
      • Get a bash shell inside a running container:
        docker exec -it my-web-app bash
      • List files in a specific directory within a container:
        docker exec my-nginx ls /etc/nginx/conf.d
    • Tip: -it is essential for interactive shells.
  • docker inspect [container_id or image_id]: Displays detailed low-level information about Docker objects.

    • Purpose: Get JSON-formatted data about a container, image, volume, or network, including configuration, network settings, and mounted volumes.
    • Example:
      docker inspect my-nginx
      docker inspect my-web-app:1.0
    • Tip: Use jq to parse the JSON output for specific information, e.g., docker inspect my-nginx | jq '.[0].NetworkSettings.IPAddress'.
  • docker cp [source_path] [container_id]:[destination_path]: Copies files/folders between a container and the local filesystem.

    • Purpose: Get logs out, put config files in, or inspect generated data.
    • Examples:
      • Copy file from host to container:
        docker cp ./local_file.txt my-nginx:/etc/nginx/conf.d/
      • Copy file from container to host:
        docker cp my-nginx:/var/log/nginx/access.log ./nginx_access.log

4. Network Management: Connecting Your Containers 🔗

Docker networking allows containers to communicate with each other and with the outside world.

  • docker network ls: Lists all Docker networks.

    • Purpose: See available networks (bridge, host, none, custom ones).
    • Example:
      docker network ls
  • docker network create [network_name]: Creates a new custom bridge network.

    • Purpose: Isolate applications or allow specific communication patterns between groups of containers.
    • Example:
      docker network create my-app-network
    • Tip: Containers on the same custom bridge network can resolve each other by name (DNS resolution).
  • docker network connect [network_name] [container_id or container_name]: Connects a running container to a network.

    • Purpose: Add a container to an existing network.
    • Example:
      docker network connect my-app-network my-nginx
  • docker network disconnect [network_name] [container_id or container_name]: Disconnects a container from a network.

    • Purpose: Remove a container from a network.
    • Example:
      docker network disconnect bridge my-nginx
  • docker network inspect [network_name]: Displays detailed information about a network.

    • Purpose: See which containers are attached to a network, its subnet, gateway, etc.
    • Example:
      docker network inspect my-app-network
  • docker network rm [network_name]: Removes one or more networks.

    • Purpose: Clean up unused networks.
    • Example:
      docker network rm my-app-network

5. Volume Management: Persistent Data Storage 💾

Containers are stateless by default. Volumes provide a way to store data persistently, independent of the container’s lifecycle.

  • docker volume ls: Lists all Docker volumes.

    • Purpose: See what volumes are available on your system.
    • Example:
      docker volume ls
  • docker volume create [volume_name]: Creates a named volume.

    • Purpose: Explicitly create a volume before using it.
    • Example:
      docker volume create my-app-data
  • docker run -v [volume_name]:[container_path] ...: Mounts a named volume to a container.

    • Purpose: Persist data generated by or used by the container.
    • Example:
      docker run -d -p 80:80 -v my-app-data:/var/www/html --name my-web-with-data my-web-app:1.0
  • docker volume inspect [volume_name]: Displays detailed information about a volume.

    • Purpose: Check the volume’s mount point on the host, driver, etc.
    • Example:
      docker volume inspect my-app-data
  • docker volume rm [volume_name]: Removes one or more volumes.

    • Purpose: Delete volumes and the data stored within them (use with caution!).
    • Example:
      docker volume rm my-app-data
    • Tip: Volumes are not automatically removed when containers are removed unless you specify --rm and the volume is created inline (e.g., -v /data without a named volume). Always manually remove named volumes you no longer need.

6. Docker Compose: Orchestrating Multi-Container Applications 🛠️

For applications with multiple services (e.g., a web app, a database, and a cache), Docker Compose is your best friend. It uses a docker-compose.yml file to define and run multi-container Docker applications.

  • docker compose up: Builds, creates, starts, and attaches to containers defined in docker-compose.yml.

    • Purpose: Bring up your entire multi-service application with a single command.
    • Example: (In a directory with docker-compose.yml)
      docker compose up
      # Run in detached mode:
      docker compose up -d
      # Build images before starting:
      docker compose up --build
    • Tip: docker compose up -d --build is a very common command during development!
  • docker compose down: Stops and removes containers, networks, and volumes created by up.

    • Purpose: Shut down and clean up your Compose application.
    • Example:
      docker compose down
      # Also remove named volumes:
      docker compose down --volumes
    • Tip: Always use --volumes when you want a clean slate and don’t care about the data in named volumes.
  • docker compose ps: Lists containers in the current Compose project.

    • Purpose: See the status of your Compose services.
    • Example:
      docker compose ps
  • docker compose logs [service_name]: Views output from services.

    • Purpose: Aggregate and view logs from all or specific services.
    • Example:
      docker compose logs
      # Follow logs from a specific service:
      docker compose logs -f web
  • docker compose exec [service_name] [command]: Executes a command in a running service container.

    • Purpose: Similar to docker exec, but for a service managed by Compose.
    • Example:
      docker compose exec web bash

7. Cleaning Up: Keeping Your System Tidy 🧹

Docker can consume a lot of disk space over time with old images, stopped containers, and unused volumes. Regular cleanup is essential!

  • docker system prune: Removes all stopped containers, all dangling images, and all unused networks.

    • Purpose: A powerful single command to free up significant disk space.
    • Example:
      docker system prune
      # Also remove all unused volumes:
      docker system prune --volumes
    • Warning: This command is aggressive! Always understand what it will remove before executing.
  • docker container prune: Removes all stopped containers.

    • Purpose: Target only stopped containers.
    • Example:
      docker container prune
  • docker image prune: Removes dangling images (those not associated with any named tags).

    • Purpose: Free up space from intermediate build layers.
    • Example:
      docker image prune
      # Remove all unused images, not just dangling ones:
      docker image prune -a
    • Warning: -a will remove all images not associated with a container.
  • docker volume prune: Removes all unused volumes.

    • Purpose: Free up disk space used by volumes that no longer have attached containers.
    • Example:
      docker volume prune
  • docker network prune: Removes all unused networks.

    • Purpose: Clean up custom networks that are no longer in use.
    • Example:
      docker network prune

8. Pro Tips for Efficient Docker Use ✨

  • Always Specify Tags: Avoid latest for production. Pin your images to specific versions (e.g., node:16-alpine, nginx:1.23.0). This ensures reproducibility.
  • Use .dockerignore: Similar to .gitignore, this file specifies files and directories to exclude from the build context. This dramatically speeds up docker build and keeps your images small.
  • Leverage Multi-Stage Builds: For compiled languages (Go, Java, Node.js), use multi-stage builds in your Dockerfile to create extremely lean final images that only contain the necessary runtime artifacts, not the build tools.
  • Understand ENTRYPOINT vs. CMD:
    • CMD: Provides defaults for an executing container. Can be overridden.
    • ENTRYPOINT: Configures a container that will run as an executable. Often used with CMD as arguments.
  • Automate with docker-compose: For anything beyond a single container, docker-compose is invaluable. It makes development, testing, and even single-host deployment much smoother.
  • Monitor Resources: Use docker stats [container_id or container_name] to see real-time CPU, memory, network I/O, and disk I/O of your running containers.
  • Know Your docker inspect: When debugging, docker inspect provides a treasure trove of information about container and image configurations. Pipe it to jq for easier parsing!

Conclusion ✅

You now have a powerful cheat sheet at your fingertips to navigate the world of Docker! From building your first image to managing complex multi-container applications and keeping your system clean, these commands are your daily drivers in the containerized landscape.

The best way to master these commands is to practice, practice, practice! Set up a small project, experiment with different options, and watch how Docker transforms your development workflow.

Happy containerizing! 🚀 If you have any favorite commands or pro tips not listed here, feel free to share them in the comments!

답글 남기기

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