Skip to content

Commit d766559

Browse files
committed
docker conf updated
1 parent cd8635a commit d766559

File tree

2 files changed

+87
-19
lines changed

2 files changed

+87
-19
lines changed

backend/Dockerfile

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
11
FROM python:3.11-slim
22

3-
WORKDIR /app
4-
RUN pip install --no-cache-dir gunicorn
5-
6-
COPY requirements.txt .
7-
RUN pip install --no-cache-dir -r requirements.txt
3+
# Set environment variables
4+
ENV PYTHONUNBUFFERED=1 \
5+
PYTHONDONTWRITEBYTECODE=1 \
6+
ENV_MODE="production" \
7+
PYTHONPATH=/app
88

9+
WORKDIR /app
910

10-
# Copy the .env file first
11-
# COPY .env . # ATTENTION. We shouldn't copy secrets to the image
11+
# Install system dependencies
12+
RUN apt-get update && apt-get install -y --no-install-recommends \
13+
build-essential \
14+
curl \
15+
&& rm -rf /var/lib/apt/lists/*
1216

13-
# Copy the backend code
14-
COPY . .
17+
# Create non-root user and set up directories
18+
RUN useradd -m -u 1000 appuser && \
19+
mkdir -p /app/logs && \
20+
chown -R appuser:appuser /app
1521

16-
# Set environment variable
17-
ENV PYTHONPATH=/app
22+
# Install Python dependencies
23+
COPY --chown=appuser:appuser requirements.txt .
24+
RUN pip install --no-cache-dir -r requirements.txt gunicorn
1825

26+
# Switch to non-root user
27+
USER appuser
1928

20-
ENV ENV_MODE="production"
29+
# Copy application code
30+
COPY --chown=appuser:appuser . .
2131

2232
# Expose the port the app runs on
2333
EXPOSE 8000
2434

25-
# 24 workers
26-
CMD ["gunicorn", "api:app", "--workers", "24", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "--timeout", "600", "--graceful-timeout", "300", "--keep-alive", "250", "--max-requests", "0", "--max-requests-jitter", "0", "--forwarded-allow-ips", "*", "--worker-connections", "5000", "--worker-tmp-dir", "/dev/shm", "--preload"]
35+
# Calculate optimal worker count based on 16 vCPUs
36+
# Using (2*CPU)+1 formula for CPU-bound applications
37+
ENV WORKERS=33
38+
ENV THREADS=2
39+
ENV WORKER_CONNECTIONS=2000
40+
41+
# Gunicorn configuration
42+
CMD ["sh", "-c", "gunicorn api:app \
43+
--workers $WORKERS \
44+
--worker-class uvicorn.workers.UvicornWorker \
45+
--bind 0.0.0.0:8000 \
46+
--timeout 600 \
47+
--graceful-timeout 300 \
48+
--keep-alive 250 \
49+
--max-requests 2000 \
50+
--max-requests-jitter 400 \
51+
--forwarded-allow-ips '*' \
52+
--worker-connections $WORKER_CONNECTIONS \
53+
--worker-tmp-dir /dev/shm \
54+
--preload \
55+
--log-level info \
56+
--access-logfile - \
57+
--error-logfile - \
58+
--capture-output \
59+
--enable-stdio-inheritance \
60+
--threads $THREADS"]

backend/docker-compose.yml

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
- .env
1212
volumes:
1313
- .:/app
14+
- ./logs:/app/logs
1415
restart: unless-stopped
1516
depends_on:
1617
redis:
@@ -21,6 +22,26 @@ services:
2122
- REDIS_HOST=redis
2223
- REDIS_PORT=6379
2324
- REDIS_PASSWORD=
25+
- LOG_LEVEL=INFO
26+
logging:
27+
driver: "json-file"
28+
options:
29+
max-size: "10m"
30+
max-file: "3"
31+
deploy:
32+
resources:
33+
limits:
34+
cpus: '14'
35+
memory: 48G
36+
reservations:
37+
cpus: '8'
38+
memory: 32G
39+
healthcheck:
40+
test: ["CMD", "curl", "-f", "http://localhost:8000/api/health"]
41+
interval: 30s
42+
timeout: 10s
43+
retries: 3
44+
start_period: 40s
2445

2546
redis:
2647
image: redis:7-alpine
@@ -31,13 +52,26 @@ services:
3152
restart: unless-stopped
3253
networks:
3354
- app-network
34-
command: redis-server --appendonly yes --bind 0.0.0.0 --protected-mode no
55+
command: redis-server --appendonly yes --bind 0.0.0.0 --protected-mode no --maxmemory 8gb --maxmemory-policy allkeys-lru
3556
healthcheck:
3657
test: ["CMD", "redis-cli", "ping"]
37-
interval: 5s
38-
timeout: 3s
39-
retries: 3
40-
start_period: 5s
58+
interval: 10s
59+
timeout: 5s
60+
retries: 5
61+
start_period: 10s
62+
logging:
63+
driver: "json-file"
64+
options:
65+
max-size: "10m"
66+
max-file: "3"
67+
deploy:
68+
resources:
69+
limits:
70+
cpus: '2'
71+
memory: 12G
72+
reservations:
73+
cpus: '1'
74+
memory: 8G
4175

4276
networks:
4377
app-network:

0 commit comments

Comments
 (0)