ν™”. 8μ›” 12th, 2025

G: Hello, future Docker wizard! πŸ‘‹ Are you tired of hearing “it works on my machine” only to find your application crumbling in a different environment? Or perhaps you’re just looking for a more efficient and consistent way to develop and deploy software? If so, Docker is your new best friend!

This comprehensive guide is designed specifically for beginners. We’ll demystify Docker by breaking down its core concepts and walking you through the most essential commands you’ll need to get started, along with practical tips and tricks. Let’s dive in! πŸš€


Understanding Docker Basics: Images vs. Containers 🧠

Before we unleash the commands, let’s quickly grasp the two most fundamental concepts in Docker:

  1. Docker Image (πŸ“¦): Your Application’s Blueprint

    • Think of an image as a lightweight, standalone, executable package that includes everything needed to run a piece of software: code, runtime, system tools, libraries, and settings.
    • It’s read-only and immutable.
    • Analogy: If you’re baking cookies, the Docker Image is like the cookie cutter – a template for creating many identical cookies. πŸͺ
  2. Docker Container (πŸƒ): Your Running Application

    • A container is a runnable instance of an image. You can create, start, stop, move, or delete a container.
    • It’s isolated from other containers and from the host system.
    • Analogy: Following our cookie analogy, the Docker Container is the actual cookie you bake using the cookie cutter. You can eat it, decorate it, or put it on a plate. 🍽️

Where do images come from? Most public images are stored on Docker Hub, which is like GitHub for Docker images. You can also build your own custom images using a Dockerfile.


Section 1: Getting Started – Your First Docker Commands 🏁

Let’s verify your Docker installation and run your first container!

1.1 Check Docker Version: docker --version

First things first, let’s make sure Docker is installed correctly and see its version.

  • Purpose: Verify Docker installation.
  • Command:
    docker --version
  • Example Output:
    Docker version 24.0.7, build afdd53b

    If you see a version number, you’re good to go! πŸ‘

1.2 Download a Docker Image: docker pull

Before you can run a container, you need an image. docker pull downloads an image from Docker Hub to your local machine.

  • Purpose: Download a specified image from a registry (default: Docker Hub).
  • Command:
    docker pull [image_name]:[tag]
    • image_name: The name of the image (e.g., ubuntu, nginx, mysql).
    • tag: The specific version of the image (e.g., latest, 20.04, 1.23). If no tag is specified, latest is assumed.
  • Examples:
    • Download the latest Ubuntu image:
      docker pull ubuntu
      # Equivalent to: docker pull ubuntu:latest
    • Download a specific Nginx version:
      docker pull nginx:1.23
    • Download the official hello-world image (it’s tiny and great for a first test!):
      docker pull hello-world

      You’ll see a download progress bar. Once complete, the image is ready! βœ…

1.3 Run Your First Container: docker run

This is where the magic happens! docker run creates a new container from an image and starts it. It’s one of the most powerful and frequently used commands.

  • Purpose: Create and start a container from an image.

  • Command:

    docker run [OPTIONS] [image_name]:[tag] [COMMAND] [ARG...]
  • Key Options for docker run:

    • -d, --detach: Run container in detached mode (in the background). Your terminal won’t be blocked. πŸ’«
    • -p, `–publish :`: Publish a container’s port to the host. Essential for web apps! 🌐
    • -it: Interactive mode + TTY. Use this to get an interactive shell inside a container. πŸ—£οΈ
    • `–name `: Assign a specific name to your container. Makes it easier to manage! 🏷️
    • -v, `–volume :`: Mount a volume (bind mount) to persist data or share files between host and container. πŸ“βž‘οΈπŸ“¦
    • --rm: Automatically remove the container when it exits. Great for temporary tests. πŸ—‘οΈ
  • Examples:

    1. Run hello-world (basic test):

      docker run hello-world

      This will download the hello-world image (if not already present), run a container from it, print a message, and then exit. You’ll see “Hello from Docker!” πŸ₯³

    2. Run an interactive Ubuntu shell:

      docker run -it ubuntu bash
      • -it: Makes the container interactive and allocates a pseudo-TTY, so you can type commands inside the container.
      • ubuntu: The image to use.
      • bash: The command to run inside the container (starts a bash shell). Now you’re inside the Ubuntu container! Try ls -la / or pwd. Type exit to leave the container. πŸ‘‹
    3. Run Nginx in detached mode, exposing port 80:

      docker run -d -p 80:80 --name my-nginx-server nginx
      • -d: Runs the Nginx server in the background.
      • -p 80:80: Maps port 80 on your host machine to port 80 inside the nginx container. Now you can open http://localhost in your web browser and see the Nginx welcome page! 🌍
      • --name my-nginx-server: Gives this specific Nginx container the name my-nginx-server.

