본문으로 건너뛰기

© 2026 Molayo

Dev.to헤드라인2026. 05. 16. 03:51

AI 개발을 위한 Docker: LLM 애플리케이션 컨테이너화

요약

Docker를 활용하면 AI 애플리케이션을 개발 환경부터 운영 환경까지 일관성 있게 배포할 수 있습니다. 본 가이드는 Claude 및 ofox.ai와 같은 LLM 기반 서비스를 컨테이너화하는 방법을 Dockerfile, Docker Compose 예시 코드와 함께 제시합니다. 이를 통해 재현 가능한 환경 구축, 의존성 격리, 그리고 쉬운 클라우드 배포가 가능해집니다.

핵심 포인트

  • Docker는 AI 애플리케이션의 개발-배포 전 과정에 걸쳐 일관된 환경을 보장하여 운영 안정성을 높입니다.
  • Dockerfile은 Python 기반 LLM 서비스를 위한 기본 구조를 제공하며, 시스템 의존성 설치와 패키지 관리를 포함합니다.
  • Docker Compose를 사용하면 API 서비스(FastAPI)와 Redis 캐시 같은 보조 컴포넌트를 정의하고 통합적으로 관리할 수 있습니다.
  • 제공된 예시는 FastAPI를 사용하여 ofox.ai의 Claude 모델을 호출하는 실제 LLM 채팅 기능을 구현한 'Production-Ready' 구조입니다.

Docker는 개발부터 운영(production)까지 일관된 환경을 제공함으로써 AI 애플리케이션 배포를 단순화합니다. Claude 및 ofox.ai로 구동되는 AI 애플리케이션을 컨테이너화하는 방법은 다음과 같습니다.

AI 앱에 Docker를 사용하는 이유?

  • 재현 가능한 환경 (Reproducible environments) — 로컬과 운영 환경에서 동일한 동작 보장
  • 의존성 격리 (Dependency isolation) — Python 패키지, 시스템 라이브러리, CUDA 버전 관리
  • 쉬운 배포 (Easy deployment) — Docker를 통해 어떤 클라우드로든 전송 가능
  • 리소스 제어 (Resource control) — 컨테이너당 CPU/메모리 제한

AI 앱을 위한 기본 Dockerfile

Dockerfile
FROM python:3.11-slim
WORKDIR /app

# 시스템 의존성 설치
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# (캐싱을 위해) requirements를 먼저 복사
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 애플리케이션 복사
COPY . .

# 환경 변수 설정
ENV PYTHONUNBUFFERED=1

# 애플리케이션 실행
CMD ["python", "main.py"]

requirements.txt

fastapi==0.109.0
uvicorn==0.27.0
httpx==0.26.0
python-dotenv==1.0.0

AI 서비스를 위한 Docker Compose

docker-compose.yml
version: '3.8'
services:
  api:
    build: .

ports: "8000:8000"
environment:
- OFOXAPIKEY=${OFOXAPIKEY}
- MODEL=claude-3-5-sonnet-20241022
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3

Optional: Redis 캐시 추가
redis:
image: redis:7-alpine
ports: "6379:6379"
volumes:
- redis-data:/data

volumes:
redis-data:

Production-Ready FastAPI + ofox.ai

python main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import httpx
import os

app = FastAPI(title="Claude API Service", version="1.0.0")

class Message(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[Message]
    model: str = "claude-3-5-sonnet-20241022"
    max_tokens: Optional[int] = 1024
    temperature: Optional[float] = 0.7

@app.get("/health")
async def health():
    return {"status": "healthy"}

@app.post("/chat")
async def chat(request: ChatRequest):
    async with httpx.AsyncClient(timeout=120.0) as client:
        try:
            response = await client.post(
                "https://api.ofox.ai/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {os.environ['OFOXAPIKEY']}",
                    "Content-Type": "application/json"
                },
                json={
                    "model": request.model,
                    "messages": [m.model_dump() for m in request.messages],
                    "max_tokens": request.max_tokens,
                    "temperature": request.temperature
                }
            )
            response.raise_for_status()
            data = response.json()
            return {
                "content": data["choices"][0]["message"]["content"],
                "model": data["model"],
                "tokens": data["usage"]["total_tokens"]
            }
        except httpx.HTTPStatusError as e:
            raise HTTPException(status_code=e.response.status_code, detail=str(e))
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

