-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDockerfile
69 lines (54 loc) · 1.57 KB
/
Dockerfile
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
61
62
63
64
65
66
67
68
69
# Stage 1: Build stage
FROM python:3.8-alpine AS builder
# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /usr/src/app
# Install build dependencies
RUN apk add --no-cache \
build-base \
gcc \
musl-dev \
libc-dev \
postgresql-dev \
libffi-dev \
openssl-dev \
jpeg-dev \
zlib-dev \
libxslt-dev \
&& apk add --no-cache --virtual .build-deps linux-headers
# Upgrade pip and install pip-tools for dependency management
RUN pip install --upgrade pip pip-tools
# Copy requirements file and compile dependencies
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application source code
COPY . .
# Stage 2: Runtime stage
FROM python:3.8-alpine
# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /usr/src/app
# Install runtime dependencies
RUN apk add --no-cache \
libpq \
libffi \
jpeg \
zlib \
libxslt
# Copy dependencies from the builder image
COPY --from=builder /usr/src/app /usr/src/app
# Add a non-root user and set permissions
RUN adduser -D -H -u 1001 django \
&& chown -R django /usr/src/app \
&& mkdir -p /usr/src/app/logging /usr/src/app/staticfiles /usr/src/app/media \
&& chown -R django /usr/src/app/logging /usr/src/app/staticfiles /usr/src/app/media
# Switch to non-root user
USER django
# Expose the application port (if applicable)
EXPOSE 8000
# Run the entrypoint script
ENTRYPOINT ["/usr/src/app/entrypoint.prod.sh"]