English version
Docker becomes easier to understand when you separate two ideas: image and container.
A Docker image is the package. It contains the application, runtime, configuration, scripts, and filesystem layers needed to start the application. It is an artifact that can be moved around.
A Docker container is the running environment created from an image. When an image starts, it becomes a container. The container has a process, environment variables, networking, port mappings, and a writable layer on top of the image.
That distinction explains most beginner confusion. You build or pull images. You run containers from images.
Basic commands
Common Docker commands from the Velog note:
docker search <image>
docker pull <image>
docker images
docker run <image>
docker ps
docker ps --all
docker stop <container_id>
docker start <container_id>
docker restart <container_id>
docker rename <old_name> <new_name>
docker search looks for public images. docker pull downloads an image. docker images shows local images. docker run creates and starts a new container from an image. docker ps shows running containers, while docker ps --all also shows stopped containers.
Detached mode and ports
A container can run in detached mode:
docker run -d <image>
Detached mode starts the container in the background so the terminal can be used again.
Port mapping connects a host port to a container port:
docker run -p 8080:8080 <image>
This matters because multiple containers can run on one host, but a host port can only be bound once. If two containers both try to bind the same host port, one of them will fail.
The container port is the port the application listens on inside the container. The host port is the port exposed on the machine running Docker.
Environment variables and networks
A container often needs runtime configuration. Environment variables are a common way to pass that configuration:
docker run -d \
--name mongo-express \
-p 8080:8080 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--net mongo-network \
mongo-express
The example is simplified, but the shape is important. A container gets a name, port mapping, environment variables, network, and image.
Do not hard-code real passwords in commands for production. Use secret management or environment injection from the deployment platform.
Docker Compose
Docker Compose runs multiple containers from a YAML file. A Dockerfile describes how to build an image. A Compose file describes how to run services.
A simplified Compose shape looks like this:
services:
mongodb:
image: mongo
mongo-express:
image: mongo-express
ports:
- "8080:8080"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongodb
Run it with:
docker compose -f mongo.yaml up
The practical distinction:
- Dockerfile: build the image.
- Compose YAML: run one or more services from images.
Dockerfile
A Dockerfile is a blueprint for building an image. It describes the base image, environment variables, copied files, build steps, and default command.
A simplified Node example:
FROM node
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password
RUN mkdir -p /home/app
COPY . /home/app
CMD ["node", "server.js"]
RUN executes while building the image. COPY copies files from the host build context into the image. CMD defines the default command when a container starts.
Again, production credentials should not be baked into a Dockerfile. The example is only for learning the mechanics.
Korean notes
Docker를 이해할 때 가장 먼저 나눠야 하는 것은 image와 container다.
Docker image는 package다. Application, runtime, configuration, start script, filesystem layer가 들어 있는 artifact다. Image 자체는 실행 중인 상태가 아니다.
Docker container는 image를 실행한 runtime environment다. Image가 start되면 container가 된다. Container는 process, environment variable, network, port mapping, writable layer를 가진다.
쉽게 말하면 image는 실행 전 package이고, container는 실행 중인 instance다.
기본 명령어
docker search <image>
docker pull <image>
docker images
docker run <image>
docker ps
docker ps --all
docker stop <container_id>
docker start <container_id>
docker restart <container_id>
docker rename <old_name> <new_name>
docker search는 public image를 찾는다. docker pull은 image를 가져온다. docker images는 local machine에 있는 image를 보여준다. docker run은 image로부터 새 container를 만들고 실행한다.
docker ps는 실행 중인 container를 보여주고, docker ps --all은 stopped container까지 보여준다.
Detached mode와 port
docker run -d <image>
-d는 detached mode다. Container를 background에서 실행하고 terminal을 다시 사용할 수 있게 한다.
Port mapping은 host port와 container port를 연결한다.
docker run -p 8080:8080 <image>
Container 안 application이 8080 port를 listen하고 있고, host에서도 8080으로 접근하고 싶다면 이런 mapping이 필요하다.
주의할 점은 host port는 하나의 process/container만 사용할 수 있다는 것이다. 여러 container가 같은 host port를 잡으려고 하면 충돌이 난다.
Environment variable과 network
Container는 실행 시점 설정이 필요할 때가 많다. Environment variable은 그 설정을 넘기는 가장 흔한 방식이다.
docker run -d \
--name mongo-express \
-p 8080:8080 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--net mongo-network \
mongo-express
이 예시는 학습용이다. Production에서는 password를 command나 Dockerfile에 직접 쓰지 말고 secret manager나 deployment platform의 secret injection을 써야 한다.
Docker Compose
Docker Compose는 YAML 파일로 여러 container/service를 실행하는 도구다.
services:
mongodb:
image: mongo
mongo-express:
image: mongo-express
ports:
- "8080:8080"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongodb
실행은 보통 이렇게 한다.
docker compose -f mongo.yaml up
Dockerfile은 image를 build하기 위한 blueprint이고, Compose YAML은 image를 어떻게 실행할지 정의하는 파일이다.
Dockerfile
Dockerfile은 image를 만들기 위한 설계도다.
FROM node
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password
RUN mkdir -p /home/app
COPY . /home/app
CMD ["node", "server.js"]
RUN은 image build 중 실행된다. COPY는 host의 file을 image 안으로 복사한다. CMD는 container가 시작될 때 실행할 기본 command를 정한다.
정리하면 Docker의 핵심 흐름은 image를 만들고, 그 image에서 container를 실행하고, port/network/environment를 통해 runtime behavior를 제어하는 것이다.