1 ๋ถ„ ์†Œ์š”

๊ธฐ๋Šฅ ๋ณ„ ๊ธฐ์ˆ (ํ”„๋ ˆ์ž„์›Œํฌ)์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ, Docker๋ฅผ ์‚ฌ์šฉํ•ด MSA๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•?!

์šฐ์„  Django ๊ธฐ๋Šฅ 1๊ฐœ์™€ FastAPI ๊ธฐ๋Šฅ 1๊ฐœ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ MSA๋ฅผ ๊ตฌ์ถ•ํ•ด๋ณด์ž!

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

DOCKER_MSA/
โ”œโ”€โ”€ django_web/
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ”œโ”€โ”€ manage.py
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ chatbot/
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ .gitmodules
โ”œโ”€โ”€ .env

๋‚˜๋Š” git submodule ์„ ์‚ฌ์šฉํ•ด ๊ฐ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜์˜ ๋ ˆํฌ์ง€ํ† ๋ฆฌ(Docker_MSA)์—์„œ ๊ด€๋ฆฌํ•˜๊ณ , ์ด ๋ ˆํฌ๋ฅผ Docker-Compose ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐํฌ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค!

์ด ํ”„๋กœ์ ํŠธ๋Š” ํ˜„์žฌ ์ง„ํ–‰ ์ค‘(24.07.22)์ด๋ฏ€๋กœ, ์—ฌ๋Ÿฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์—ฐ๊ฒฐํ•  ์˜ˆ์ •์ด๊ณ , ์—ฐ๊ฒฐ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ ๋˜ํ•œ ์ •๋ฆฌํ•  ์˜ˆ์ •์ด๋‹ค.

Docker_MSA/.env

DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=

OPENAI_API_KEY=
  • chatbot ๊ธฐ๋Šฅ์€ chatGPT๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— OPENAI_API_KEY ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

Docker_MSA/.gitmodules

[submodule "chatbot"]
	path = chatbot
	url = [Repository URL]

[submodule "django_web"]
	path = django_web
	url = [Repository URL]
	branch = test
  • django_web ์ด๋ผ๋Š” ๋ ˆํฌ๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด test ๋ผ๋Š” ๋ธŒ๋žœ์น˜๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , ์ด ๋ธŒ๋žœ์น˜๋ฅผ ์—ฐ๊ฒฐํ•จ
  • Docker_MSA ๋ ˆํฌ์—์„œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด chatbot๊ณผ django_web ๋ ˆํฌ๋ฅผ ํ•œ ๋ฒˆ์— ๋ถˆ๋Ÿฌ์™€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค!
    • git submodule update --init --recursive ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, chatbot๊ณผ django_web ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด

Docker_MSA/docker-compose.yml

version: '3.8'

services:
  db:
    image: postgres:16
    restart: always
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    ports:
      - "${DB_PORT}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  django_web:
    build:
      context: ./django_web
    ports:
      - "8000:8000"
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    env_file:
      - .env
    depends_on:
      - db
    networks:
      - app-network

  chatbot:
    build:
      context: ./chatbot
    ports:
      - "8001:8000"
    env_file:
      - .env
    depends_on:
      - db
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  postgres_data:
  • ์—ฌ๊ธฐ์„œ ๊ฐ service ์˜ ports ๋ฅผ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค!
    • ๊ฐ ports๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด์ค˜์•ผ ํฌํŠธ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ์Œ

django_web/Dockerfile

FROM python:3.9-slim

WORKDIR /app

# ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์„ค์น˜
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]

chatbot/Dockerfile

FROM python:3.8

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ํ›„, docker-compose up --build ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ๋นŒ๋“œ๋ฅผ ๋งˆ์น  ์ˆ˜ ์žˆ๋‹ค!

๋ฐฐํฌ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด?

๋ฐฐํฌ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•˜๋ฉด ๋ ๊นŒ?

์•„๊นŒ docker-compose.yml ์—์„œ ๊ฐ ์„œ๋น„์Šค ๋ณ„๋กœ port ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ–ˆ๋‹ค.

์ฆ‰, ๊ฐ ์„œ๋น„์Šค ๋ณ„๋กœ ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์œผ๋ฉด docker-compose.yml ์—์„œ ์ง€์ •ํ•œ port ๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋œ๋‹ค!

๐Ÿ’กย ๋„ˆ๋ฌด๋‚˜๋„ ๊ธฐ์ดˆ์ ์ธ ์ •๋ณด์ผ ์ˆ˜ ์žˆ๊ณ , ์ž˜๋ชป๋œ ์ •๋ณด ํ˜น์€ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์ฐพ๊ณ  ๊ณ ์ณ ์ฝ”๋“œ์™€ ๊ธ€ ๋ชจ๋‘ ์ˆ˜์ •ํ•ด ๋‚˜์•„๊ฐˆ ์˜ˆ์ •!

์ฐธ๊ณ  ์ž๋ฃŒ

Git - ์„œ๋ธŒ๋ชจ๋“ˆ

9.2: submodule ๊ฐฑ์‹  (update) ํ•˜๊ธฐ

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