Docker Deployment¶
Containerize your REROUTE application.
Dockerfile¶
FROM python:3.11-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application
COPY . .
# Expose port
EXPOSE 8000
# Run application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Requirements¶
requirements.txt:
Build and Run¶
Docker Compose¶
docker-compose.yml:
version: '3.8'
services:
app:
build: .
ports:
- "8000:8000"
environment:
- REROUTE_PORT=8000
- REROUTE_DEBUG=False
volumes:
- ./app:/app/app
restart: unless-stopped
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app
restart: unless-stopped
Multi-stage Build¶
# Build stage
FROM python:3.11-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt
# Runtime stage
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
ENV PATH=/root/.local/bin:$PATH
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Docker Commands¶
# Build
docker-compose build
# Run
docker-compose up -d
# View logs
docker-compose logs -f
# Stop
docker-compose down
Production Tips¶
- Use multi-stage builds
- Run as non-root user
- Use .dockerignore
- Pin dependency versions
- Enable health checks
- Set resource limits
.dockerignore¶
Health Check¶
Add to Dockerfile: