Skip to content

Commit 769e99f

Browse files
authored
Merge pull request #183 from datopian/chore/streamline-dockerfile
Revamp Dockerfile
2 parents e35bcb9 + 31eda43 commit 769e99f

File tree

4 files changed

+92
-57
lines changed

4 files changed

+92
-57
lines changed

Dockerfile

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,108 @@
11
# Dockerfile for uWSGI wrapped Giftless Git LFS Server
2+
# Shared build ARGs among stages
3+
ARG WORKDIR=/app
4+
ARG VENV="$WORKDIR/.venv"
25

36
### --- Build Depdendencies ---
4-
5-
FROM python:3.12 as builder
6-
MAINTAINER "Shahar Evron <[email protected]>"
7-
8-
# Build wheels for uWSGI and all requirements
9-
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
10-
&& apt-get install -y build-essential libpcre3 libpcre3-dev git
11-
RUN pip install -U pip
12-
RUN mkdir /wheels
13-
7+
FROM python:3.12 AS builder
148
ARG UWSGI_VERSION=2.0.23
15-
RUN pip wheel -w /wheels uwsgi==$UWSGI_VERSION
16-
17-
COPY requirements/main.txt /requirements.txt
18-
RUN pip wheel -w /wheels -r /requirements.txt
19-
20-
### --- Build Final Image ---
21-
22-
FROM python:3.12-slim
23-
24-
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
25-
&& apt-get install -y libpcre3 libxml2 tini git \
26-
&& apt-get clean \
27-
&& apt -y autoremove
28-
29-
RUN mkdir /app
30-
31-
# Install dependencies
32-
COPY --from=builder /wheels /wheels
33-
RUN pip install /wheels/*.whl
34-
35-
# Copy project code
36-
COPY . /app
37-
RUN pip install -e /app
38-
39-
ARG USER_NAME=giftless
40-
ARG STORAGE_DIR=/lfs-storage
41-
ENV GIFTLESS_TRANSFER_ADAPTERS_basic_options_storage_options_path $STORAGE_DIR
42-
43-
RUN useradd -d /app $USER_NAME
44-
RUN mkdir $STORAGE_DIR
45-
RUN chown $USER_NAME $STORAGE_DIR
46-
47-
# Pip-install some common WSGI middleware modules
9+
# Common WSGI middleware modules to be pip-installed
4810
# These are not required in every Giftless installation but are common enough
4911
ARG EXTRA_PACKAGES="wsgi_cors_middleware"
12+
# expose shared ARGs
13+
ARG WORKDIR
14+
ARG VENV
15+
16+
# Set WORKDIR (also creates the dir)
17+
WORKDIR $WORKDIR
18+
19+
# Install packages to build wheels for uWSGI and other requirements
20+
RUN set -eux ;\
21+
export DEBIAN_FRONTEND=noninteractive ;\
22+
apt-get update ;\
23+
apt-get install -y --no-install-recommends build-essential libpcre3 libpcre3-dev git ;\
24+
rm -rf /var/lib/apt/lists/*
25+
26+
# Create virtual env to store dependencies, "activate" it
27+
RUN python -m venv --upgrade-deps "$VENV"
28+
ENV VIRTUAL_ENV="$VENV" PATH="$VENV/bin:$PATH"
29+
30+
# Set a couple pip-related settings
31+
# Wait a bit longer for slow connections
32+
ENV PIP_TIMEOUT=100
33+
# Don't nag about newer pip
34+
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
35+
# Don't cache pip packages
36+
ENV PIP_NO_CACHE_DIR=1
37+
# Require activated virtual environment
38+
ENV PIP_REQUIRE_VIRTUALENV=1
39+
# Eventual python cache files go here (not to be copied)
40+
ENV PYTHONPYCACHEPREFIX=/tmp/__pycache__
41+
42+
# Install runtime dependencies
43+
RUN --mount=target=/build-ctx \
44+
pip install -r /build-ctx/requirements/main.txt
45+
RUN pip install uwsgi==$UWSGI_VERSION
46+
# Install extra packages into the virtual env
5047
RUN pip install ${EXTRA_PACKAGES}
5148

52-
WORKDIR /app
49+
# Copy project contents necessary for an editable install
50+
COPY .git .git/
51+
COPY giftless giftless/
52+
COPY pyproject.toml .
53+
# Editable-install the giftless package (add a kind of a project path reference in site-packages)
54+
# To detect the package version dynamically, setuptools-scm needs the git binary
55+
RUN pip install -e .
5356

54-
ENV UWSGI_MODULE "giftless.wsgi_entrypoint"
57+
### --- Build Final Image ---
58+
FROM python:3.12-slim AS final
59+
LABEL org.opencontainers.image.authors="Shahar Evron <[email protected]>"
5560

61+
ARG USER_NAME=giftless
62+
# Writable path for local LFS storage
63+
ARG STORAGE_DIR=/lfs-storage
64+
# Set to true to add a runtime dockerhub deprecation warning
5665
ARG IS_DOCKERHUB
57-
# Override default docker entrypoint for dockerhub
58-
RUN --mount=target=/build-ctx set -e ;\
59-
if [ "$IS_DOCKERHUB" = true ]; then \
60-
cp /build-ctx/scripts/docker-entrypoint-dockerhub.sh scripts/docker-entrypoint.sh ;\
66+
# expose shared ARGs
67+
ARG WORKDIR
68+
ARG VENV
69+
70+
# Set WORKDIR (also creates the dir)
71+
WORKDIR $WORKDIR
72+
73+
# Create a user and set local storage write permissions
74+
RUN set -eux ;\
75+
useradd -d "$WORKDIR" "$USER_NAME" ;\
76+
mkdir "$STORAGE_DIR" ;\
77+
chown "$USER_NAME" "$STORAGE_DIR"
78+
79+
# Install runtime dependencies
80+
RUN set -eux ;\
81+
export DEBIAN_FRONTEND=noninteractive ;\
82+
apt-get update ;\
83+
apt-get install -y libpcre3 libxml2 tini ;\
84+
rm -rf /var/lib/apt/lists/*
85+
86+
# Use the virtual env with dependencies from builder stage
87+
COPY --from=builder "$VENV" "$VENV"
88+
ENV VIRTUAL_ENV="$VENV" PATH="$VENV/bin:$PATH"
89+
# Copy project source back into the same path referenced by the editable install
90+
COPY --from=builder "$WORKDIR/giftless" "giftless"
91+
92+
# Copy desired docker-entrypoint
93+
RUN --mount=target=/build-ctx set -eux ;\
94+
target_de=scripts/docker-entrypoint.sh ;\
95+
mkdir -p "$(dirname "$target_de")" ;\
96+
if [ "${IS_DOCKERHUB:-}" = true ]; then \
97+
cp /build-ctx/scripts/docker-entrypoint-dockerhub.sh "$target_de" ;\
98+
else \
99+
cp /build-ctx/scripts/docker-entrypoint.sh "$target_de" ;\
61100
fi
62101

102+
# Set runtime properties
63103
USER $USER_NAME
104+
ENV GIFTLESS_TRANSFER_ADAPTERS_basic_options_storage_options_path="$STORAGE_DIR"
105+
ENV UWSGI_MODULE="giftless.wsgi_entrypoint"
64106

65107
ENTRYPOINT ["tini", "--", "scripts/docker-entrypoint.sh"]
66108
CMD ["uwsgi", "-s", "127.0.0.1:5000", "-M", "-T", "--threads", "2", "-p", "2", \

requirements/dev.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ urllib3==2.0.7
248248
# responses
249249
# types-requests
250250
uv==0.5.8
251-
# via
252-
# -c requirements/main.txt
253-
# -r requirements/dev.in
251+
# via -r requirements/dev.in
254252
vcrpy==6.0.1
255253
# via pytest-vcr
256254
virtualenv==20.25.1

requirements/main.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,4 @@ boto3~=1.34
2222
# GitHub AA Provider
2323
cachetools~=5.3
2424

25-
# uv: fast pip replacement
26-
uv
27-
2825
importlib-metadata; python_version < '3.13'

requirements/main.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ urllib3==2.0.7
132132
# via
133133
# botocore
134134
# requests
135-
uv==0.5.8
136-
# via -r requirements/main.in
137135
webargs==8.4.0
138136
# via -r requirements/main.in
139137
werkzeug==3.0.3

0 commit comments

Comments
 (0)