화. 8월 12th, 2025

G: “

Hey there, fellow tech explorers! 🚀 Have you ever heard about Docker and felt a mix of excitement and overwhelm? You’re not alone! Docker has revolutionized how we build, ship, and run applications, making development workflows smoother and more consistent. It truly is a game-changer!

But let’s be honest, diving into Docker can feel like learning a new language, especially when you’re faced with a seemingly endless list of commands. Fear not! This comprehensive guide is designed to be your compass, navigating you through the essential Docker commands, from the absolute basics to more advanced maneuvers. By the end of this journey, you’ll be confidently managing images, containers, volumes, and networks like a seasoned pro.

Let’s embark on this exciting adventure! 💡


1. Getting Started: The Foundation 🏗️

Before you can build anything spectacular, you need to ensure your tools are set up and ready. These commands help you confirm your Docker installation and get basic information.

  • docker --version:

    • What it does: Displays the Docker version installed on your system. It’s your quick check to see if Docker is ready for action! ✅
    • Example:
      docker --version
      # Docker version 24.0.7, build afdd53b
  • docker info:

    • What it does: Provides detailed information about your Docker installation, including the number of containers, images, storage driver, and more. Great for troubleshooting! 🧐
    • Example:
      docker info
      # Client:
      #  Context:    default
      #  Debug Mode: false
      #  ...
      # Server:
      #  Containers: 5
      #   Running: 2
      #   Paused: 0
      #   Stopped: 3
      #  Images: 12
      #  ...

2. Image Management: The Building Blocks 🧱

Docker images are read-only templates used to create containers. Think of them as blueprints for your applications. Mastering image commands is fundamental.

  • **`docker pull

    [:]`**: * **What it does:** Downloads an image from Docker Hub (the default registry) or a specified registry to your local machine. If no tag is specified, it pulls the `latest` version. ⏬ * **Example:** “`bash docker pull ubuntu # Pulls the latest Ubuntu image docker pull nginx:1.23.0 # Pulls a specific Nginx version “`
  • docker images / docker image ls:

    • What it does: Lists all images stored on your local machine. You’ll see their names, tags, IDs, creation date, and size. 📸
    • Example:
      docker images
      # REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
      # ubuntu        latest    b3e215f7957e   2 weeks ago    77.8MB
      # nginx         1.23.0    e799292b322a   2 months ago   142MB
  • docker rmi <image_id/image_name:tag>:

    • What it does: Removes one or more images from your local storage. You can specify by ID or by name:tag. Be careful, you can’t remove an image if a container is using it! 🗑️
    • Example:
      docker rmi b3e215f7957e     # Remove by ID
      docker rmi nginx:1.23.0     # Remove by name and tag
  • **`docker build -t

    [:] `**: * **What it does:** Builds a new Docker image from a `Dockerfile` and a “context” (usually the current directory `.` where the Dockerfile is located). The `-t` flag tags the image with a name and optionally a tag. 🔨 * **Example:** (Assuming you have a `Dockerfile` in the current directory) “`bash docker build -t my-custom-app:1.0 . # Sending build context to Docker daemon 2.048kB # Step 1/4 : FROM ubuntu:latest # … (build output) “`
  • docker history <image_id/image_name:tag>:

    • What it does: Shows the history of an image, revealing the commands used to build each layer. Useful for understanding how an image was constructed. 📜
    • Example:
      docker history nginx:latest
      # IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
      # 168b556f8573   2 months ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon ...   0B
      # 
      <missing>      2 months ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B
      # ...

3. Container Management: Orchestrating Your Applications 🚀

