Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/bci_build/package/appcontainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def _generate_prometheus_family_healthcheck(port: int) -> str:
"LICENSE",
"20-envsubst-on-templates.sh",
"30-tune-worker-processes.sh",
"40-unprivileged-mode.sh",
"index.html",
):
_NGINX_FILES[filename] = (Path(__file__).parent / "nginx" / filename).read_bytes()
Expand Down Expand Up @@ -278,6 +279,8 @@ def _get_nginx_kwargs(os_version: OsVersion):
"nginx",
"findutils",
_envsubst_pkg_name(os_version),
"sed",
"grep",
]
)
+ (["libcurl-mini4"] if os_version.is_sl16 else []),
Expand All @@ -293,13 +296,14 @@ def _get_nginx_kwargs(os_version: OsVersion):
),
"custom_end": textwrap.dedent(f"""
{DOCKERFILE_RUN} mkdir /docker-entrypoint.d
COPY [1-3]0-*.sh /docker-entrypoint.d/
COPY [1-4]0-*.sh /docker-entrypoint.d/
COPY docker-entrypoint.sh /usr/local/bin
COPY index.html /srv/www/htdocs/
{DOCKERFILE_RUN} chmod +x /docker-entrypoint.d/*.sh /usr/local/bin/docker-entrypoint.sh
{DOCKERFILE_RUN} 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
{DOCKERFILE_RUN} set -euo pipefail; mkdir -p /var/cache/nginx /var/run/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;\
ln -sf /dev/stdout /var/log/nginx/access.log;\
ln -sf /dev/stderr /var/log/nginx/error.log;\
chmod -R 777 /var/cache/nginx /etc/nginx /var/run/nginx /var/log/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a chmod -R 0777 is kinda ugly, can we chown it to the nginx user instead? we should then set that as a stableuid and update the README accordingly.

STOPSIGNAL SIGQUIT"""),
}

Expand Down
34 changes: 34 additions & 0 deletions src/bci_build/package/nginx/40-unprivileged-mode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

set -e

CURRENT_UID=$(id -u)
if [ "$CURRENT_UID" -gt "0" ]; then
echo "$0: Running as unprivileged user (UID: $CURRENT_UID). Configuring for unprivileged mode (Port 8080)."

CONF_FILES="/etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf"

for FILE in $CONF_FILES; do
if [ -w "$FILE" ]; then
if grep -q "listen .*80;" "$FILE"; then
echo "Changing port 80 to 8080 in $FILE"
sed 's/listen\s*80;/listen 8080;/g' "$FILE" > /tmp/client_temp/nginx_swap.conf && \
cat /tmp/client_temp/nginx_swap.conf > "$FILE" && \
rm -f /tmp/client_temp/nginx_swap.conf
fi

if [ "$FILE" = "/etc/nginx/nginx.conf" ]; then
echo "Redirecting NGINX temp paths and setting PID to /tmp in $FILE"
sed -e '/^user/d' \
-e 's,^#\?\s*pid\s\+.*;$,pid /var/run/nginx/nginx.pid;,' \
-e '/http {/a \ client_body_temp_path /tmp/client_temp;\n proxy_temp_path /tmp/proxy_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;' \
"$FILE" > /tmp/client_temp/nginx_ultra.conf && \
cat /tmp/client_temp/nginx_ultra.conf > "$FILE" && \
rm -f /tmp/client_temp/nginx_ultra.conf
echo "$0: Removed 'user' directive and updated PID path."
fi
fi
done

echo "$0: Listening on port 8080."
fi
6 changes: 6 additions & 0 deletions src/bci_build/package/nginx/README.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ The template above is then rendered to `/etc/nginx/conf.d/default.conf` as follo
```nginx
listen 80;
```
## Running nginx as a non-root user
To run the image as a less privileged user using the `nginx` user, do the following:
```ShellSession
$ podman run -it --user nginx --rm -p 8080:8080 -v /path/to/html/:/srv/www/htdocs/:Z {{ image.pretty_reference }}
```
**Note:** When running as the `nginx` user the default port is 8080.

Comment thread
rcmadhankumar marked this conversation as resolved.
## Environment variables

Expand Down
Loading