Skip to main content
The RAG Support System includes Docker support for containerized deployment, making it easy to run the entire stack with a single command.

Prerequisites

Ensure you have Docker and Docker Compose installed on your machine before proceeding.
  • Docker: Version 20.10 or higher
  • Docker Compose: Version 2.0 or higher
  • Environment variables: .env file with required API keys

Quick start

1

Clone the repository

git clone https://github.com/JoAmps/rgt-assignment.git
cd rgt-assignment
2

Create .env file

Create a .env file in the project root with your API keys:
OPENAI_API_KEY=your_openai_api_key
UNSTRUCTURED_API_KEY=your_unstructured_api_key
See environment variables for complete configuration.
3

Build and run

Build the Docker image and start the services:
docker-compose up --build
The API will be available at http://localhost:8000.
4

Verify deployment

Test the health endpoint:
curl http://localhost:8000/api/v1/health
Expected response:
{"status": "ok"}

Docker configuration

Dockerfile

The system uses a multi-stage build for optimized image size:
# Stage 1: Builder
FROM python:3.12-slim AS builder
WORKDIR /app

# Install build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential git curl \
    && rm -rf /var/lib/apt/lists/*

# Create virtual environment and install dependencies
RUN python -m venv /app/.venv
RUN /app/.venv/bin/pip install --upgrade pip
COPY pyproject.toml poetry.lock* ./
RUN /app/.venv/bin/pip install .

# Stage 2: Runtime
FROM python:3.12-slim AS runtime

# Create non-root user for security
RUN groupadd -r appgroup && useradd -r -g appgroup appuser
WORKDIR /app

# Copy venv from builder stage
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONPATH=/app

# Copy source code
COPY --chown=appuser:appgroup src/ ./src/
COPY --chown=appuser:appgroup main.py ./
COPY --chown=appuser:appgroup .env ./

# Create reports directory
RUN mkdir -p reports && chown appuser:appgroup reports

# Switch to non-root user
USER appuser

# Expose FastAPI port
EXPOSE 8000

# Run the application
CMD ["python", "main.py", "--host", "0.0.0.0", "--port", "8000"]
Key features:
  • Multi-stage build reduces final image size
  • Non-root user (appuser) for security
  • Virtual environment isolation
  • Only runtime dependencies in final image

docker-compose.yml

The Docker Compose configuration manages the service:
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: rag_api
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - "8000:8000"
    volumes:
      - ./reports:/app/reports
      - ./kb_docs:/app/kb_docs
      - ./artifacts:/app/artifacts
      - ./chroma_db:/app/chroma_db
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 5
Configuration details:
  • Port mapping: 8000:8000 exposes the FastAPI server
  • Volumes: Persist data, models, and reports on the host
  • Health check: Monitors service availability every 30 seconds
  • Restart policy: Automatically restarts on failures

Volume mounts

The following directories are mounted for data persistence:
VolumePurpose
./reportsEvaluation reports and metrics
./kb_docsKnowledge base documents
./artifactsTrained ML models
./chroma_dbChroma vector database
Ensure these directories exist before running docker-compose up, or Docker will create them with root ownership.

Common operations

Running in detached mode

docker-compose up -d

Viewing logs

# Follow logs
docker-compose logs -f

# View last 100 lines
docker-compose logs --tail=100

Stopping the service

docker-compose down

Rebuilding after code changes

docker-compose down
docker-compose up --build

Accessing the container shell

docker exec -it rag_api /bin/bash

Health checks

The container includes an automated health check:
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/health"]
  interval: 30s
  timeout: 10s
  retries: 5
Check health status:
docker inspect --format='{{.State.Health.Status}}' rag_api

Production considerations

The current configuration is suitable for development. For production deployment:
  • Remove .env from image: Use Docker secrets or environment variables instead of copying .env into the image
  • Use specific base image tags: Pin python:3.12-slim to a specific version (e.g., python:3.12.1-slim)
  • Enable TLS: Place the service behind a reverse proxy (nginx, Traefik) with HTTPS
  • Resource limits: Add CPU and memory constraints to docker-compose.yml
  • Logging: Configure structured logging with log drivers
  • Monitoring: Integrate with Prometheus/Grafana for metrics
See production deployment for detailed guidance.

Troubleshooting

Check logs:
docker-compose logs app
Common causes:
  • Missing environment variables in .env
  • Port 8000 already in use
  • Insufficient disk space
Solution:
# Check port usage
lsof -i :8000

# Free up disk space
docker system prune
Verify the service is running:
docker exec -it rag_api curl http://localhost:8000/api/v1/health
Common causes:
  • Application startup time exceeds timeout
  • FastAPI server not listening on 0.0.0.0
  • Missing dependencies
Solution: Increase timeout and start_period in the healthcheck configuration.
Error: PermissionError: [Errno 13] Permission deniedCause: Volume directories created by Docker with root ownershipSolution:
# Create directories before running docker-compose
mkdir -p reports kb_docs artifacts chroma_db

# Or fix ownership after creation
sudo chown -R $USER:$USER reports kb_docs artifacts chroma_db
Check image size:
docker images | grep rag
Optimization tips:
  • Multi-stage build already minimizes size
  • Remove unnecessary dependencies from pyproject.toml
  • Use .dockerignore to exclude test files and documentation
  • Consider using python:3.12-alpine for smaller base image

Next steps

Environment variables

Configure API keys and system settings

Production deployment

Deploy to production with monitoring and scaling

API reference

Explore the API endpoints

Data ingestion

Load documents into the knowledge base

Build docs developers (and LLMs) love