Containers are runnable instances of images. This is where your applications come to life!

  • **`docker run [OPTIONS]

    [:] [COMMAND] [ARG…]`**: * **What it does:** Creates and starts a new container from a specified image. This is arguably the most frequently used command! You can add many options to control its behavior. 🌟 * **Key Options:** * `-d`, `–detach`: Runs the container in the background (detached mode). * `-p :`: Publishes (maps) a container’s port to a host port. * `–name `: Assigns a custom name to your container (makes it easier to reference). * `-it`: Runs the container in interactive mode with a pseudo-TTY, often used for shell access. * `-v :`: Mounts a volume (for persistent data). * `–rm`: Automatically remove the container when it exits. * **Examples:** “`bash docker run -d -p 80:80 –name my-nginx-webserver nginx # Runs an Nginx container in the background, mapping host port 80 to container port 80, and names it ‘my-nginx-webserver’. docker run -it –rm ubuntu bash # Runs an Ubuntu container interactively, giving you a bash shell inside it, and automatically removes it when you exit. “`
  • docker ps / docker container ls:

    • What it does: Lists all currently running containers. By default, it only shows active ones. 📋
    • Example:
      docker ps
      # CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
      # a1b2c3d4e5f6   nginx     "nginx -g 'daemon of…"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp   my-nginx-webserver
  • docker ps -a / docker container ls -a:

    • What it does: Lists all containers, including those that are stopped or exited. Super useful for seeing what’s accumulated! 🧐
    • Example:
      docker ps -a
      # CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                     PORTS                NAMES
      # a1b2c3d4e5f6   nginx     "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes               0.0.0.0:80->80/tcp   my-nginx-webserver
      # f7e6d5c4b3a2   ubuntu    "bash"                   About an hour ago   Exited (0) 5 minutes ago                        my-stopped-ubuntu
  • docker start <container_id/name>:

    • What it does: Starts one or more stopped containers. 🟢
    • Example:
      docker start my-nginx-webserver
  • docker stop <container_id/name>:

    • What it does: Gracefully stops one or more running containers. Docker sends a SIGTERM signal, giving the application a chance to shut down cleanly. 🔴
    • Example:
      docker stop my-nginx-webserver
  • docker restart <container_id/name>:

    • What it does: Stops and then starts a container. Useful for applying configuration changes or simply refreshing. 🔄
    • Example:
      docker restart my-nginx-webserver
  • docker rm <container_id/name>:

    • What it does: Removes one or more stopped containers. You cannot remove a running container unless you use the -f (force) flag. 💀
    • Example:
      docker rm my-stopped-ubuntu
      docker rm -f my-nginx-webserver # Force remove a running container
  • docker kill <container_id/name>:

    • What it does: Forcefully stops a running container immediately by sending a SIGKILL signal. Use this when docker stop doesn’t work or you need an instant shutdown. 🔥
    • Example:
      docker kill my-nginx-webserver

4. Container Interaction & Inspection: Peeking Inside 👀

These commands allow you to interact with running containers, inspect their state, and troubleshoot issues.

  • docker logs <container_id/name>:

    • What it does: Fetches the logs of a container. Very helpful for debugging! You can also use -f to follow logs in real-time, or `–tail ` to see only the last N lines. 📜
    • Example:
      docker logs my-nginx-webserver
      docker logs -f my-nginx-webserver # Follow logs
      docker logs --tail 50 my-nginx-webserver # Last 50 lines
  • docker exec -it <container_id/name> <command>:

    • What it does: Executes a command inside a running container. The -it flags are commonly used to open an interactive terminal session (e.g., a bash shell). 💻
    • Example:
      docker exec -it my-nginx-webserver bash  # Get a shell inside the Nginx container
      docker exec my-nginx-webserver ls -l /usr/share/nginx/html # List files in Nginx webroot
  • docker inspect <container_id/name/image_id>:

    • What it does: Returns detailed low-level information about Docker objects (containers, images, volumes, networks) in JSON format. Invaluable for deep dives and debugging! 🔍
    • Example:
      docker inspect my-nginx-webserver
      # [
      #     {
      #         "Id": "a1b2c3d4e5f6...",
      #         "Created": "2023-10-27T10:00:00.123456789Z",
      #         "Path": "nginx",
      #         "Args": [
      #             "-g",
      #             "daemon off;"
      #         ],
      #         "State": {
      #             "Status": "running",
      #             "Running": true,
      #             "Paused": false,
      #             "Restarting": false,
      #             ...
      #         },
      #         "Config": { ... },
      #         "NetworkSettings": { ... },
      #         ...
      #     }
      # ]
  • docker top <container_id/name>:

    • What it does: Displays the running processes within a container. Similar to the top command on Linux systems. 📊
    • Example:
      docker top my-nginx-webserver
      # UID    PID    PPID   C   STIME   TTY   TIME     CMD
      # root   1      0      0   10:00   ?     00:00:00 nginx: master process nginx -g daemon off;
      # 101    7      1      0   10:00   ?     00:00:00 nginx: worker process
  • docker stats:

    • What it does: Displays a live stream of resource usage statistics (CPU, memory, network I/O, disk I/O) for all running containers. Great for monitoring performance! 📈
    • Example:
      docker stats
      # CONTAINER ID   NAME                   CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
      # a1b2c3d4e5f6   my-nginx-webserver     0.00%     4.125MiB / 1.936GiB   0.21%     1.51MB / 1.34MB   0B / 0B           7
  • **`docker cp

    :` / `docker cp : `**: * **What it does:** Copies files or folders between a container and the local filesystem. 📂 * **Example:** “`bash docker cp ./local_file.txt my-nginx-webserver:/usr/share/nginx/html/ # Copies local_file.txt into the container’s Nginx webroot. docker cp my-nginx-webserver:/var/log/nginx/access.log ./access_logs/ # Copies the Nginx access log from the container to a local folder. “`

