-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
60 lines (45 loc) · 1.65 KB
/
Copy pathDockerfile
File metadata and controls
60 lines (45 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Stage 1: Base build stage
FROM python:3.13-slim AS builder
# Create the app directory
RUN mkdir /app
# Set the working directory
WORKDIR /app
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Upgrade pip and install dependencies
RUN pip install --upgrade pip
# Copy the requirements file first (better caching)
COPY requirements.txt /app/
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Stage 2: Production stage
FROM python:3.13-slim
# Create user, app directory, and ensure all necessary directories are created with correct permissions
RUN useradd -m -r appuser && \
mkdir -p /app && \
mkdir -p /app/db && \
mkdir -p /app/staticfiles && \
mkdir -p /app/media/vaults && \
chown -R appuser:appuser /app && \
chown -R appuser:appuser /app/db && \
chown -R appuser:appuser /app/staticfiles && \
chown -R appuser:appuser /app/media
# Copy the Python dependencies from the builder stage
COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/
# Set the working directory
WORKDIR /app
# Copy application code
COPY --chown=appuser:appuser . .
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Switch to non-root user
USER appuser
# Expose the application port
EXPOSE 8000
# Make migrations & Start the application using Gunicorn
CMD python manage.py migrate && \
python manage.py collectstatic --noinput && \
gunicorn keepmesafe.wsgi:application --bind 0.0.0.0:8000 --workers 3