Section 2: Managing Your Containers – The Daily Grind βš™οΈ

Once your containers are running, you’ll need to monitor, stop, start, and remove them.

2.1 List Running Containers: docker ps

This command shows you which containers are currently active.

  • Purpose: List running containers.
  • Command:
    docker ps
    # To show all containers (running and stopped):
    docker ps -a
  • Example Output:
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
    a1b2c3d4e5f6   nginx     "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    0.0.0.0:80->80/tcp   my-nginx-server

    This output gives you crucial info like CONTAINER ID, IMAGE, STATUS, PORTS, and NAMES. You’ll use the CONTAINER ID or NAMES for other commands.

2.2 Stop a Running Container: docker stop

Gracefully stops one or more running containers.

  • Purpose: Send a SIGTERM signal to the main process inside the container, giving it time to shut down.
  • Command:
    docker stop [container_id_or_name]
  • Example:
    docker stop my-nginx-server
    # Or using the container ID:
    docker stop a1b2c3d4e5f6

    After stopping, docker ps won’t show it, but docker ps -a will show its status as Exited.

2.3 Start a Stopped Container: docker start

Restarts a container that was previously stopped.

  • Purpose: Start one or more stopped containers.
  • Command:
    docker start [container_id_or_name]
  • Example:
    docker start my-nginx-server

2.4 Restart a Container: docker restart

A quick way to stop and then start a container.

  • Purpose: Stop and then start one or more containers.
  • Command:
    docker restart [container_id_or_name]
  • Example:
    docker restart my-nginx-server

2.5 Remove a Container: docker rm

Deletes a container that is no longer needed. You must stop a container before you can remove it (unless using -f).

  • Purpose: Remove one or more stopped containers.
  • Command:
    docker rm [container_id_or_name]
    # To force remove a running container:
    docker rm -f [container_id_or_name]
  • Example:
    docker stop my-nginx-server # First stop it
    docker rm my-nginx-server   # Then remove it

    Tip: You can remove multiple containers at once: docker rm container1 container2

2.6 Execute Commands Inside a Running Container: docker exec

This is incredibly useful for debugging or performing tasks inside a running container without stopping it.

  • Purpose: Run a command in a running container.
  • Command:
    docker exec [OPTIONS] [container_id_or_name] [COMMAND] [ARG...]
    • -it: Essential for getting an interactive shell.
  • Examples:
    1. Get a bash shell inside your Nginx container:
      docker exec -it my-nginx-server bash

      Now you can navigate the container’s file system, install packages, etc. (e.g., apt update, ls /etc/nginx). Type exit to leave. πŸšͺ

    2. Check Nginx version without entering the shell:
      docker exec my-nginx-server nginx -v

2.7 View Container Logs: docker logs

See what’s happening inside your container by viewing its standard output and standard error streams.

  • Purpose: Fetch the logs of a container.
  • Command:
    docker logs [OPTIONS] [container_id_or_name]
    • -f, --follow: Follow log output (like tail -f). πŸ“
    • `–tail `: Output the specified number of lines from the end of the logs.
  • Examples:
    • View all logs for your Nginx server:
      docker logs my-nginx-server
    • Follow Nginx logs in real-time:
      docker logs -f my-nginx-server

      This is fantastic for debugging! πŸ›


Section 3: Managing Your Images – Keeping Things Tidy 🧹

Images can take up a lot of disk space. Here’s how to manage them.

3.1 List Images: docker images

See all the images you have downloaded or built on your local machine.

  • Purpose: List local Docker images.
  • Command:
    docker images
  • Example Output:
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    ubuntu        latest    d70e132049d1   3 weeks ago    77.8MB
    nginx         latest    53e207908c69   2 months ago   187MB
    hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB

3.2 Remove an Image: docker rmi

Delete an image from your local system. You cannot remove an image if a container is still using it! You must remove the container first.

  • Purpose: Remove one or more images.
  • Command:
    docker rmi [image_id_or_name]:[tag]
  • Example:
    docker rmi ubuntu:latest
    # Or using the IMAGE ID:
    docker rmi d70e132049d1

    If an image is associated with a running or stopped container, you’ll need to remove those containers first (docker rm).

3.3 Build Your Own Image: docker build (Introduction)

