Docker는 현대 소프트웨어 개발에서 필수적인 도구가 되었습니다. "내 컴퓨터에서는 되는데..."라는 악몽 같은 상황을 해결해주고, 개발 환경을 코드처럼 버전 관리할 수 있게 해줍니다.
Docker has become an essential tool in modern software development. It solves the nightmare of "But it works on my machine..." and lets you version control your development environment like code.
하지만 Docker 명령어는 옵션이 많고 복잡해 보입니다. 이 가이드에서는 컨테이너의 라이프사이클을 이해하고, 자주 사용하는 명령어들을 체계적으로 정리합니다.
However, Docker commands look complicated with many options. In this guide, we'll understand the container lifecycle and systematically organize frequently used commands.
컨테이너 라이프사이클 이해하기 Understanding Container Lifecycle
Docker 명령어를 이해하려면 먼저 컨테이너의 생명주기를 알아야 합니다. 컨테이너는 이미지로부터 생성되고, 실행되고, 정지되고, 삭제됩니다.
To understand Docker commands, you first need to know the container lifecycle. Containers are created from images, run, stopped, and deleted.
📦 이미지 vs 컨테이너 📦 Image vs Container
이미지(Image)는 실행할 애플리케이션의 스냅샷입니다. 읽기 전용이며 변경되지 않습니다.
Image is a snapshot of the application to run. It's read-only and never changes.
컨테이너(Container)는 이미지를 기반으로 실행된 인스턴스입니다. 실행 중에 데이터가 변경될 수 있습니다.
Container is a running instance based on an image. Data can change during execution.
비유하자면, 이미지는 "설계도"이고 컨테이너는 설계도로 지은 "실제 집"입니다.
As an analogy, an image is a "blueprint" and a container is the "actual house" built from it.
필수 명령어: docker run Essential Command: docker run
가장 많이 사용하는 명령어입니다. 이미지로부터 컨테이너를 생성하고 실행합니다.
This is the most frequently used command. It creates and runs a container from an image.
docker run
이미지로부터 새 컨테이너를 생성하고 실행합니다.
Creates and runs a new container from an image.
$ docker run nginx
# 백그라운드 실행 (-d: detached)
$ docker run -d nginx
# 포트 매핑 (-p: publish)
$ docker run -d -p 8080:80 nginx
# 호스트의 8080 포트 → 컨테이너의 80 포트
# 컨테이너 이름 지정
$ docker run -d --name my-nginx -p 8080:80 nginx
# 환경 변수 설정
$ docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
# 볼륨 마운트 (데이터 영속성)
$ docker run -d -v /host/data:/container/data postgres
$ docker run nginx
# Background execution (-d: detached)
$ docker run -d nginx
# Port mapping (-p: publish)
$ docker run -d -p 8080:80 nginx
# Host port 8080 → Container port 80
# Specify container name
$ docker run -d --name my-nginx -p 8080:80 nginx
# Set environment variable
$ docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
# Volume mount (data persistence)
$ docker run -d -v /host/data:/container/data postgres
| 옵션 | 의미 | 설명 |
|---|---|---|
-d |
Detached | 백그라운드에서 실행 |
-p |
Publish | 포트 매핑 (호스트:컨테이너) |
-e |
Environment | 환경 변수 설정 |
-v |
Volume | 볼륨 마운트 (호스트:컨테이너) |
--name |
Name | 컨테이너 이름 지정 |
-it |
Interactive + TTY | 인터랙티브 모드 (터미널 접속) |
--rm |
Remove | 종료 시 컨테이너 자동 삭제 |
| Option | Meaning | Description |
|---|---|---|
-d |
Detached | Run in background |
-p |
Publish | Port mapping (host:container) |
-e |
Environment | Set environment variable |
-v |
Volume | Volume mount (host:container) |
--name |
Name | Specify container name |
-it |
Interactive + TTY | Interactive mode (terminal access) |
--rm |
Remove | Auto-delete container on exit |
💡 자주 쓰는 조합 💡 Frequently Used Combination
docker run -it --rm ubuntu bash
일회성으로 Ubuntu 컨테이너에 접속하여 작업 후 자동 삭제. 테스트 시 매우 유용합니다.
Access Ubuntu container for one-time work with auto-delete. Very useful for testing.
컨테이너 관리 명령어 Container Management Commands
$ docker ps
# 모든 컨테이너 목록 (정지된 것 포함)
$ docker ps -a
# 컨테이너 정지
$ docker stop my-nginx
# 컨테이너 시작 (정지된 컨테이너)
$ docker start my-nginx
# 컨테이너 재시작
$ docker restart my-nginx
# 컨테이너 삭제
$ docker rm my-nginx
# 실행 중인 컨테이너에 명령어 실행
$ docker exec -it my-nginx bash
# 컨테이너 로그 확인
$ docker logs -f my-nginx
$ docker ps
# List all containers (including stopped)
$ docker ps -a
# Stop container
$ docker stop my-nginx
# Start container (stopped container)
$ docker start my-nginx
# Restart container
$ docker restart my-nginx
# Delete container
$ docker rm my-nginx
# Execute command in running container
$ docker exec -it my-nginx bash
# Check container logs
$ docker logs -f my-nginx
이미지 관리 명령어 Image Management Commands
$ docker images
# 이미지 다운로드
$ docker pull nginx:latest
# Dockerfile로 이미지 빌드
$ docker build -t my-app:1.0 .
# 이미지 태그 추가
$ docker tag my-app:1.0 myregistry/my-app:1.0
# 이미지 푸시
$ docker push myregistry/my-app:1.0
# 이미지 삭제
$ docker rmi nginx:latest
$ docker images
# Download image
$ docker pull nginx:latest
# Build image from Dockerfile
$ docker build -t my-app:1.0 .
# Tag image
$ docker tag my-app:1.0 myregistry/my-app:1.0
# Push image
$ docker push myregistry/my-app:1.0
# Delete image
$ docker rmi nginx:latest
docker-compose: 멀티 컨테이너 관리 docker-compose: Multi-Container Management
실제 프로젝트에서는 웹 서버, 데이터베이스, 캐시 등 여러 컨테이너가 함께 동작합니다. docker-compose는 이런 멀티 컨테이너 환경을 YAML 파일로 정의하고 한 번에 관리할 수 있게 해줍니다.
In real projects, multiple containers like web servers, databases, and caches work together. docker-compose lets you define multi-container environments in a YAML file and manage them at once.
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: secret
$ docker-compose up -d
# 서비스 상태 확인
$ docker-compose ps
# 로그 확인
$ docker-compose logs -f
# 모든 서비스 정지 및 삭제
$ docker-compose down
# 볼륨까지 삭제
$ docker-compose down -v
$ docker-compose up -d
# Check service status
$ docker-compose ps
# Check logs
$ docker-compose logs -f
# Stop and delete all services
$ docker-compose down
# Delete including volumes
$ docker-compose down -v
정리 명령어 (Clean Up) Clean Up Commands
Docker를 사용하다 보면 사용하지 않는 이미지, 컨테이너, 볼륨이 쌓입니다. 디스크 공간을 확보하기 위한 정리 명령어입니다.
Using Docker accumulates unused images, containers, and volumes. Here are cleanup commands to free up disk space.
$ docker container prune
# 사용하지 않는 이미지 삭제
$ docker image prune
# 전체 정리 (컨테이너, 이미지, 네트워크)
$ docker system prune
# 볼륨까지 포함 전체 정리 (주의!)
$ docker system prune -a --volumes
$ docker container prune
# Delete unused images
$ docker image prune
# Full cleanup (containers, images, networks)
$ docker system prune
# Full cleanup including volumes (caution!)
$ docker system prune -a --volumes
핵심 정리 Key Summary
| 상황 | 명령어 |
|---|---|
| 이미지로 컨테이너 실행 | docker run -d -p 80:80 nginx |
| 실행 중인 컨테이너 확인 | docker ps |
| 컨테이너 내부 접속 | docker exec -it container bash |
| 로그 실시간 확인 | docker logs -f container |
| 이미지 빌드 | docker build -t name:tag . |
| 멀티 컨테이너 시작 | docker-compose up -d |
| Situation | Command |
|---|---|
| Run container from image | docker run -d -p 80:80 nginx |
| Check running containers | docker ps |
| Access container shell | docker exec -it container bash |
| Real-time log viewing | docker logs -f container |
| Build image | docker build -t name:tag . |
| Start multi-container | docker-compose up -d |
Docker 명령어는 처음에는 복잡해 보이지만, 컨테이너 라이프사이클의 개념을 이해하면 각 명령어가 어디에 속하는지 쉽게 파악할 수 있습니다. 실무에서는 docker-compose를 통해 대부분의 작업을 처리하게 되니, 기본 명령어를 익힌 후 docker-compose 사용법을 깊이 학습하시길 추천합니다.
Docker commands look complex at first, but once you understand the container lifecycle concept, you can easily identify where each command belongs. In practice, most work is done through docker-compose, so I recommend learning docker-compose deeply after mastering basic commands.