fix: arbitrary uid, entrypoint user, and docs

- Register arbitrary runtime UID as 'default' in /etc/passwd to avoid duplicate 'frappe' entries
- Assert user uniqueness, /tmp writability, and test frontend container in arbitrary UID test
- Update docs and env templates for GID 0 / group-writable sites and DEBIAN_BASE
- Remove trailing backslashes on Dockerfile comments
This commit is contained in:
Revant Nandgaonkar 2026-09-19 18:06:00 +05:30
parent b810e91142
commit 6303620ff8
8 changed files with 66 additions and 42 deletions

View File

@ -95,11 +95,10 @@ See [Automated Builds and Deployment](../03-production/06-automated-builds-and-d
| **Dependencies** | | | **Dependencies** | |
| PYTHON_VERSION | Python version for the base image | | PYTHON_VERSION | Python version for the base image |
| NODE_VERSION | Node.js version | | NODE_VERSION | Node.js version |
| DEBIAN_BASE | Debian base version for base, custom, and bench images, defaults to `trixie` (`bookworm` for v15) |
| WKHTMLTOPDF_VERSION | wkhtmltopdf version | | WKHTMLTOPDF_VERSION | wkhtmltopdf version |
| INSTALL_CHROMIUM | Configure chromium installation, defaults to `true` - needed for Frappe Workbench version >15 |
| **bench only** | |
| DEBIAN_BASE | Debian base version for the bench image, defaults to `bookworm` |
| WKHTMLTOPDF_DISTRO | use the specified distro for debian package. Default is `bookworm` | | WKHTMLTOPDF_DISTRO | use the specified distro for debian package. Default is `bookworm` |
| INSTALL_CHROMIUM | Configure chromium installation, defaults to `true` - needed for Frappe Workbench version >15 |
# Deploy the stack # Deploy the stack

View File

@ -57,11 +57,11 @@ Then edit `.env` and set variables according to your needs.
The host directory must exist before `docker compose up -d` and must be writable by the user the container runs as: The host directory must exist before `docker compose up -d` and must be writable by the user the container runs as:
| Directory | Owner | | Directory | Owner / Permissions |
| ------------------ | ------------------------------------------------- | | ------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `sites` | uid/gid `1000` (the `frappe` user of the image) | | `sites` | uid `1000`, gid `0` (root group) with group write permissions (`chmod -R g=u` or `775`), supporting arbitrary UIDs in OpenShift |
| `db-data` | uid/gid `999` (the `mariadb` or `postgres` image) | | `db-data` | uid/gid `999` (the `mariadb` or `postgres` image) |
| `redis-queue-data` | uid/gid `999` (the `redis` image) | | `redis-queue-data` | uid/gid `999` (the `redis` image) |
> Warning: with a bind mount, the data lives on the host, not inside the Docker volume. Removing the volume (for example with `docker compose down -v`) will **not** delete the data. To actually erase the data, delete the host directory manually with `rm -r <data path>`. > Warning: with a bind mount, the data lives on the host, not inside the Docker volume. Removing the volume (for example with `docker compose down -v`) will **not** delete the data. To actually erase the data, delete the host directory manually with `rm -r <data path>`.
@ -69,7 +69,7 @@ The host directory must exist before `docker compose up -d` and must be writable
```bash ```bash
mkdir -p /srv/frappe/{sites,db-data,redis-queue-data} mkdir -p /srv/frappe/{sites,db-data,redis-queue-data}
chown 1000:1000 /srv/frappe/sites chown 1000:0 /srv/frappe/sites && chmod 775 /srv/frappe/sites
chown 999:999 /srv/frappe/db-data /srv/frappe/redis-queue-data chown 999:999 /srv/frappe/db-data /srv/frappe/redis-queue-data
``` ```

View File

@ -15,10 +15,12 @@ DB_PORT=
REDIS_CACHE= REDIS_CACHE=
REDIS_QUEUE= REDIS_QUEUE=
# Only with the bind mount overrides. # Only with the bind mount overrides: absolute paths to existing host directories.
# Absolute path to an existing host directory, owned by the uid/gid the service runs as (1000 for sites, 999 for db-data and redis-queue-data). # SITES_DATA_LOCATION: overrides/compose.bind-mount-sites.yaml (owned by 1000:0, group-writable)
SITES_DATA_LOCATION= SITES_DATA_LOCATION=
# DB_DATA_LOCATION: overrides/compose.bind-mount-db-data.yaml (owned by uid/gid 999)
DB_DATA_LOCATION= DB_DATA_LOCATION=
# REDIS_QUEUE_DATA_LOCATION: overrides/compose.bind-mount-redis-queue.yaml (owned by uid/gid 999)
REDIS_QUEUE_DATA_LOCATION= REDIS_QUEUE_DATA_LOCATION=

View File

@ -25,26 +25,26 @@ RUN useradd -u 1000 -g 0 -d /home/frappe -ms /bin/bash frappe \
nginx \ nginx \
gettext-base \ gettext-base \
file \ file \
# weasyprint dependencies \ # weasyprint dependencies
libpango-1.0-0 \ libpango-1.0-0 \
libharfbuzz0b \ libharfbuzz0b \
libpangoft2-1.0-0 \ libpangoft2-1.0-0 \
libpangocairo-1.0-0 \ libpangocairo-1.0-0 \
# For backups \ # For backups
restic \ restic \
gpg \ gpg \
# MariaDB \ # MariaDB
mariadb-client \ mariadb-client \
less \ less \
# Postgres \ # Postgres
libpq-dev \ libpq-dev \
postgresql-client \ postgresql-client \
# For healthcheck \ # For healthcheck
wait-for-it \ wait-for-it \
jq \ jq \
# For MIME type detection \ # For MIME type detection
media-types \ media-types \
# NodeJS \ # NodeJS
&& mkdir -p ${NVM_DIR} \ && mkdir -p ${NVM_DIR} \
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash \ && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash \
&& . ${NVM_DIR}/nvm.sh \ && . ${NVM_DIR}/nvm.sh \
@ -57,28 +57,28 @@ RUN useradd -u 1000 -g 0 -d /home/frappe -ms /bin/bash frappe \
&& echo 'export NVM_DIR="/home/frappe/.nvm"' >>/home/frappe/.bashrc \ && echo 'export NVM_DIR="/home/frappe/.nvm"' >>/home/frappe/.bashrc \
&& echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' >>/home/frappe/.bashrc \ && echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' >>/home/frappe/.bashrc \
&& echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >>/home/frappe/.bashrc \ && echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >>/home/frappe/.bashrc \
# Install wkhtmltopdf with patched qt \ # Install wkhtmltopdf with patched qt
&& if [ "$(uname -m)" = "aarch64" ]; then export ARCH=arm64; fi \ && if [ "$(uname -m)" = "aarch64" ]; then export ARCH=arm64; fi \
&& if [ "$(uname -m)" = "x86_64" ]; then export ARCH=amd64; fi \ && if [ "$(uname -m)" = "x86_64" ]; then export ARCH=amd64; fi \
&& downloaded_file=wkhtmltox_${WKHTMLTOPDF_VERSION}.${WKHTMLTOPDF_DISTRO}_${ARCH}.deb \ && downloaded_file=wkhtmltox_${WKHTMLTOPDF_VERSION}.${WKHTMLTOPDF_DISTRO}_${ARCH}.deb \
&& curl -sLO https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTMLTOPDF_VERSION/$downloaded_file \ && curl -sLO https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTMLTOPDF_VERSION/$downloaded_file \
&& apt-get install -y ./$downloaded_file \ && apt-get install -y ./$downloaded_file \
&& rm $downloaded_file \ && rm $downloaded_file \
# Chromium \ # Chromium
&& if [ "$INSTALL_CHROMIUM" != "false" ]; then \ && if [ "$INSTALL_CHROMIUM" != "false" ]; then \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
chromium-headless-shell; \ chromium-headless-shell; \
fi \ fi \
# Clean up \ # Clean up
&& rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/apt/lists/* \
&& rm -fr /etc/nginx/sites-enabled/default \ && rm -fr /etc/nginx/sites-enabled/default \
&& mkdir -p /etc/nginx/snippets \ && mkdir -p /etc/nginx/snippets \
&& pip3 install frappe-bench \ && pip3 install frappe-bench \
# Fixes for non-root nginx and logs to stdout \ # Fixes for non-root nginx and logs to stdout
&& sed -i '/user www-data/d' /etc/nginx/nginx.conf \ && sed -i '/user www-data/d' /etc/nginx/nginx.conf \
&& ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log \ && ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log \
&& touch /run/nginx.pid \ && touch /run/nginx.pid \
# OpenShift and arbitrary UID compatibility (GID 0 & group writable) \ # OpenShift and arbitrary UID compatibility (GID 0 & group writable)
&& chgrp -R 0 /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \ && chgrp -R 0 /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \
&& chmod -R g=u /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \ && chmod -R g=u /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \
&& chmod g+w /etc/passwd \ && chmod g+w /etc/passwd \

View File

@ -21,26 +21,26 @@ RUN useradd -u 1000 -g 0 -d /home/frappe -ms /bin/bash frappe \
nginx \ nginx \
gettext-base \ gettext-base \
file \ file \
# weasyprint dependencies \ # weasyprint dependencies
libpango-1.0-0 \ libpango-1.0-0 \
libharfbuzz0b \ libharfbuzz0b \
libpangoft2-1.0-0 \ libpangoft2-1.0-0 \
libpangocairo-1.0-0 \ libpangocairo-1.0-0 \
# For backups \ # For backups
restic \ restic \
gpg \ gpg \
# MariaDB \ # MariaDB
mariadb-client \ mariadb-client \
less \ less \
# Postgres \ # Postgres
libpq-dev \ libpq-dev \
postgresql-client \ postgresql-client \
# For healthcheck \ # For healthcheck
wait-for-it \ wait-for-it \
jq \ jq \
# For MIME type detection \ # For MIME type detection
media-types \ media-types \
# NodeJS \ # NodeJS
&& mkdir -p ${NVM_DIR} \ && mkdir -p ${NVM_DIR} \
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash \ && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash \
&& . ${NVM_DIR}/nvm.sh \ && . ${NVM_DIR}/nvm.sh \
@ -53,28 +53,28 @@ RUN useradd -u 1000 -g 0 -d /home/frappe -ms /bin/bash frappe \
&& echo 'export NVM_DIR="/home/frappe/.nvm"' >>/home/frappe/.bashrc \ && echo 'export NVM_DIR="/home/frappe/.nvm"' >>/home/frappe/.bashrc \
&& echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' >>/home/frappe/.bashrc \ && echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' >>/home/frappe/.bashrc \
&& echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >>/home/frappe/.bashrc \ && echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >>/home/frappe/.bashrc \
# Install wkhtmltopdf with patched qt \ # Install wkhtmltopdf with patched qt
&& if [ "$(uname -m)" = "aarch64" ]; then export ARCH=arm64; fi \ && if [ "$(uname -m)" = "aarch64" ]; then export ARCH=arm64; fi \
&& if [ "$(uname -m)" = "x86_64" ]; then export ARCH=amd64; fi \ && if [ "$(uname -m)" = "x86_64" ]; then export ARCH=amd64; fi \
&& downloaded_file=wkhtmltox_${WKHTMLTOPDF_VERSION}.${WKHTMLTOPDF_DISTRO}_${ARCH}.deb \ && downloaded_file=wkhtmltox_${WKHTMLTOPDF_VERSION}.${WKHTMLTOPDF_DISTRO}_${ARCH}.deb \
&& curl -sLO https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTMLTOPDF_VERSION/$downloaded_file \ && curl -sLO https://github.com/wkhtmltopdf/packaging/releases/download/$WKHTMLTOPDF_VERSION/$downloaded_file \
&& apt-get install --no-install-recommends -y ./$downloaded_file \ && apt-get install --no-install-recommends -y ./$downloaded_file \
&& rm $downloaded_file \ && rm $downloaded_file \
# Chromium \ # Chromium
&& if [ "$INSTALL_CHROMIUM" != "false" ]; then \ && if [ "$INSTALL_CHROMIUM" != "false" ]; then \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
chromium-headless-shell; \ chromium-headless-shell; \
fi \ fi \
# Clean up \ # Clean up
&& rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/apt/lists/* \
&& rm -fr /etc/nginx/sites-enabled/default \ && rm -fr /etc/nginx/sites-enabled/default \
&& mkdir -p /etc/nginx/snippets \ && mkdir -p /etc/nginx/snippets \
&& pip3 install frappe-bench \ && pip3 install frappe-bench \
# Fixes for non-root nginx and logs to stdout \ # Fixes for non-root nginx and logs to stdout
&& sed -i '/user www-data/d' /etc/nginx/nginx.conf \ && sed -i '/user www-data/d' /etc/nginx/nginx.conf \
&& ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log \ && ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log \
&& touch /run/nginx.pid \ && touch /run/nginx.pid \
# OpenShift and arbitrary UID compatibility (GID 0 & group writable) \ # OpenShift and arbitrary UID compatibility (GID 0 & group writable)
&& chgrp -R 0 /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \ && chgrp -R 0 /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \
&& chmod -R g=u /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \ && chmod -R g=u /home/frappe /etc/nginx /var/log/nginx /var/lib/nginx /run \
&& chmod g+w /etc/passwd && chmod g+w /etc/passwd

View File

@ -4,8 +4,8 @@
# - `SITES_DATA_LOCATION` (required): absolute path to the host directory to bind mount. # - `SITES_DATA_LOCATION` (required): absolute path to the host directory to bind mount.
# - The directory must exist before running `docker compose up -d`, otherwise Docker fails # - The directory must exist before running `docker compose up -d`, otherwise Docker fails
# when populating the volume. # when populating the volume.
# - The directory must be writable by the `frappe` user of the image (uid/gid 1000): # - The directory must be writable by the `frappe` user of the image (uid 1000, gid 0):
# `chown -R 1000:1000 <path>`. # `chown -R 1000:0 <path> && chmod -R 775 <path>`.
volumes: volumes:
sites: sites:
driver: local driver: local

View File

@ -5,7 +5,11 @@ umask 0002
if ! whoami &>/dev/null; then if ! whoami &>/dev/null; then
if [ -w /etc/passwd ]; then if [ -w /etc/passwd ]; then
echo "frappe:x:$(id -u):0:frappe user:${HOME:-/home/frappe}:/bin/bash" >>/etc/passwd user_name="default"
if grep -q "^${user_name}:" /etc/passwd; then
user_name="default_$(id -u)"
fi
echo "${user_name}:x:$(id -u):0:${user_name} user:${HOME:-/home/frappe}:/bin/bash" >>/etc/passwd
fi fi
fi fi

View File

@ -177,13 +177,14 @@ class TestPostgres:
def test_arbitrary_uid_execution(compose: Compose): def test_arbitrary_uid_execution(compose: Compose):
"""Verify container starts and runs under arbitrary UID with GID 0 (OpenShift restricted-v2). """Verify backend and frontend containers start and run under arbitrary UID with GID 0 (OpenShift restricted-v2).
Executes the entrypoint directly to verify: Executes the entrypoints directly to verify:
- Container startup with arbitrary non-root UID (e.g. 1000680000) and GID 0 - Container startup with arbitrary non-root UID (1000680000) and GID 0
- Entrypoint dynamic user lookup (/etc/passwd mapping) - Entrypoint dynamic user lookup (/etc/passwd mapping)
- Effective runtime umask (0002) - Effective runtime umask (0002)
- Writability of required paths (/home/frappe, /tmp, sites directory) - Writability of required paths (/home/frappe, /tmp, sites directory)
- Frontend nginx configuration test and startup under arbitrary UID
""" """
check_script = ( check_script = (
"import os\n" "import os\n"
@ -191,11 +192,18 @@ def test_arbitrary_uid_execution(compose: Compose):
"import tempfile\n" "import tempfile\n"
"uid = os.getuid()\n" "uid = os.getuid()\n"
"gid = os.getgid()\n" "gid = os.getgid()\n"
"user = pwd.getpwuid(uid).pw_name\n" "assert uid == 1000680000, f'Expected UID 1000680000, got {uid}'\n"
"assert gid == 0, f'Expected GID 0, got {gid}'\n" "assert gid == 0, f'Expected GID 0, got {gid}'\n"
"user = pwd.getpwuid(uid).pw_name\n"
"assert user == 'default', f'Expected username default, got {user}'\n"
"with open('/etc/passwd') as f:\n"
" frappe_lines = [line for line in f if line.startswith('frappe:')]\n"
"assert len(frappe_lines) == 1, f'Expected exactly 1 frappe user, found {len(frappe_lines)}'\n"
"old_umask = os.umask(0)\n" "old_umask = os.umask(0)\n"
"os.umask(old_umask)\n" "os.umask(old_umask)\n"
"assert old_umask == 0o002, f'Expected umask 0002, got {oct(old_umask)}'\n" "assert old_umask == 0o002, f'Expected umask 0002, got {oct(old_umask)}'\n"
"with tempfile.NamedTemporaryFile(dir='/tmp') as f:\n"
" f.write(b'ok')\n"
"with tempfile.NamedTemporaryFile(dir='/home/frappe') as f:\n" "with tempfile.NamedTemporaryFile(dir='/home/frappe') as f:\n"
" f.write(b'ok')\n" " f.write(b'ok')\n"
"with tempfile.NamedTemporaryFile(dir='/home/frappe/frappe-bench/sites') as f:\n" "with tempfile.NamedTemporaryFile(dir='/home/frappe/frappe-bench/sites') as f:\n"
@ -214,3 +222,14 @@ def test_arbitrary_uid_execution(compose: Compose):
"-c", "-c",
check_script, check_script,
) )
compose(
"run",
"--rm",
"--no-deps",
"--user",
"1000680000:0",
"frontend",
"nginx",
"-t",
)