Skip to content

Commit 8f406d2

Browse files
dav-pascualdekobon
authored andcommitted
Add support for unpriviledged non-root image container
Running container images with a non-root user provides additional security against vulnerabilities, and some containers orchestrators such as Openshift enforce this security policy making official regular nginx not compatible out of the box. These changes, based on 'nginxinc/docker-nginx-unprivileged', modify the parent image 'Dockerfile.oss' to allow nginx to execute with a non-root user. Signed-off-by: David Pascual <davherna@redhat.com>
1 parent 3052946 commit 8f406d2

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Dockerfile.latest-unpriviledged

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This container images makes the necessary modifications in the
2+
# inherited image (which could be OSS NGINX or NGINX Plus) in order
3+
# to allow running NGINX S3 Gateway as a non root user.
4+
# Steps are based on the official unpriviledged container:
5+
# https://github.com/nginxinc/docker-nginx-unprivileged/blob/main/Dockerfile-debian.template
6+
FROM nginx-s3-gateway
7+
8+
# Implement changes required to run NGINX as an unprivileged user
9+
RUN sed -i "/^server {/a \ listen 8080;" /etc/nginx/templates/default.conf.template \
10+
&& sed -i '/user nginx;/d' /etc/nginx/nginx.conf \
11+
&& sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf \
12+
&& 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 \
13+
# Nginx user must own the cache and etc directory to write cache and tweak the nginx config
14+
&& chown -R nginx:0 /var/cache/nginx \
15+
&& chmod -R g+w /var/cache/nginx \
16+
&& chown -R nginx:0 /etc/nginx \
17+
&& chmod -R g+w /etc/nginx
18+
19+
EXPOSE 8080
20+
21+
USER nginx

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Dockerfile.buildkit.plus Dockerfile with the same configuration as Docke
7070
with support for hiding secrets using Docker's Buildkit
7171
Dockerfile.latest-njs Dockerfile that inherits from the last build of the gateway and
7272
then builds and installs the latest version of njs from source
73+
Dockerfile.latest-unpriviledged Dockerfiles that inherits from the last build of the gateway and
74+
makes the necessary modifications to allow running the container
75+
as a non root, unpriviledged user.
7376
settings.example Docker env file example
7477
standalone_ubuntu_oss_install.sh install script that will install the gateway as a Systemd service
7578
test.sh test launcher

docs/getting_started.md

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ docker run --env-file ./settings --publish 80:80 --name nginx-s3-gateway \
148148
nginx-s3-gateway:oss
149149
```
150150

151+
In the same way, if you want to use NGINX OSS container image as a non-root, unpriviledged user,
152+
you can build it as follows:
153+
```
154+
docker build --file Dockerfile.latest-unpriviledged --tag nginx-s3-gateway --tag nginx-s3-gateway:latest-unpriviledged-oss .
155+
```
156+
And run the image binding the container port 8080 to 80 in the host like:
157+
```
158+
docker run --env-file ./settings --publish 80:8080 --name nginx-s3-gateway \
159+
nginx-s3-gateway:latest-unpriviledged-oss
160+
```
161+
151162
### Building the NGINX Plus Container Image
152163

153164
In order to build the NGINX Plus container image, copy your NGINX Plus

0 commit comments

Comments
 (0)