Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
168 lines (127 loc) · 5 KB
/
Copy pathDockerfile
File metadata and controls
168 lines (127 loc) · 5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ==============================================================================
# PlexTraktSync Enhanced
#
# Multi-stage Docker build that clones PlexTraktSync from upstream source,
# applies optional .patch files from the Patches/ directory, then builds a
# minimal runtime image.
# ==============================================================================
ARG PYTHON_IMAGE=python:3.15.1-alpine
# --- Clone Source --------------------------------------------------------------
FROM alpine/git:latest AS source
WORKDIR /src
ARG PTS_VERSION=main
ARG PTS_REPO=https://github.com/Taxel/PlexTraktSync.git
# Cache buster: GitHub's ref API returns a different response per-commit,
# forcing the next layer to re-execute instead of using a stale cached clone.
ADD "https://api.github.com/repos/Taxel/PlexTraktSync/git/refs/heads/${PTS_VERSION}" /upstream-ref.json
RUN git clone --depth 1 --branch ${PTS_VERSION} ${PTS_REPO} .
# --- Apply Patches ------------------------------------------------------------
FROM source AS patched
COPY Patches/ /patches/
# Install dos2unix to normalize patch line endings (defense against CRLF)
RUN apk add --no-cache dos2unix
RUN if ls /patches/*.patch 2>/dev/null; then \
for patch in /patches/*.patch; do \
echo "==> Applying: $(basename $patch)"; \
dos2unix "$patch"; \
patch -p1 --ignore-whitespace -d /src < "$patch"; \
done; \
else \
echo "No patches to apply"; \
fi
# --- Build Wheels (dependencies) ----------------------------------------------
FROM ${PYTHON_IMAGE} AS wheels
WORKDIR /dist
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_ROOT_USER_ACTION=ignore \
PYTHONDONTWRITEBYTECODE=1
# Download all dependency wheels
COPY --from=patched /src/requirements.txt ./requirements.txt
RUN --mount=type=cache,id=pip,target=/root/.cache/pip \
pip download --dest /wheels -r requirements.txt
# Build any source-only packages into wheels
RUN --mount=type=cache,id=pip,target=/root/.cache/pip \
set -x && \
files=$(ls /wheels/*.tar.gz /wheels/*.zip 2>/dev/null || true) && \
if [ -n "$files" ]; then \
pip wheel $files --wheel-dir=/wheels; \
fi
# --- Install Dependencies -----------------------------------------------------
FROM ${PYTHON_IMAGE} AS build
COPY --from=wheels /wheels /wheels
RUN pip install --prefix=/install --no-cache-dir /wheels/*.whl
# --- Compile Application ------------------------------------------------------
FROM ${PYTHON_IMAGE} AS compile
ARG APP_VERSION
ENV APP_VERSION=$APP_VERSION
WORKDIR /app
COPY --from=patched /src/plextraktsync/ ./plextraktsync/
COPY --from=patched /src/plextraktsync.sh .
# Stamp version and verify
RUN echo "__version__ = '${APP_VERSION:-unknown}'" > plextraktsync/__init__.py && \
python -c "from plextraktsync import __version__; print(f'Version: {__version__}')"
# Compile to bytecode for faster startup
RUN python -m compileall . && \
chmod -R a+rX,g-w .
# --- Extract Runtime Tools ----------------------------------------------------
FROM ${PYTHON_IMAGE} AS tools
WORKDIR /dist
RUN apk add --no-cache util-linux shadow
RUN <<EOF
install -d ./usr/bin ./usr/lib ./usr/sbin
install -p /usr/bin/setpriv ./usr/bin/
install -p /usr/lib/libcap-ng.so.0 ./usr/lib/
install -p /usr/lib/libbsd.so.0 ./usr/lib/
install -p /usr/lib/libmd.so.0 ./usr/lib/
install -p /usr/sbin/usermod /usr/sbin/groupmod ./usr/sbin/
EOF
# --- Final Runtime Image ------------------------------------------------------
FROM ${PYTHON_IMAGE} AS runtime
WORKDIR /app
# 1. System setup: create non-root user
RUN <<EOF
set -x
apk add --no-cache su-exec tzdata
addgroup --gid 1000 --system plextraktsync
adduser \
--disabled-password \
--gecos "Plex Trakt Sync" \
--home /app \
--ingroup plextraktsync \
--no-create-home \
--uid 1000 \
plextraktsync
EOF
# 2. Environment variables
ENV \
XDG_CACHE_HOME=/app/xdg/cache \
XDG_CONFIG_HOME=/app/xdg/config \
XDG_DATA_HOME=/app/xdg/data \
PIPX_BIN_DIR=/app/xdg/bin \
PIPX_HOME=/app/xdg/pipx \
PYTHONUSERBASE=/app/xdg \
HOME=/app/xdg \
PTS_CONFIG_DIR=/app/config \
PTS_CACHE_DIR=/app/config \
PTS_LOG_DIR=/app/config \
PTS_IN_DOCKER=1 \
PYTHONUNBUFFERED=1 \
PATH=/app/xdg/bin:/app/xdg/.local/bin:$PATH
# 3. Copy build artifacts
COPY --from=build /install /usr/local/
COPY --from=tools /dist /
COPY --from=compile --chown=plextraktsync:plextraktsync /app ./
COPY --chown=plextraktsync:plextraktsync entrypoint.sh /init
# 4. Final configuration
RUN ln -s /app/plextraktsync.sh /usr/bin/plextraktsync && \
chmod +x /init /app/plextraktsync.sh && \
mkdir -p /app/config /app/xdg && \
chown -R plextraktsync:plextraktsync /app
VOLUME ["/app/config", "/app/xdg"]
ENTRYPOINT ["/init"]
# --- Test Target --------------------------------------------------------------
FROM runtime AS test
ENV TRACE=1
RUN ["/init", "test"]
# --- Default Target -----------------------------------------------------------
FROM runtime