5. Data Management: Persistent Storage with Volumes 💾

Containers are ephemeral, meaning data inside them is lost when the container is removed. Volumes provide a way to store data persistently, independent of the container’s lifecycle.

  • **`docker volume create

    `**: * **What it does:** Creates a named Docker volume. These are managed by Docker and are the preferred way to persist data. 📚 * **Example:** “`bash docker volume create my-app-data “`
  • docker volume ls:

    • What it does: Lists all Docker volumes on your system. 🔗
    • Example:
      docker volume ls
      # DRIVER    VOLUME NAME
      # local     my-app-data
      # local     another-volume
  • **`docker volume inspect

    `**: * **What it does:** Provides detailed information about a specific volume, including its mount point on the host. 🔍 * **Example:** “`bash docker volume inspect my-app-data # [ # { # “CreatedAt”: “2023-10-27T10:30:00Z”, # “Driver”: “local”, # “Labels”: {}, # “Mountpoint”: “/var/lib/docker/volumes/my-app-data/_data”, # “Name”: “my-app-data”, # “Options”: {}, # “Scope”: “local” # } # ] “`
  • **`docker volume rm

    `**: * **What it does:** Removes one or more named volumes. Be careful, this deletes the data! ♻️ * **Example:** “`bash docker volume rm my-app-data “`
  • Using volumes with docker run:

    • What it does: The -v flag is used to mount volumes (either named volumes or bind mounts from the host filesystem) into your containers.
    • Example (Named Volume):
      docker run -d -p 80:80 -v my-app-data:/usr/share/nginx/html --name my-nginx-persistent nginx
      # Mounts the 'my-app-data' named volume to Nginx's web root, making content persistent.
    • Example (Bind Mount):
      docker run -d -p 80:80 -v $(pwd)/html_files:/usr/share/nginx/html --name my-nginx-bind-mount nginx
      # Mounts the local 'html_files' directory into the container.

6. Network Management: Connecting the Dots 🌐

Docker containers can communicate with each other and the outside world through networks. Understanding how to manage them is key for multi-container applications.

  • docker network ls:

    • What it does: Lists all Docker networks on your system. You’ll usually see bridge, host, and none by default. 🗺️
    • Example:
      docker network ls
      # NETWORK ID     NAME         DRIVER    SCOPE
      # a1b2c3d4e5f6   bridge       bridge    local
      # f7e6d5c4b3a2   host         host      local
      # c9d8e7f6a5b4   none         null      local
  • **`docker network create

    `**: * **What it does:** Creates a new user-defined bridge network. Custom networks offer better isolation and service discovery compared to the default `bridge` network. 🤝 * **Example:** “`bash docker network create my-app-network “`
  • **`docker network inspect

    `**: * **What it does:** Provides detailed information about a specific network, including connected containers and their IP addresses. 🔍 * **Example:** “`bash docker network inspect my-app-network # [ # { # “Name”: “my-app-network”, # “Id”: “…”, # “Scope”: “local”, # “Driver”: “bridge”, # “Containers”: { # “a1b2c3d4e5f6”: { # “Name”: “my-app-db”, # “IPv4Address”: “172.18.0.2/16”, # … # } # }, # … # } # ] “`
  • **`docker network connect

    `**: * **What it does:** Connects a running container to a specified network. Containers on the same custom network can communicate by container name. 🔗 * **Example:** “`bash docker run -d –name my-app-db –network my-app-network postgres docker run -d –name my-app-backend –network my-app-network my-backend-image # Now ‘my-app-backend’ can reach ‘my-app-db’ by using the hostname ‘my-app-db’. “`
  • **`docker network disconnect

    `**: * **What it does:** Disconnects a container from a network. ✂️ * **Example:** “`bash docker network disconnect bridge my-nginx-webserver “`

