Skip to content

Commit 3fb10ba

Browse files
richvdht3chguy
andauthored
Docker: run as non-root (#28849)
* Docker: allow configuration of HTTP listen port via env var * Update docs/install.md Co-authored-by: Michael Telatynski <[email protected]> * prettier * Docker: run as non-root * Simplify sed incantation --------- Co-authored-by: Michael Telatynski <[email protected]>
1 parent cf49f9e commit 3fb10ba

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Dockerfile

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Builder
2-
FROM --platform=$BUILDPLATFORM node:22-bullseye as builder
2+
FROM --platform=$BUILDPLATFORM node:22-bullseye AS builder
33

44
# Support custom branch of the js-sdk. This also helps us build images of element-web develop.
55
ARG USE_CUSTOM_SDKS=false
@@ -25,8 +25,18 @@ COPY --from=builder /src/webapp /app
2525
# through `envsubst` by the nginx docker image entry point.
2626
COPY /docker/nginx-templates/* /etc/nginx/templates/
2727

28+
# Tell nginx to put its pidfile elsewhere, so it can run as non-root
29+
RUN sed -i -e 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf
30+
31+
# nginx user must own the cache and etc directory to write cache and tweak the nginx config
32+
RUN chown -R nginx:0 /var/cache/nginx /etc/nginx
33+
RUN chmod -R g+w /var/cache/nginx /etc/nginx
34+
2835
RUN rm -rf /usr/share/nginx/html \
2936
&& ln -s /app /usr/share/nginx/html
3037

38+
# Run as nginx user by default
39+
USER nginx
40+
3141
# HTTP listen port
3242
ENV ELEMENT_WEB_PORT=80

docs/install.md

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ would be:
6060
docker run --rm -p 127.0.0.1:80:80 -v /etc/element-web/config.json:/app/config.json vectorim/element-web
6161
```
6262

63+
The Docker image is configured to run as an unprivileged (non-root) user by
64+
default. This should be fine on modern Docker runtimes, but binding to port 80
65+
on other runtimes may require root privileges. To resolve this, either run the
66+
image as root (`docker run --user 0`) or, better, change the port that nginx
67+
listens on via the `ELEMENT_WEB_PORT` environment variable.
68+
6369
The behaviour of the docker image can be customised via the following
6470
environment variables:
6571

0 commit comments

Comments
 (0)