While not strictly a “command to run a container,” docker build is how you create your custom images using a Dockerfile. This is fundamental for packaging your own applications.

  • Purpose: Build a new image from a Dockerfile and a “context” (the set of files at a specified path).
  • Command:
    docker build [OPTIONS] PATH | URL | -
    • PATH: The path to the directory containing your Dockerfile and application code.
    • -t, `–tag :`: Name and optionally tag the image (e.g., `my-app:1.0`).
  • Example (Conceptual): Let’s say you have a simple Python Flask app.
    1. Create a Dockerfile in your app’s root directory:
      # Dockerfile
      FROM python:3.9-slim-buster
      WORKDIR /app
      COPY requirements.txt .
      RUN pip install -r requirements.txt
      COPY . .
      EXPOSE 5000
      CMD ["python", "app.py"]
    2. Build the image from the same directory where your Dockerfile is:
      docker build -t my-flask-app:1.0 .

      The . at the end means “use the current directory as the build context.” Now you have my-flask-app:1.0 available locally! You can docker run it.


Section 4: Advanced & Essential Concepts for Beginners ✨

These commands and concepts will significantly enhance your Docker experience.

4.1 Data Persistence with Volumes: -v (Revisited)

Containers are ephemeral; data inside them is lost when the container is removed. Volumes solve this by storing data outside the container on the host system.

  • Bind Mounts (-v host_path:container_path): You explicitly map a directory on your host machine to a directory inside the container. Great for development, configuration files.

    • Example: Persisting Nginx configuration:
      docker run -d -p 80:80 \
        -v /path/to/my/nginx/conf:/etc/nginx/conf.d \
        --name my-nginx-custom nginx

      Now, changes you make to nginx.conf on your host machine in /path/to/my/nginx/conf will instantly reflect inside the container.

  • Managed Volumes (docker volume): Docker manages the storage location on the host. Ideal for databases and other long-term data storage.

    • Create a volume:
      docker volume create my_data_volume
    • Use the volume with a container:
      docker run -d \
        -v my_data_volume:/var/lib/mysql \
        --name my-mysql-db mysql:8.0

      Even if you remove and re-create my-mysql-db, the data in my_data_volume will persist! πŸ’Ύ

4.2 Inspect Docker Objects: docker inspect

Get detailed low-level information about Docker objects (containers, images, volumes, networks).

  • Purpose: Return low-level information on Docker objects.
  • Command:
    docker inspect [container_id_or_name_or_image_id_etc]
  • Example:
    docker inspect my-nginx-server

    This will output a large JSON object containing configuration details, network settings, volume mounts, and much more. Excellent for debugging complex setups. 🧐

4.3 Clean Up Unused Docker Objects: docker system prune

Docker can accumulate a lot of unused “junk” over time: stopped containers, dangling images, unused networks, and volumes. This command is your cleanup superhero! 🧹

  • Purpose: Remove unused Docker data.
  • Command:
    docker system prune
    • This will remove:
      • All stopped containers.
      • All dangling images (images that are not tagged and not used by any container).
      • All unused networks.
    • Use with caution! It will ask for confirmation.
  • More aggressive cleanup:
    docker system prune -a
    • This will remove all unused images (not just dangling ones) and all build cache. Use this if you really need to free up a lot of disk space.

Section 5: Next Steps – Where to Go From Here πŸ—ΊοΈ

You’ve mastered the essential Docker commands! What’s next on your journey?

  • Docker Compose: For multi-container applications (e.g., a web app, a database, and a cache working together), docker-compose simplifies the management with a single YAML file. It’s the next logical step after individual container commands.
  • Dockerfiles Deep Dive: Truly understand how to craft efficient and secure Docker images for your applications.
  • Docker Networks: Learn more about how containers communicate with each other and the outside world.
  • Orchestration (Kubernetes): For deploying and managing large-scale containerized applications across clusters of machines. This is a big leap but worth exploring when you’re ready.

Conclusion πŸŽ‰

Congratulations! You’ve taken a significant step in understanding and using Docker. You now have a solid foundation with the most crucial commands at your fingertips. Remember, practice is key! Experiment with these commands, try running different applications, and don’t be afraid to break things (that’s why Docker is great – you can easily clean up and start fresh).

Docker will revolutionize your development workflow, making your applications more portable, isolated, and easier to manage. Keep building, keep learning! Happy Dockering! πŸš€πŸ³

λ‹΅κΈ€ 남기기

이메일 μ£Όμ†ŒλŠ” κ³΅κ°œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•„μˆ˜ ν•„λ“œλŠ” *둜 ν‘œμ‹œλ©λ‹ˆλ‹€