화. 7월 22nd, 2025

Docker has revolutionized the way we develop, ship, and run applications. By packaging applications into isolated “containers,” Docker ensures consistency across different environments, simplifies deployment, and boosts productivity. At its core, Docker relies on a powerful command-line interface (CLI) to manage these containers, images, volumes, and networks.

If you’re new to Docker or looking to solidify your understanding of its most frequently used commands, you’ve come to the right place! This blog post will be your go-to guide for mastering the essential Docker commands, complete with explanations, examples, and tips to make your containerization journey smoother.


Table of Contents

  1. Introduction to Docker Commands
  2. Getting Started: Images & Running Containers
    • docker pull
    • docker run
  3. Container Lifecycle Management
    • docker ps
    • docker start
    • docker stop
    • docker restart
    • docker rm
  4. Building Your Own Images
    • docker build
    • docker images
    • docker rmi
  5. Interacting with Containers
    • docker exec
    • docker logs
  6. Advanced & Utility Commands
    • docker login / docker push
    • docker info
    • docker version
    • docker system prune

1. Introduction to Docker Commands

Every Docker command typically follows the structure: docker [command] [subcommand] [options] [arguments]. For instance, docker run -d -p 80:80 nginx.

  • docker: The main Docker client.
  • run: The command to run a container.
  • -d -p 80:80: Options specifying how the container should run (detached, port mapping).
  • nginx: The argument, in this case, the image name.

Let’s dive into the most common ones! 👇


2. Getting Started: Images & Running Containers

These commands are your entry point into the Docker world. You’ll use them to get new images and spin up your first containers.

docker pull [image_name]:[tag] ⬇️

  • Purpose: Downloads an image from Docker Hub (or a configured registry) to your local machine. Images are read-only templates from which containers are built.
  • When to use: Before you run a container based on an image that isn’t already on your system.
  • Example:

    docker pull ubuntu:latest
    # This downloads the latest version of the Ubuntu image.
    
    docker pull nginx:1.21
    # Downloads a specific version of the Nginx web server image.

    If you don’t specify a tag, Docker defaults to latest.

docker run [options] [image_name]:[tag] [command] 🚀

  • Purpose: Creates a new container from a specified image and runs a command inside it. This is arguably the most frequently used Docker command!
  • When to use: To start a new instance of an application or service defined by an image.
  • Key Options:
    • -d or --detach: Runs the container in the background (detached mode).
    • -p [host_port]:[container_port] or --publish: Maps a port from your host machine to a port inside the container.
    • --name [container_name]: Assigns a custom name to your container (makes it easier to refer to later).
    • -it or --interactive --tty: Allocates a pseudo-TTY and keeps STDIN open, allowing you to interact with the container (e.g., to open a shell).
    • --rm: Automatically removes the container when it exits.
  • Examples:

    docker run hello-world
    # Runs the "hello-world" image, which prints a message and exits.
    # Good for a first test!
    
    docker run -d -p 80:80 --name my-nginx nginx:latest
    # Runs an Nginx web server in the background (-d).
    # Maps host port 80 to container port 80 (-p 80:80).
    # Names the container "my-nginx".
    
    docker run -it ubuntu:latest bash
    # Runs an interactive Bash shell inside a new Ubuntu container.
    # You'll be dropped into the container's shell. Type 'exit' to leave.

3. Container Lifecycle Management

Once you have containers running, you’ll need commands to inspect, stop, start, and remove them.

docker ps [options] 📋

  • Purpose: Lists running containers. It’s like Task Manager or Activity Monitor for your Docker containers.
  • When to use: To see what containers are currently active on your system.
  • Key Options:
    • -a or --all: Shows all containers, including stopped ones.
    • -s or --size: Displays the total file sizes.
    • -l or --last: Shows the last created container (including exited).
  • Example:

    docker ps
    # Shows only running containers.
    
    docker ps -a
    # Shows all containers (running and stopped).

docker start [container_id/name] ▶️

  • Purpose: Starts one or more stopped containers.
  • When to use: To resume a previously stopped container without creating a new one.
  • Example:
    docker start my-nginx
    # Starts the container named "my-nginx" if it was stopped.

docker stop [container_id/name] ⏹️

  • Purpose: Gracefully stops one or more running containers. Docker sends a SIGTERM signal, allowing the container’s process to shut down cleanly. If the process doesn’t exit after a timeout (default 10 seconds), Docker sends SIGKILL.
  • When to use: To shut down a running container.
  • Example:
    docker stop my-nginx
    # Stops the container named "my-nginx".

docker restart [container_id/name] 🔄

  • Purpose: Stops a running container and then starts it again.
  • When to use: To apply configuration changes to a running container (if those changes don’t require recreating the container) or simply to cycle a service.
  • Example:
    docker restart my-nginx
    # Restarts the "my-nginx" container.

docker rm [container_id/name] 🗑️

  • Purpose: Removes one or more stopped containers.
  • When to use: To clean up your system by deleting containers you no longer need.
  • Key Options:
    • -f or --force: Forces the removal of a running container. Use with caution! This sends a SIGKILL immediately.
  • Example:

    docker rm my-nginx
    # Removes the stopped container named "my-nginx".
    
    docker rm -f another-container
    # Forces removal of "another-container", even if it's running.

4. Building Your Own Images

Docker’s power extends beyond using pre-built images. You can create your own custom images using a Dockerfile.

