Skip to content

Latest commit

ย 

History

History
185 lines (148 loc) ยท 4.25 KB

docker.md

File metadata and controls

185 lines (148 loc) ยท 4.25 KB

Docker ๊ธฐ๋ณธ ๊ฐœ๋…์— ๋Œ€ํ•œ ๊ธฐ์ˆ  ๋ฉด์ ‘ ๋‹ต๋ณ€

์ฃผ์š” ์งˆ๋ฌธ

"Docker์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ์ปจํ…Œ์ด๋„ˆํ™”์˜ ์žฅ์ ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๊ฐ€์ƒํ™”(VM)์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?"

1. Docker ํ•ต์‹ฌ ๊ฐœ๋…

1.1 ์ปจํ…Œ์ด๋„ˆ vs ๊ฐ€์ƒํ™”

[๊ธฐ์กด ๊ฐ€์ƒํ™”]               [Docker ์ปจํ…Œ์ด๋„ˆ]
+----------------+         +----------------+
| App A | App B  |         | App A | App B  |
+----------------+         +----------------+
| Guest OS  A|B |         |  Container A|B |
+----------------+         +----------------+
| Hypervisor    |         | Docker Engine  |
+----------------+         +----------------+
| Host OS       |         | Host OS        |
+----------------+         +----------------+
| Infrastructure |         | Infrastructure |
+----------------+         +----------------+

1.2 Docker ๊ธฐ๋ณธ ๊ตฌ์„ฑ์š”์†Œ

# Dockerfile ์˜ˆ์‹œ
FROM openjdk:11-jdk
WORKDIR /app
COPY target/myapp.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

# Docker Image ๋นŒ๋“œ
docker build -t myapp:1.0 .

# Docker Container ์‹คํ–‰
docker run -d -p 8080:8080 myapp:1.0

2. Docker ์ฃผ์š” ๋ช…๋ น์–ด์™€ ํ™œ์šฉ

2.1 ์ด๋ฏธ์ง€ ๊ด€๋ฆฌ

# ์ด๋ฏธ์ง€ ์กฐํšŒ
docker images

# ์ด๋ฏธ์ง€ ํ’€
docker pull nginx:latest

# ์ด๋ฏธ์ง€ ์‚ญ์ œ
docker rmi nginx:latest

# ์ด๋ฏธ์ง€ ๋นŒ๋“œ
docker build -t myapp:latest .

2.2 ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ฆฌ

# ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰
docker run -d --name mycontainer -p 8080:8080 myapp:latest

# ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ์กฐํšŒ
docker ps
docker ps -a  # ์ค‘์ง€๋œ ์ปจํ…Œ์ด๋„ˆ ํฌํ•จ

# ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€/์‹œ์ž‘
docker stop mycontainer
docker start mycontainer

# ์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ํ™•์ธ
docker logs mycontainer
docker logs -f mycontainer  # ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ

3. Dockerfile ์ž‘์„ฑ ์˜ˆ์‹œ

3.1 Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

# Multi-stage build ์˜ˆ์‹œ
FROM maven:3.8.4-openjdk-11 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

FROM openjdk:11-jre-slim
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

3.2 Node.js ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

4. Docker Compose

4.1 ๋‹ค์ค‘ ์„œ๋น„์Šค ๊ตฌ์„ฑ

# docker-compose.yml
version: '3'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=prod
      - DB_HOST=db
    depends_on:
      - db
  
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=rootpass
      - MYSQL_DATABASE=myapp
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:

๊ทผ๊ฑฐ ์ž๋ฃŒ

1. ๊ณต์‹ ๋ฌธ์„œ

2. ๋ชจ๋ฒ” ์‚ฌ๋ก€

3. ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ

์‹ค๋ฌด ๊ด€๋ จ ์ถ”๊ฐ€ ์งˆ๋ฌธ

  1. "Docker ์ปจํ…Œ์ด๋„ˆ์˜ ๋กœ๊ทธ ๊ด€๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํ•˜์‹œ๋‚˜์š”?"

  2. "์ปจํ…Œ์ด๋„ˆ ๋ชจ๋‹ˆํ„ฐ๋ง์€ ์–ด๋–ค ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋‚˜์š”?"

  3. "Docker ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ๊ฐ€์š”?"

  4. "Docker ๋ณด์•ˆ์„ ์œ„ํ•ด ์–ด๋–ค ์กฐ์น˜๋“ค์„ ์ทจํ•˜์‹œ๋‚˜์š”?"

์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์‹œ

์ปจํ…Œ์ด๋„ˆ ๋ฆฌ์†Œ์Šค ์ œํ•œ

docker run -d \
  --name myapp \
  --memory="512m" \
  --cpus="1.0" \
  -p 8080:8080 \
  myapp:latest

ํ—ฌ์Šค์ฒดํฌ ์„ค์ •

HEALTHCHECK --interval=30s --timeout=3s \
  CMD wget --quiet --tries=1 --spider http://localhost:8080/health || exit 1

๋กœ๊ทธ ์„ค์ •

# JSON ๋กœ๊ทธ ๋“œ๋ผ์ด๋ฒ„ ์‚ฌ์šฉ
docker run -d \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  myapp:latest

์‹ค์ œ ๋ฉด์ ‘์—์„œ๋Š” ์ด๋ก ์ ์ธ ์ง€์‹๊ณผ ํ•จ๊ป˜ Docker๋ฅผ ์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ–ˆ๋Š”์ง€, ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๋“ค์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€ ๊ตฌ์ฒด์ ์ธ ๊ฒฝํ—˜์„ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.