-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathDockerfile.base
More file actions
67 lines (56 loc) · 2.76 KB
/
Dockerfile.base
File metadata and controls
67 lines (56 loc) · 2.76 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
61
62
63
64
65
66
67
# Use the following commands to build and push multi-arch images to GitHub Container Registry
# docker buildx create --name mybuilder --driver=docker-container --use
# docker buildx build -t ghcr.io/raft-tech/tdp-backend-base:dev-02 -f Dockerfile.base . --platform linux/amd64,linux/arm64 --push
# =============================================================================
# Stage 1: Builder - Install build dependencies and compile Python packages
# =============================================================================
FROM python:3.10.8-slim-bullseye AS builder
COPY Pipfile Pipfile.lock /tdpapp/
WORKDIR /tdpapp/
# Install build dependencies, Python packages, then clean up in a single layer
RUN apt-get update && \
apt-get install -y --no-install-recommends \
gcc \
graphviz \
graphviz-dev \
libpq-dev \
python3-dev \
curl \
ca-certificates && \
pip install --no-cache-dir --upgrade pip pipenv && \
# If we can remove the '--dev' we will save 200MB. However, so much depends on it that it is easier to keep it for now.
pipenv install --dev --system --deploy && \
rm -rf /var/lib/apt/lists/*
# Download PostgreSQL GPG key in builder stage (so curl isn't needed in runtime)
RUN install -d /pgdg && \
curl -o /pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
# =============================================================================
# Stage 2: Runtime - Minimal image with only runtime dependencies
# =============================================================================
FROM python:3.10.8-slim-bullseye
ARG user=tdpuser
ARG group=tdpuser
ARG uid=1000
ARG gid=1000
ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE=tdpservice.settings.local
ENV DJANGO_CONFIGURATION=Local
WORKDIR /tdpapp/
# Copy PostgreSQL GPG key from builder
COPY --from=builder /pgdg/apt.postgresql.org.asc /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc
# Install only runtime dependencies and PostgreSQL client in a single layer
RUN apt-get update && \
apt-get install -y --no-install-recommends \
libpq5 \
graphviz \
ca-certificates && \
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
apt-get update && \
apt-get install -y --no-install-recommends postgresql-client-15 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
groupadd -g ${gid} ${group} && \
useradd -u ${uid} -g ${group} -s /bin/sh ${user}
# Copy Python packages from builder stage
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin