월. 7월 21st, 2025

Hello, developers! Have you ever said or heard something like, “It works fine on my machine…” or “Why doesn’t it work on my deployment?”? As a developer, you’ve probably experienced the headaches that come from the difference between your development environment and your production environment. There’s a magical tool that can solve this pain: Docker.

More than just a “hot technology”, Docker has become the standard for modern software development and deployment. In this article, we’ll explain everything about Docker in detail, from what it is, to why you need it, to how to use it! 😉 Let’s get started!

—.

🚀 1. What is Docker?

Docker is an open-source platform that allows you to package and run applications in self-contained environments called containers. In short, it bundles your application and everything it needs to run (code, runtime, system tools, libraries, etc.) into a single, lightweight package that can run the same in any environment.

It’s a bit like shipping internationally and putting a bunch of different things like clothes, shoes, electronics, etc. into a standardized container box. The container box ensures that they’re transported efficiently and safely, regardless of their contents! 🚢.

—.

🧐 2. Why should you use a docker? (Core benefits)

Docker provides developers with a number of innovative benefits that greatly improve development workflows.

    • Ensures environment consistency (Consistency) ✨**
    • *It solves the tiresome problem of “It works on my machine, why doesn’t it work on yours?” 👉. Docker containers fully encapsulate your application’s execution environment, so it behaves the same whether it’s in development, test, or production.
  • **Isolation.
    • Each container works completely independently. If something goes wrong in one container, it doesn’t affect the others, and multiple applications can run on the same server without crashing.
  • Portability 🌍
    • Containerized applications can run anywhere Docker is installed. This means that they can be deployed on any operating system or infrastructure, from local development environments to the cloud (AWS, Azure, GCP).
  • Efficiency ⚡.
    • Compared to virtual machines (VMs), they are much lighter and start up faster. Containers have less overhead because they share the kernel of the host OS, and are more resource efficient because they use only the resources they need.
  • **Scalability.
      • You can easily and quickly clone and run multiple copies of the same container as needed. This is essential for flexibly responding to traffic growth and making your application more scalable.
  • Simplified CI/CD (⚙️)
    • *The process of building and deploying Docker images is easy to automate, which is highly beneficial for building continuous integration (CI) and continuous delivery (CD) pipelines.

—.

🧩 3. Understand the core concepts of Docker

To use Docker properly, you need to know a few important concepts.

3.1. Container vs. Virtual Machine 🧐

This is where a lot of people get confused: both provide isolated environments, but there is a big difference in how they work.

Distinction Virtual Machine (VM) Container (Container)
Concept Virtualizes hardware to run an independent OS Shares the OS kernel and isolates only applications
Size GB (including OS) MB (application and dependencies only)
Startup Time Minutes (OS boot time) Seconds (no OS boot required)
Resource Usage: High (each VM consumes OS resources) Low (host OS kernel sharing, efficient)
Purpose Multiple OS environments required, strong isolation required Application deployment and environment consistency required
Structure Host OS ➡️ Hypervisor ➡️ Guest OS ➡️ App Host OS ➡️ Docker Engine ➡️ Container ➡️ App
Metaphor Each is a separate “house” 🏠 Rooms in an apartment complex 🏢

3.2. Docker Image 🖼️

  • Definition: A read-only template for creating a container. It contains everything needed to run an application (code, runtime, libraries, environment variables, configuration files).
  • Analogy: It’s like a “taiyaki mold” for making taiyaki bread, or an “installation file (.exe, .dmg)” for installing specific software. The image doesn’t change, and you can create multiple containers based on it.

For more information, see: #### 3.3. Docker Containers 📦

  • Definition: An independent, isolated execution environment that runs on top of a Docker image. The image is the blueprint for creating a container, and the container is the actual application instance that is built from that blueprint.
  • Analogy: It’s like the actual taiyaki bread that’s molded into a taiyaki mold, or a program that’s installed on your computer with an installer file and running.

For example: #### 3.4. Dockerfile 📝

  • Definition: A command-line script for building (creating) a Docker image. It is written as a text file and defines what operating system to base it on, what files to copy, what commands to run, etc. in that order.
  • Analogy: It’s a ‘recipe’ for making taiyaki bread: you can look at this recipe (Dockerfile) and make the taiyaki mold (Image).