docker build [options] PATH 🏗️

  • Purpose: Builds a new Docker image from a Dockerfile and a “context” (a set of files at a specified path).
  • When to use: When you want to package your own application or custom environment into an image.
  • Key Options:
    • -t [image_name]:[tag] or --tag: Names and optionally tags the image. Crucial for organization.
  • Example:

    • First, make sure you have a Dockerfile in your current directory (e.g., . for the current directory):
      # Dockerfile example
      FROM ubuntu:latest
      RUN apt-get update && apt-get install -y cowsay
      CMD ["cowsay", "Hello, Docker!"]
    • Then, build the image:

      docker build -t my-cowsay-app:1.0 .
      # Builds an image from the Dockerfile in the current directory (.).
      # Tags it as "my-cowsay-app" with version "1.0".
      
      docker run --rm my-cowsay-app:1.0
      # Runs your newly built image!

docker images [options] 🖼️

  • Purpose: Lists all Docker images stored on your local machine.
  • When to use: To see what images you have available for running containers or to identify images for removal.
  • Example:
    docker images
    # Lists all local images, their tags, IDs, creation dates, and sizes.

docker rmi [image_id/name]

  • Purpose: Removes one or more images from your local storage.
  • When to use: To free up disk space by deleting old or unused images.
  • Key Options:
    • -f or --force: Forces the removal of an image, even if it’s currently being used by a container. Use with extreme caution!
  • Example:

    docker rmi my-cowsay-app:1.0
    # Removes the image tagged "my-cowsay-app:1.0".
    
    docker rmi abc123def456
    # Removes the image with the specified ID.

5. Interacting with Containers

These commands allow you to peek inside or even execute commands within a running container.

docker exec [options] [container_id/name] [command] 💬

  • Purpose: Executes a command inside a running container. This is incredibly useful for debugging or performing administrative tasks.
  • When to use: To run a script, open a shell, or inspect files within a container without restarting it.
  • Key Options:
    • -it or --interactive --tty: Essential for opening an interactive shell.
  • Examples:

    docker exec my-nginx ls /etc/nginx/conf.d
    # Lists the contents of the Nginx configuration directory inside the 'my-nginx' container.
    
    docker exec -it my-nginx bash
    # Opens an interactive Bash shell inside the 'my-nginx' container.
    # You'll get a command prompt from within the container. Type 'exit' to return to your host shell.

docker logs [options] [container_id/name] 📜

  • Purpose: Fetches the logs (STDOUT/STDERR) of a container.
  • When to use: To monitor the output of your application running inside a container, diagnose errors, or see its activity.
  • Key Options:
    • -f or --follow: Follows log output in real-time (like tail -f).
    • --tail [number]: Shows only the last number of log lines.
    • --since [timestamp]: Shows logs since a specific timestamp.
  • Examples:

    docker logs my-nginx
    # Displays all accumulated logs from the 'my-nginx' container.
    
    docker logs -f my-nginx
    # Continuously streams logs from 'my-nginx'. Press Ctrl+C to stop.
    
    docker logs --tail 50 my-nginx
    # Displays only the last 50 lines of logs from 'my-nginx'.

6. Advanced & Utility Commands

Beyond the daily essentials, these commands offer deeper control and help you manage your Docker environment.

docker login [options] / docker push [image_name]:[tag] 🔑⬆️

  • Purpose (login): Authenticates your Docker client with a Docker registry (like Docker Hub) so you can push and pull private images.
  • Purpose (push): Uploads a local image to a Docker registry.
  • When to use: To share your custom images with others, deploy them to cloud services, or back them up.
  • Examples:

    docker login
    # Prompts for your Docker Hub username and password.
    
    docker tag my-custom-app:1.0 myusername/my-custom-app:1.0
    # Before pushing, you usually need to tag your image with your Docker Hub username.
    
    docker push myusername/my-custom-app:1.0
    # Uploads the tagged image to your Docker Hub repository.

docker info ℹ️

  • Purpose: Displays system-wide information about your Docker installation, including the number of containers and images, storage driver, Docker Engine version, etc.
  • When to use: For troubleshooting or getting an overview of your Docker setup.
  • Example:
    docker info
    # Provides a comprehensive summary of your Docker environment.

docker version 🏷️

  • Purpose: Shows the Docker client and server (Daemon) versions.
  • When to use: To check if your Docker client and server versions are compatible or to ensure you’re running the desired version.
  • Example:
    docker version
    # Displays client and server version details.

docker system prune [options] 🧹

  • Purpose: Cleans up unused Docker objects (containers, images, networks, build cache) to free up disk space. This is your go-to command for spring cleaning!
  • When to use: When you’re running low on disk space or after a development cycle where many temporary containers and images were created.
  • Key Options:
    • -a or --all: Remove all unused images not just dangling ones (images not associated with any container).
    • --volumes: Also remove unused volumes. Use with caution, as volumes can contain important data!
  • Example:

    docker system prune
    # Removes all stopped containers, all dangling images, and all unused networks.
    # It will prompt for confirmation.
    
    docker system prune -a --volumes
    # Removes all stopped containers, all images (even those not dangling if not used by a running container),
    # all unused networks, and all unused volumes. This is a very aggressive cleanup.
    # It will prompt for confirmation.

Conclusion

Congratulations! You’ve now got a solid foundation in the most essential Docker commands. From pulling images and running containers to building your own and cleaning up your system, these commands are the backbone of your Docker workflow.

The best way to learn is by doing. So, fire up your terminal, practice these commands, and start containerizing your applications with confidence. The Docker ecosystem is vast, but mastering these basics will empower you to explore more advanced topics like Docker Compose, Swarm, and Kubernetes with ease.

Happy containerizing! 🐳 G

답글 남기기

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