로컬 모델을 위한 GPU 지원

dockerfile

GPU 지원이 포함된 Dockerfile

FROM nvidia/cuda:12.1.0-base-ubuntu22.04

RUN apt-get update && apt-get install -y \
    python3.11 \
    python3.11-venv \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY requirements.txt .

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

Ollama와 같은 로컬 모델 (local models)을 실행하기 위한 명령:
RUN curl -fsSL https://ollama.ai/install.sh | sh

COPY . .
CMD ["python3", "main.py"]

# GPU 서비스를 포함한 docker-compose.yml
services:
  api:
    build: .
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

멀티 스테이지 빌드 (Multi-Stage Build) (더 작은 이미지 생성)

`dockerfile

빌드 단계 (Build stage)

FROM python:3.11-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt

프로덕션 단계 (Production stage)

FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
ENV PATH=/root/.local/bin:$PATH
CMD ["python", "main.py"]


` `
### 환경 기반 설정 (Environment-Based Configuration)

`python
config.py
import os
from dataclasses import dataclass

@dataclass
class Config:
    api_key: str
    model: str
    max_tokens: int
    temperature: float

def get_config() -> Config:
    return Config(
        api_key=os.environ["OFOXAPI_KEY"],
        model=os.environ.get("MODEL", "claude-3-5-sonnet-20241022"),
        max_tokens=int(os.environ.get("MAX_TOKENS", "1024")),
        temperature=float(os.environ.get("TEMPERATURE", "0.7"))
    )

빌드 및 실행 (Building and Running)

`bash

Docker 빌드

docker build -t claude-api-service .

Docker 실행

docker run -d -p 8000:8000
-e OFOXAPI_KEY=your-key-here
--name claude-api
claude-api-service

Docker Compose로 실행

docker-compose up -d

로그 확인

docker logs -f claude-api

컨테이너 내부로 접속 (Shell into container)

docker exec -it claude-api /bin/bash


` `
### GitHub Actions를 이용한 CI/CD

`yaml
name: Build and Deploy
on:
  push:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        name: Build image
        run: docker build -t claude-api:${{ github.sha }} .

name: 테스트 실행
  run: |
    docker run claude-api:${{ github.sha }} pytest

name: 레지스트리에 푸시
  run: |
    docker tag claude-api:${{ github.sha }} registry/app/claude-api:latest
    docker push registry/app/claude-api:latest

` Docker를 사용하면 어디에서나 배포할 수 있습니다. 당신의 AI 애플리케이션은 다음 서비스들에 배포됩니다:
- AWS ECS — 관리형 컨테이너 서비스 (Managed container service)
- Google Cloud Run — 서버리스 컨테이너 (Serverless containers)
- Azure Container Instances — 간편한 배포 (Simple deployment)
- DigitalOcean App Platform — 간편한 PaaS
- 자체 서버 — docker-compose를 사용하여

시작하기
AI 애플리케이션을 컨테이너화하고 자신 있게 배포하세요. 경쟁력 있는 가격과 99.9%의 가동 시간 (uptime)을 보장하는 신뢰할 수 있는 Claude API인 ofox.ai로 애플리케이션에 힘을 실어주세요.
👉 ofox.ai로 시작하기

이 기사에는 제휴 링크가 포함되어 있습니다.
태그: docker, devops, ai, programming, developer
Canonical URL: https://dev.to/zny10289

AI 자동 생성 콘텐츠

본 콘텐츠는 Dev.to AI tag의 원문을 AI가 자동으로 요약·번역·분석한 것입니다. 원 저작권은 원저작자에게 있으며, 정확한 내용은 반드시 원문을 확인해 주세요.

원문 바로가기
0

댓글

0