7. System Management & Cleanup: Keeping Things Tidy 🧹

Docker can consume a lot of disk space over time with unused images, stopped containers, and volumes. These commands help you manage and clean up your Docker environment.

  • docker system df:

    • What it does: Shows Docker disk usage, broken down by images, containers, and local volumes. Helps you identify where space is being consumed. 📊
    • Example:
      docker system df
      # TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
      # Images            12        5         1.2GB     800MB (66%)
      # Containers        10        3         200MB     150MB (75%)
      # Local Volumes     4         2         50MB      25MB (50%)
      # Build Cache       0         0         0B        0B
  • docker system prune:

    • What it does: Removes all stopped containers, all dangling images (images not associated with any container), and all unused networks. This is a powerful cleanup command! 💪
    • Example:
      docker system prune
      # WARNING! This will remove:
      #   - all stopped containers
      #   - all networks not used by at least one container
      #   - all dangling images
      #   - all build cache
      # Are you sure you want to continue? [y/N] y
      # ... (cleanup details)
    • Additional Cleanup Options:
      • docker system prune -a or docker system prune --all: Removes all unused images (not just dangling ones) and all build cache. Use with caution! ⚠️
      • docker system prune --volumes: Also removes all unused volumes. Extremely powerful, as it deletes persistent data! Use only when you are certain. 🚨

8. Interacting with Registries: Sharing Your Creations ☁️

Docker Hub is the public registry for Docker images. You can also use private registries. These commands help you interact with them to pull, push, and manage your images.

  • docker login [server]:

    • What it does: Logs you into a Docker registry. If no server is specified, it defaults to Docker Hub. You’ll be prompted for your username and password. 🔑
    • Example:
      docker login
      # Login with your Docker ID to push and pull images, scan images for vulnerabilities, and more.
      # Username: yourusername
      # Password:
      # Login Succeeded
  • **`docker push

    [:]`**: * **What it does:** Pushes an image to a configured registry (e.g., Docker Hub). Before pushing, you usually need to `tag` your image with your registry username or the full registry path. ⬆️ * **Example:** “`bash docker tag my-custom-app:1.0 yourusername/my-custom-app:1.0 docker push yourusername/my-custom-app:1.0 # The push refers to repository [docker.io/yourusername/my-custom-app] # … (push progress) “`
  • docker logout [server]:

    • What it does: Logs you out of a Docker registry. 🚪
    • Example:
      docker logout

9. Beyond Basics: Docker Compose (A Glimpse) 🎬

While not a single command, docker-compose is an indispensable tool for managing multi-container Docker applications. It allows you to define and run a complex application stack using a single YAML file (docker-compose.yml). For an “expert” level, understanding docker-compose is crucial.

  • docker-compose up:

    • What it does: Builds (if necessary), creates, starts, and attaches to containers for a service. If services are already running, it tries to recreate them if changes are detected. ⬆️
    • Example: (Assuming you have a docker-compose.yml file)
      docker-compose up -d # Run in detached mode
  • docker-compose down:

    • What it does: Stops and removes containers, networks, and volumes (if defined in the Compose file) created by up. ⬇️
    • Example:
      docker-compose down
  • docker-compose ps:

    • What it does: Lists all services defined in the docker-compose.yml file and their status. 📋
  • docker-compose build:

    • What it does: Builds or rebuilds services. Useful after making changes to a Dockerfile. 🔨

This is just a tiny peek into Docker Compose, but it’s a powerful tool that simplifies complex application deployments immensely!


Conclusion: Your Docker Journey Continues! ✨

Congratulations! You’ve just taken a massive leap from a Docker novice to someone with a solid grasp of its essential commands. You now have the power to:

  • Manage Images: Pull, build, and remove your application blueprints.
  • Orchestrate Containers: Run, stop, start, and remove your live applications.
  • Inspect and Interact: Peek into container logs, execute commands, and monitor performance.
  • Handle Data: Ensure your important information persists using volumes.
  • Configure Networks: Connect your services for seamless communication.
  • Maintain Your System: Keep your Docker environment clean and efficient.
  • Share Your Work: Interact with registries to share or pull images.

Remember, this guide is a starting point. The best way to truly master Docker is by experimenting, building projects, and integrating it into your daily workflow. The Docker documentation is an excellent resource for diving deeper into specific commands and advanced topics.

Keep experimenting, keep learning, and happy containerizing! 🚀💡🛣️

답글 남기기

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