๊ฐœ๋ฐœ Code/๋ถ€๋ก Supplement

[Supplement][Docker] ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ๊ด€๋ฆฌ

5hr1rnp 2025. 2. 9. 20:23
๋ฐ˜์‘ํ˜•

๋„์ปค ์ด๋ฏธ์ง€๋Š” ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋ชจ๋“  ์š”์†Œ(์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํ™˜๊ฒฝ ์„ค์ • ๋“ฑ)๋ฅผ ํฌํ•จํ•˜๋Š” ์ฝ๊ธฐ ์ „์šฉ ํ…œํ”Œ๋ฆฟ์ด๋‹ค. ์ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋นŒ๋“œํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ๋‹ค.


1. ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ


1.1 ๋„์ปคํŒŒ์ผ(Dockerfile) ์ž‘์„ฑ

๋„์ปค ์ด๋ฏธ์ง€๋Š” Dockerfile์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑ๋จ

๊ธฐ๋ณธ์ ์ธ Dockerfile ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Œ:

# ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€ ์„ค์ •
FROM python:3.9

# ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ ์„ค์ •
WORKDIR /app

# ํ•„์š” ํŒจํ‚ค์ง€ ๋ณต์‚ฌ ๋ฐ ์„ค์น˜
COPY requirements.txt .
RUN pip install -r requirements.txt

# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ณต์‚ฌ
COPY . .

# ์‹คํ–‰ ๋ช…๋ น์–ด ์„ค์ •
CMD ["python", "app.py"]

1.2 ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ

์œ„ Dockerfile์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋จ:

docker build -t myapp:latest .

์˜ต์…˜ ์„ค๋ช…:

  • -t myapp:latest : ์ด๋ฏธ์ง€ ์ด๋ฆ„(myapp)๊ณผ ํƒœ๊ทธ(latest) ์ง€์ •
  • . : ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ Dockerfile์„ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œ

728x90
๋ฐ˜์‘ํ˜•

2. ๋„์ปค ์ด๋ฏธ์ง€ ๊ด€๋ฆฌ


2.1 ๋„์ปค ์ด๋ฏธ์ง€ ๋ชฉ๋ก ํ™•์ธ

๋นŒ๋“œ๋œ ๋„์ปค ์ด๋ฏธ์ง€๋Š” ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ:

docker images

2.2 ๋„์ปค ์ด๋ฏธ์ง€ ์‚ญ์ œ

ํŠน์ • ์ด๋ฏธ์ง€๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด:

docker rmi ์ด๋ฏธ์ง€์ด๋ฆ„ ๋˜๋Š” ์ด๋ฏธ์ง€ID

2.3 ์ด๋ฏธ์ง€ ํƒœ๊ทธ ๋ณ€๊ฒฝ

๊ธฐ์กด ์ด๋ฏธ์ง€๋ฅผ ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ํƒœ๊ทธํ•  ์ˆ˜ ์žˆ์Œ:

docker tag myapp:latest myrepo/myapp:v1.0

2.4 ๋„์ปค ํ—ˆ๋ธŒ(Docker Hub) ๋˜๋Š” ํ”„๋ผ์ด๋น— ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์— ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ

๋„์ปค ํ—ˆ๋ธŒ์— ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด ๋จผ์ € ๋กœ๊ทธ์ธ:

docker login

์ด๋ฏธ์ง€๋ฅผ ํ‘ธ์‹œํ•˜๋ ค๋ฉด:

docker push myrepo/myapp:v1.0

ํ”„๋ผ์ด๋น— ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์— ์—…๋กœ๋“œํ•˜๋ ค๋ฉด ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์ฃผ์†Œ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•จ:

docker tag myapp:latest myregistry.com/myapp:v1.0
docker push myregistry.com/myapp:v1.0

3. ๋„์ปค ์ด๋ฏธ์ง€ ์ตœ์ ํ™”


3.1 ๋ฉ€ํ‹ฐ์Šคํ…Œ์ด์ง€ ๋นŒ๋“œ ์‚ฌ์šฉ

๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๊ฒฝ๋Ÿ‰ํ™”ํ•˜๋ ค๋ฉด ๋ฉ€ํ‹ฐ์Šคํ…Œ์ด์ง€ ๋นŒ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ:

# ๋นŒ๋“œ ๋‹จ๊ณ„
FROM node:18 AS builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

# ์‹คํ–‰ ๋‹จ๊ณ„
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html

์ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ์„ ํฌํ•จํ•˜์ง€ ์•Š์•„ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

3.2 ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ ์ œ์™ธ

.dockerignore ํŒŒ์ผ์„ ์„ค์ •ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ์„ ์ œ์™ธํ•  ์ˆ˜ ์žˆ์Œ:

node_modules
*.log
.git

3.3 ๊ฒฝ๋Ÿ‰ ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€ ์‚ฌ์šฉ

ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ํฌํ•จํ•œ ๊ฒฝ๋Ÿ‰ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์šฉ๋Ÿ‰์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ.

  • ์ผ๋ฐ˜์ ์ธ python ์ด๋ฏธ์ง€ ๋Œ€์‹  python:alpine ์‚ฌ์šฉ
  • ubuntu ๋Œ€์‹  debian ๋˜๋Š” alpine ๊ธฐ๋ฐ˜ ์ด๋ฏธ์ง€ ์‚ฌ์šฉ
๋ฐ˜์‘ํ˜•