For example: #### 3.5. Docker Registry 🌐

  • Definition: A centralized repository for storing and sharing Docker images. The most prominent public registry is Docker Hub. Here, developers can download images created by others (pull) or upload their own (push).
  • An analogy: It’s like a giant shared kitchen or an app store where the world shares taiyaki molds.

3.6. Docker Volumes 💾

  • Definition: A way to permanently store data from a container on a host machine. Containers are volatile, so if a container is deleted, the data that was in it is gone. With a volume, the data is preserved even if the container is restarted or deleted.
  • An analogy: When a taiyaki bakery closes, it’s like “storing the dough ingredients in the refrigerator” for the next day’s business.

3.7. Docker Network 🔗

  • Definition: A feature that allows multiple containers to communicate with each other. By default, containers are isolated, but with network settings, they can connect to each other and send data back and forth.
  • Analogy: It’s like creating a “connected pathway” between multiple taiyaki shops so that they can lend each other materials or tools.

—.

⚙️ 4. Basic Docker Usage (Terminal Commands)

Now let’s learn how to use Docker with actual Docker commands!

4.1. Installing Docker

You can install Docker Desktop or Docker Engine for each operating system (Windows, macOS, Linux) from the official Docker website. Docker Official Installation Guide

After installation, you can verify that Docker is installed properly by running the following command in the terminal.

docker --version
# Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1

4.2. Pull the image (Pull)

This command downloads an image from Docker Hub.

docker pull :
# Example: Download the latest Ubuntu image
docker pull ubuntu:latest
# Example: Download an Nginx web server image
docker pull nginx

4.3. Run the Container (Run)

This is one of the most used commands. It creates and runs a container based on an image.

docker run [options] : [run_command]

# Key options:
# -d: Detached mode. Run a container in the background (returns terminal control).
# -p :: Port forwarding (connect a specific port on the host to a specific port on the container)
# --name : Give the container a unique name (randomly generated if not specified)
# -it: Interactive TTY. Connect a terminal to interact with the container (e.g., access the bash shell)
# -v :: Mount the volume (link the host folder to a folder inside the container)

# Example 1: Run the Nginx web server container in the background, and connect port 80 on the host with port 80 on the container
docker run -d -p 80:80 --name my-nginx-web nginx

# Example 2: Run an Ubuntu container and connect to the bash shell
docker run -it ubuntu /bin/bash
# Once inside the container, the prompt will change.
# root@:/#
# You can exit the container with the exit command.

4.4. Checking the running container (ps)

Check the list of currently running containers.

docker ps
# Check all containers (including non-running ones):
docker ps -a

4.5. Stop/Restart/Remove Containers (stop, restart, rm)

  • Stop: docker stop
    docker stop my-nginx-web
  • Restart: docker restart
    docker restart my-nginx-web
  • Delete: docker rm (container must be stopped to delete)
    docker rm my-nginx-web
    # Force delete:
    # docker rm -f my-nginx-web

4.6. Accessing Container Internals (exec)

You can access the inside of a running container and execute commands.

docker exec -it  /bin/bash
# Example: To get inside the running my-nginx-web container
docker exec -it my-nginx-web /bin/bash

4.7. Create your own image (Build)

Build an image using a Dockerfile. You must run it from the directory where the Dockerfile is located.

docker build [option] 

# -t :: Give the image to be built a name and tag (if tag is omitted :latest)
# ..: Find the Dockerfile in the current directory.

# Example: Build the my-app:1.0 image with the Dockerfile in the current directory
docker build -t my-app:1.0 .

4.8. Check the image list (images)

Check the list of downloaded or built images.

docker images
```bash docker images

#### 4.9. Delete an image (rmi)

Delete an image (no containers using the image must exist to delete it)
```bash
docker rmi :
# Example: Delete the ubuntu:latest image
docker rmi ubuntu:latest

—.

🚀 5. Going further: advanced docker techniques

5.1. Docker Compose 🐳➡️🐳🐳

  • Definition: A tool for defining and running applications composed of multiple containers. You can manage multiple services (web server, database, cache, etc.) at once with a single YAML file called docker-compose.yml.
  • Example (docker-compose.yml):
    version: '3.8'
    services:
      web:
        . build: . # Build an image from the Dockerfile in the current directory.
        ports:
          - "8000:5000" # connect host 8000 -> container 5000 ports
        volumes:
          - .:/app # Mount the current directory as /app in the container
      redis:
        image: "redis:alpine" # Use redis image.
  • Usage: Running the docker-compose up -d command in the directory where the docker-compose.yml file is located will run all defined services at once.

