Skip to content

Commit 44f4d53

Browse files
author
SUSE Update Bot
committed
Test build for #3167
1 parent eb6b404 commit 44f4d53

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

nginx-image/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ COPY --from=target / /target
2727

2828
RUN set -euo pipefail; \
2929
export CHKSTAT_ALLOW_INSECURE_MODE_IF_NO_PROC=1; \
30-
zypper -n --installroot /target --gpg-auto-import-keys install --no-recommends curl gawk nginx findutils gettext-runtime
30+
zypper -n --installroot /target --gpg-auto-import-keys install --no-recommends curl gawk nginx findutils gettext-runtime sed grep
3131
# sanity check that the version from the tag is equal to the version of nginx that we expect
3232
RUN set -euo pipefail; \
3333
[ "$(rpm --root /target -q --qf '%{version}' nginx | \
@@ -76,5 +76,5 @@ COPY [1-3]0-*.sh /docker-entrypoint.d/
7676
COPY docker-entrypoint.sh /usr/local/bin
7777
COPY index.html /srv/www/htdocs/
7878
RUN set -euo pipefail; chmod +x /docker-entrypoint.d/*.sh /usr/local/bin/docker-entrypoint.sh
79-
RUN set -euo pipefail; install -d -o nginx -g nginx -m 750 /var/log/nginx; ln -sf /dev/stdout /var/log/nginx/access.log; ln -sf /dev/stderr /var/log/nginx/error.log
79+
RUN set -euo pipefail; install -d -o nginx -g nginx -m 750 /var/log/nginx; install -d /var/cache/nginx /var/run/nginx; ln -sf /dev/stdout /var/log/nginx/access.log; ln -sf /dev/stderr /var/log/nginx/error.log; chown -R nginx:nginx /var/cache/nginx; chown -R nginx:nginx /etc/nginx; chown -R nginx:nginx /var/run/nginx; install -d -o nginx -g nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp; chown -R nginx:nginx /tmp; chmod -R g+w /var/cache/nginx /var/log/nginx /etc/nginx /var/run/nginx /tmp
8080
STOPSIGNAL SIGQUIT

nginx-image/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ The template above is then rendered to `/etc/nginx/conf.d/default.conf` as follo
4242
```nginx
4343
listen 80;
4444
```
45+
## Running nginx as a non-root user
46+
To run the image as a less privileged user using the `nginx` user, do the following:
47+
```ShellSession
48+
$ podman run -it --user nginx --rm -p 8080:8080 -v /path/to/html/:/srv/www/htdocs/:Z -v $PWD/nginx.conf:/etc/nginx/nginx.conf:Z registry.suse.com/suse/nginx:1.21
49+
```
50+
**Note:** When running as the `nginx` user the default port is 8080.
4551

4652
## Environment variables
4753

nginx-image/docker-entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
4444
fi
4545
fi
4646

47+
# Ensure PID path is set to /var/run/nginx.pid for both privileged and unprivileged users
48+
sed -i 's,^#\?\s*pid\s\+.*;$,pid /var/run/nginx/nginx.pid;,' /etc/nginx/nginx.conf
49+
# modify temp paths for both privileged and unprivileged users
50+
sed -i "/^http {/a \ proxy_temp_path /tmp/proxy_temp;\n client_body_temp_path /tmp/client_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;\n" /etc/nginx/nginx.conf
51+
52+
CURRENT_UID=$(id -u)
53+
if [ "$CURRENT_UID" -gt "0" ]; then
54+
# Running as Unprivileged User
55+
entrypoint_log "$0: Running as unprivileged user (UID: $CURRENT_UID). Configuring for unprivileged mode (Port 8080)."
56+
57+
# Remove 'user' directive (unprivileged users can't switch users)
58+
sed -i '/^user/d' /etc/nginx/nginx.conf
59+
entrypoint_log "$0: Removed 'user' directive for unprivileged worker."
60+
61+
sed -i 's/listen \(.*\)80;/listen \18080;/' /etc/nginx/conf.d/default.conf 2>/dev/null || \
62+
sed -i 's/listen \(.*\)80;/listen \18080;/' /etc/nginx/nginx.conf 2>/dev/null || true
63+
entrypoint_log "$0: Listening on port 8080."
64+
fi
65+
4766
exec "$@"

0 commit comments

Comments
 (0)