1 ๋ถ„ ์†Œ์š”

Dockerfile ๊ธฐ๋ณธ ๊ตฌ์กฐ

Dockerfile ์€ ๊ฐ ์ค„์ด ๋ช…๋ น์–ด๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๊ฐ ๋ช…๋ น์–ด๋Š” ์ด๋ฏธ์ง€์˜ ํ•œ ๋ ˆ์ด์–ด๋ฅผ ์ •์˜ํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด๋Š” FROM, COPY, RUN, CMD, EXPOSE, ENTRYPOINT, ENV ๋“ฑ์ด ์žˆ๋‹ค!

Dockerfile ๋ช…๋ น์–ด

1. FROM

FROM ubuntu:20.04
  • ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๋ฅผ ์ง€์ •ํ•จ.
  • Dockerfile์€ ํ•ญ์ƒ FROM์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.

2. RUN

RUN apt-get update && apt-get install -y python3
  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•จ.
  • ์ผ๋ฐ˜์ ์œผ๋กœ ํŒจํ‚ค์ง€ ์„ค์น˜์™€ ๊ฐ™์€ ์ž‘์—…์— ์‚ฌ์šฉ๋จ.

3. COPY

COPY . /app
  • ๋กœ์ปฌ ํŒŒ์ผ์„ ์ปจํ…Œ์ด๋„ˆ์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์œผ๋กœ ๋ณต์‚ฌํ•จ.

4. ADD

ADD <https://example.com/file.tar.gz> /app
  • COPY ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, URL์—์„œ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์••์ถ• ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ํ•ด์ œํ•  ์ˆ˜ ์žˆ์Œ.

5. CMD

CMD ["python3", "/app/main.py"]
  • ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ์‹คํ–‰๋  ๋ช…๋ น์„ ์ง€์ •ํ•จ.
  • Dockerfile์—์„œ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ!

6. ENTRYPOINT

ENTRYPOINT ["python3", "/app/main.py"]
  • ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ์‹คํ–‰๋  ๋ช…๋ น์„ ์ง€์ •ํ•จ.
  • CMD์™€ ์ฐจ์ด์ ?
    • ์„ค์ •ํ•œ ๋ช…๋ น์–ด๋Š” ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ์‹œ, ์ถ”๊ฐ€ ์ธ์ž๋กœ ๋ฎ์–ด์“ธ ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ด๋‹ค!

7. ENV

ENV APP_HOME /app
  • ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•จ.

8. EXPOSE

EXPOSE 80
  • ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋…ธ์ถœํ•  ํฌํŠธ๋ฅผ ์ง€์ •ํ•จ.
  • ๋ฌธ์„œํ™”๊ฐ€ ๋ชฉ์ ์ด๋ฉฐ, ์‹ค์ œ ํฌํŠธ ๋ฐ”์ธ๋”ฉ์€ docker run -p ๋ช…๋ น์–ด๋กœ ์ˆ˜ํ–‰๋œ๋‹ค!

9. WORKDIR

WORKDIR /app
  • ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  • RUN, CMD, ENTRYPOINT ๋ช…๋ น์–ด๋Š” ์ด ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์‹คํ–‰๋จ.

10. USER

USER appuser
  • ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์‚ฌ์šฉ์ž์˜ ๊ณ„์ •์„ ์„ค์ •ํ•จ.

11. VOLUME

VOLUME /data
  • ํ˜ธ์ŠคํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ณต์œ ํ•จ.

12. ARG

ARG VERSION=1.0
  • ๋นŒ๋“œ ์‹œ์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•จ.

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

Python & Flask ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ Dockerfile ์˜ˆ์ œ

# ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๋กœ Python 3.10์„ ์‚ฌ์šฉ
FROM python:3.10-slim

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

# ์˜์กด์„ฑ ํŒŒ์ผ์„ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ(./)์— ๋ณต์‚ฌํ•˜๊ณ  ์„ค์น˜
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

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

# ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ์‹œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
CMD ["python3", "app.py"]
  • FROM python:3.10-slim
    • slim ๋ฒ„์ „์€ ํ•„์š” ์—†๋Š” ํŒจํ‚ค์ง€๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•„ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์Œ.
  • WORKDIR /app
    • ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ •.
    • /app ๋””๋ ‰ํ† ๋ฆฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํŒŒ์ผ์ด ๋ณต์‚ฌ๋  ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์˜๋ฏธํ•จ.
  • pip install --no-cache-dir -r requirements.txt
    • --no-cache-dir ์˜ต์…˜์œผ๋กœ pip ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ํ•˜์—ฌ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ž„.
  • COPY . .
    • ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ์ปจํ…Œ์ด๋„ˆ์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ(โ€/appโ€)๋กœ ๋ณต์‚ฌํ•จ.
    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์™€ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์ด ์ปจํ…Œ์ด๋„ˆ์— ๋ณต์‚ฌ๋จ.
  • CMD ["python3", "app.py"]
    • ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋กœ, python3 app.py ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋œ๋‹ค!

Dockerfile ๋นŒ๋“œํ•˜๊ธฐ

# Dockerfile์„ ๋นŒ๋“œํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑ
docker build -t myapp:latest .
  • t: ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€์˜ ํƒœ๊ทธ๋ฅผ ์ง€์ •
  • .: Dockerfile์˜ ์œ„์น˜๋ฅผ ์ง€์ •

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