Skip to content

Commit f684272

Browse files
authored
Use deterministic UID/GID in Dockerfile #230 (#270)
Signed-off-by: tdruez <[email protected]>
1 parent c260cce commit f684272

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

Dockerfile

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ LABEL org.opencontainers.image.source="https://github.com/aboutcode-org/dejacode
1212
LABEL org.opencontainers.image.description="DejaCode"
1313
LABEL org.opencontainers.image.licenses="AGPL-3.0-only"
1414

15-
ENV APP_NAME dejacode
16-
ENV APP_USER app
17-
ENV APP_DIR /opt/$APP_NAME
18-
ENV VENV_LOCATION /opt/$APP_NAME/.venv
15+
# Set default values for APP_UID and APP_GID at build-time
16+
ARG APP_UID=1000
17+
ARG APP_GID=1000
18+
19+
ENV APP_NAME=dejacode
20+
ENV APP_USER=app
21+
ENV APP_UID=${APP_UID}
22+
ENV APP_GID=${APP_GID}
23+
ENV APP_DIR=/opt/$APP_NAME
24+
ENV VENV_LOCATION=/opt/$APP_NAME/.venv
1925

2026
# Force Python unbuffered stdout and stderr (they are flushed to terminal immediately)
21-
ENV PYTHONUNBUFFERED 1
27+
ENV PYTHONUNBUFFERED=1
2228
# Do not write Python .pyc files
23-
ENV PYTHONDONTWRITEBYTECODE 1
29+
ENV PYTHONDONTWRITEBYTECODE=1
2430
# Add the app dir in the Python path for entry points availability
25-
ENV PYTHONPATH $PYTHONPATH:$APP_DIR
31+
ENV PYTHONPATH=$PYTHONPATH:$APP_DIR
2632

2733
# OS requirements
2834
RUN apt-get update \
@@ -36,9 +42,9 @@ RUN apt-get update \
3642
&& apt-get clean \
3743
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
3844

39-
# Create the APP_USER group, user, and directory with proper permissions
40-
RUN addgroup --system $APP_USER \
41-
&& adduser --system --group --home=$APP_DIR $APP_USER \
45+
# Create the APP_USER group, user, and directory with specific UID and GID
46+
RUN groupadd --gid $APP_GID --system $APP_USER \
47+
&& useradd --uid $APP_UID --gid $APP_GID --home-dir $APP_DIR --system --create-home $APP_USER \
4248
&& chown $APP_USER:$APP_USER $APP_DIR \
4349
&& mkdir -p /var/$APP_NAME \
4450
&& chown $APP_USER:$APP_USER /var/$APP_NAME
@@ -53,7 +59,7 @@ RUN mkdir -p /var/$APP_NAME/static/ /var/$APP_NAME/media/
5359
# Create the virtualenv
5460
RUN python -m venv $VENV_LOCATION
5561
# Enable the virtualenv, similar effect as "source activate"
56-
ENV PATH $VENV_LOCATION/bin:$PATH
62+
ENV PATH=$VENV_LOCATION/bin:$PATH
5763

5864
# Install the dependencies before the codebase COPY for proper Docker layer caching
5965
COPY --chown=$APP_USER:$APP_USER setup.cfg setup.py $APP_DIR/

docker-compose.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ services:
2323
- redis_data:/data
2424
restart: always
2525

26+
# This service is responsible for ensuring the correct ownership of files
27+
# in the shared volumes used by the application (static and media).
28+
# It ensures that all files inside the `/var/dejacode/` directory are owned
29+
# by the user and group with the UID and GID defined in the environment variables
30+
# APP_UID and APP_GID, which default to 1000 if not set.
31+
#
32+
# The service runs only once (due to "restart: no") and performs a `chown` operation
33+
# to change the ownership of the static and media directories, ensuring proper
34+
# file access rights for the running application containers.
35+
#
36+
# Volumes mounted:
37+
# - static: Ensures the ownership of static files in the /var/dejacode/static directory
38+
# - media: Ensures the ownership of media files in the /var/dejacode/media directory
39+
#
40+
# Notes: This service can be removed once DejaCode 5.3.0 will be released.
41+
chown:
42+
image: alpine:latest
43+
restart: "no"
44+
command: chown -R ${APP_UID:-1000}:${APP_GID:-1000} /var/dejacode/
45+
env_file:
46+
- docker.env
47+
volumes:
48+
- static:/var/dejacode/static
49+
- media:/var/dejacode/media
50+
2651
web:
2752
build: .
2853
command: sh -c "
@@ -46,6 +71,8 @@ services:
4671
condition: service_started
4772
clamav:
4873
condition: service_started
74+
chown:
75+
condition: service_completed_successfully
4976

5077
worker:
5178
build: .
@@ -63,6 +90,7 @@ services:
6390
- redis
6491
- db
6592
- web
93+
- chown
6694

6795
scheduler:
6896
build: .
@@ -78,6 +106,7 @@ services:
78106
- redis
79107
- db
80108
- web
109+
- chown
81110

82111
nginx:
83112
image: nginx:alpine

0 commit comments

Comments
 (0)