For more information, see: #### 5.2. Docker Swarm & Kubernetes 🌐 cluster

  • Definition: A Container Orchestration tool for deploying, managing, and scaling containerized applications across multiple servers (nodes).
  • Docker Swarm is an orchestration tool built into the Docker engine that is relatively lightweight and easy to use.
  • Kubernetes is an open source system developed by Google that is much more complex and powerful, and is now widely used as an industry standard.

—.

💡 6. Hands-on example: dockerizing your own web application 🚀

Let’s take a look at the most popular scenario: running a web application as a Docker container.

6.1. Example 1: Running a simple Nginx web server

Running Nginx, the web server king, with Docker is very easy.

  1. Download the image and run the container: ** “`bash

    docker run -d -p 80:80 --name my-nginx-server nginx
    • -d: Run in the background
    • -p 80:80: connect port 80 on the host to port 80 on the container (web default port)
    • --name my-nginx-server: name the container my-nginx-server
    • nginx: Name of the image to use
  2. Check: Open a web browser and try going to http://localhost or http://127.0.0.1. If you see Nginx’s main welcome page, you’re good to go! 🎉 .

  3. Stop and delete the container: Run

    docker stop my-nginx-server
    docker rm my-nginx-server

6.2. Example 2: Dockerizing a simple Python Flask web app

Let’s dockerize a simple web application created with Python Flask into a Docker container.

  1. Create a project structure: **Create a new directory Create a new directory, and inside it, create the following three files.

    my-flask-app/
    ├── app.py
    ├── requirements.txt
    └── Dockerfile
  2. app.py (Flask application code): app.py (Flask application code):

    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return "Hello, this is your Flask app in a docker container! 🐳"
    
    if __name__ == '__main__':
        @app.run(debug=True, host='0.0.0.0') # set to 0.0.0.0 to make it externally accessible
  3. requirements.txt (Python Dependency): **.

    Flask==2.3.2
  4. Dockerfile (Docker image build instructions):

    # 1. specify which base image to use (using Python version 3.9)
    FROM python:3.9-slim-buster
    
    # 2. set the working directory inside the container
    WORKDIR /app
    
    # 3. copy the requirements.txt file from the host to the /app directory of the container
    COPY requirements.txt .
    
    # 4. Install the Python libraries specified in requirements.txt
    RUN pip install -r requirements.txt
    
    # 5. copy all files on the host to the /app directory of the container (including app.py)
    COPY . .
    
    # 6. specify that this container will expose port 5000 (for informational purposes)
    EXPOSE 5000
    
    # 7. the default command to run when the container is started
    # (command to run the Flask app)
    CMD ["python", "app.py"]
  5. Build a Docker image: ** Go to the my-flask-app directory. Navigate to the my-flask-app directory and execute the following command.

    cd my-flask-app
    docker build -t my-flask-app:1.0 .

    Once the build is complete, you can verify that the my-flask-app:1.0 image has been created with the docker images command.

  6. Run the docker container: ** Run

    docker run -d -p 5000:5000 --name flask-web-app my-flask-app:1.0
    • -p 5000:5000: Connect port 5000 on host with port 5000 on container
  7. Verify: Open a web browser and try connecting to http://localhost:5000. If you see the message “Hello, it’s the Flask app in a docker container! 🐳”, you’re good to go! 🎉

  8. Stop and delete the container:** Run

    docker stop flask-web-app
    docker rm flask-web-app

—]

🌟 7. Conclusion: Future Development with Docker!

Docker is more than just a “green issue”, it’s a revolutionary tool that has changed the paradigm of modern software development. Container technology makes application deployment and management much more efficient and flexible, and it has played a crucial role in the spread of microservices architecture, cloud-native development, and DevOps culture.

We hope this article has familiarized you with the basic concepts and usage of Docker. Now it’s time to apply Docker to your development environment and start creating more efficient and reliable applications! 💪

Docker is constantly evolving, and the possibilities are endless. We highly recommend that you dive into the world of Docker today and experience the power of container technology for yourself! Happy Dockerizing! ✨

—]

답글 남기기

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