Skip to content

Commit c454868

Browse files
sravankasuBryan Latten
authored andcommitted
Allow docker-nginx to run under a non root user (#68)
* Allow docker-nginx to run under a non root user * remove additional layer when setting permissions * Add expectations and fix nginx error log permissions * set s6 root read only flag * fix s6-chown issues * merging comments * specify username in COPY command
1 parent cddac88 commit c454868

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RUN /bin/bash -e /security_updates.sh && \
3838
/bin/bash -e /clean.sh
3939

4040
# Overlay the root filesystem from this repo
41-
COPY ./container/root /
41+
COPY --chown=www-data ./container/root /
4242

4343
# Set nginx to listen on defined port
4444
# NOTE: order of operations is important, new config had to already installed from repo (above)
@@ -48,11 +48,13 @@ COPY ./container/root /
4848
# - Remove older WOFF mime-type
4949
# - Add again with newer mime-type
5050
# - Also add mime-type for WOFF2
51+
# Set permissions to allow image to be run under a non root user
5152
RUN sed -i "s/listen [0-9]*;/listen ${CONTAINER_PORT};/" $CONF_NGINX_SITE && \
5253
mkdir /tmp/.nginx && \
5354
sed -i "/application\/font-woff/d" /etc/nginx/mime.types && \
5455
sed -i "s/}/\n font\/woff woff;&/" /etc/nginx/mime.types && \
55-
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types
56+
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types && \
57+
/bin/bash -e /scripts/set_permissions.sh
5658

5759
RUN goss -g /tests/ubuntu/nginx.goss.yaml validate && \
5860
/aufs_hack.sh

Dockerfile-alpine

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ RUN apk update --no-cache && \
2323
/bin/bash -e /clean.sh
2424

2525
# Overlay the root filesystem from this repo
26-
COPY ./container/root /
26+
COPY --chown=www-data ./container/root /
2727

2828
# - Set nginx to listen on defined port
29-
# - Fix permissions to run unprivileged
3029
# - Make temp directory for .nginx runtime files
3130
# - Some operations can be completely removed once this ticket is resolved:
3231
# - https://trac.nginx.org/nginx/ticket/1243
3332
# - Remove older WOFF mime-type
3433
# - Add again with newer mime-type
3534
# - Also add mime-type for WOFF2
35+
# - Fix permissions for nginx folders and run set_permissions.sh to allow running image under a non root user
3636
RUN sed -i "s/listen [0-9]*;/listen ${CONTAINER_PORT};/" $CONF_NGINX_SITE && \
3737
bash -c "chown www-data:www-data /var/{lib,log}/nginx -Rh" && \
3838
bash -c "chmod 0755 -R /var/{lib,log}/nginx" && \
3939
mkdir /tmp/.nginx && \
4040
sed -i "/application\/font-woff/d" /etc/nginx/mime.types && \
4141
sed -i "s/}/\n font\/woff woff;&/" /etc/nginx/mime.types && \
42-
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types
42+
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types && \
43+
/bin/bash -e /scripts/set_permissions.sh
4344

4445
RUN goss -g /tests/alpine/nginx.goss.yaml validate && \
4546
/aufs_hack.sh

Dockerfile-centos

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ RUN /bin/bash -e /security_updates.sh && \
2121
/bin/bash -e /clean.sh
2222

2323
# Overlay the root filesystem from this repo
24-
COPY ./container/root /
24+
COPY --chown=nginx ./container/root /
2525

2626
# - Set nginx to listen on defined port
2727
# - NOTE: order of operations is important, new config had to already installed from repo (above)
@@ -32,12 +32,14 @@ COPY ./container/root /
3232
# - Remove older WOFF mime-type
3333
# - Add again with newer mime-type
3434
# - Also add mime-type for WOFF2
35+
# Set permissions to allow image to be run under a non root user
3536
RUN sed -i "s/listen [0-9]*;/listen ${CONTAINER_PORT};/" $CONF_NGINX_SITE && \
3637
mkdir /tmp/.nginx && \
3738
sed -i "s/^user .*$/user ${NOT_ROOT_USER};/" ${CONF_NGINX_SERVER} && \
3839
sed -i "/application\/font-woff/d" /etc/nginx/mime.types && \
3940
sed -i "s/}/\n font\/woff woff;&/" /etc/nginx/mime.types && \
40-
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types
41+
sed -i "s/}/\n font\/woff2 woff2;\n&/g" /etc/nginx/mime.types && \
42+
/bin/bash -e /scripts/set_permissions.sh
4143

4244
RUN goss -g /tests/centos/nginx.goss.yaml validate && \
4345
/aufs_hack.sh

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ See parent(s) [docker-base](https://github.com/behance/docker-base) for addition
1818
### Expectations
1919

2020
- Applications must copy their html/app into the `/var/www/html` folder
21+
- Any new script/file that needs to be added must be given proper permissions/ownership to the non root user through `container/root/scripts/set_permissions.sh`. This is to ensure that the image can be run under a non root user.
2122
- NOTE: Nginx is exposed and bound to an unprivileged port, `8080`
2223

2324

container/root/s6-setuidgid

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
current_user=$(whoami)
4+
set_user=${1}
5+
if [ "$current_user" == "$set_user" ]; then
6+
echo "deprivilege noop (s6-setuidgid): current user = set user: $set_user."
7+
shift
8+
exec $@
9+
else
10+
echo "***Warning*** Starting image as $current_user and deprivileging service to run under $set_user, future versions of this image will use $NOT_ROOT_USER as the default user set via https://docs.docker.com/engine/reference/builder/#user."
11+
exec /scripts/s6-setuidgid $@
12+
fi
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash -e
2+
3+
# Replace existing s6-setuidgid binary with the script we've written
4+
mv /bin/s6-setuidgid /scripts/s6-setuidgid && mv /s6-setuidgid /bin/
5+
6+
# Create the nginx error log file before nginx does so that nginx does not complain about permissions on this file
7+
touch /var/log/nginx/error.log
8+
9+
# Assign ownership of required scripts/directories to the non root user
10+
rm /var/run && mkdir -p /var/run/s6 && chown -R ${NOT_ROOT_USER} /etc/services.d/ /etc/services-available/ /bin/s6-* /etc/nginx/ /tmp/ /var/
11+
12+
chmod 755 -R /etc/s6/ /bin/s6-* /scripts/s6-setuidgid /etc/nginx/ /var/
13+
14+
# Make s6 give non root user ownership to user provided files instead of root
15+
# List of files/folders given ownership available here: https://github.com/just-containers/s6-overlay/blob/master/builder/overlay-rootfs/etc/s6/init/init-stage2-fixattrs.txt
16+
sed -i "s/root/${NOT_ROOT_USER}/" /etc/s6/init/init-stage2-fixattrs.txt

0 commit comments

Comments
 (0)