Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable running nginx containers with read-only root fs #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ target "nginx" {
dockerfile="nginx/Dockerfile"
args = {
NGINX_VERSION = "${nginx-version}"
NGINX_HOME = "/etc/nginx"
}
tags = concat(tag("nginx"),
vtag("${crs-version}", "nginx")
Expand All @@ -134,6 +135,7 @@ target "nginx-alpine" {
dockerfile="nginx/Dockerfile-alpine"
args = {
NGINX_VERSION = "${nginx-version}"
NGINX_HOME = "/etc/nginx"
}
tags = concat(tag("nginx-alpine"),
vtag("${crs-version}", "nginx-alpine")
Expand All @@ -147,6 +149,7 @@ target "openresty-alpine-fat" {
args = {
OPENRESTY_VERSION = "${openresty-version}"
NGINX_VERSION = "${nginx-version}"
NGINX_HOME = "/usr/local/openresty/nginx"
}
tags = concat(tag("openresty-alpine-fat"),
vtag("${crs-version}", "openresty-alpine-fat")
Expand Down
16 changes: 15 additions & 1 deletion nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FROM nginxinc/nginx-unprivileged:${NGINX_VERSION} as build
ARG MODSEC3_VERSION="n/a"
ARG LMDB_VERSION="n/a"
ARG LUA_VERSION="n/a"
ARG NGINX_HOME="n/a"

USER root

Expand Down Expand Up @@ -109,9 +110,12 @@ FROM nginxinc/nginx-unprivileged:${NGINX_VERSION}
ARG MODSEC3_VERSION
ARG LMDB_VERSION
ARG LUA_VERSION
ARG NGINX_HOME

LABEL maintainer="Felipe Zipitria <[email protected]>"

ENV NGINX_HOME="${NGINX_HOME}"

ENV ACCESSLOG=/var/log/nginx/access.log \
BACKEND=http://localhost:80 \
DNS_SERVER= \
Expand Down Expand Up @@ -225,7 +229,17 @@ RUN set -eux; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so.3; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so; \
ln -sv /opt/owasp-crs /etc/modsecurity.d/; \
chown nginx:nginx /opt/owasp-crs /etc/modsecurity.d
# move files that need to be writable so we can use tmpfs for them later
mkdir -p /usr/local/bootstrap/nginx /usr/local/bootstrap/modsecurity.d /usr/local/bootstrap/owasp-crs; \
mv "${NGINX_HOME}/"* /usr/local/bootstrap/nginx/; \
mv /etc/modsecurity.d/* /usr/local/bootstrap/modsecurity.d/; \
mv /opt/owasp-crs/* /usr/local/bootstrap/owasp-crs/; \
chown nginx:nginx \
/opt/owasp-crs \
/etc/modsecurity.d \
"${NGINX_HOME}" \
/usr/local/bootstrap/*


USER nginx

Expand Down
15 changes: 14 additions & 1 deletion nginx/Dockerfile-alpine
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FROM nginxinc/nginx-unprivileged:${NGINX_VERSION}-alpine as build

ARG MODSEC3_VERSION="n/a"
ARG LUA_VERSION="n/a"
ARG NGINX_HOME="n/a"

USER root

Expand Down Expand Up @@ -104,9 +105,12 @@ FROM nginxinc/nginx-unprivileged:${NGINX_VERSION}-alpine

ARG MODSEC3_VERSION
ARG LUA_VERSION
ARG NGINX_HOME

LABEL maintainer="Felipe Zipitria <[email protected]>"

ENV NGINX_HOME="${NGINX_HOME}"

ENV ACCESSLOG=/var/log/nginx/access.log \
BACKEND=http://localhost:80 \
DNS_SERVER= \
Expand Down Expand Up @@ -223,7 +227,16 @@ RUN set -eux; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so.3; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so; \
ln -sv /opt/owasp-crs /etc/modsecurity.d/; \
chown nginx:nginx /opt/owasp-crs /etc/modsecurity.d
# move files that need to be writable so we can use tmpfs for them later
mkdir -p /usr/local/bootstrap/nginx /usr/local/bootstrap/modsecurity.d /usr/local/bootstrap/owasp-crs; \
mv "${NGINX_HOME}/"* /usr/local/bootstrap/nginx/; \
mv /etc/modsecurity.d/* /usr/local/bootstrap/modsecurity.d/; \
mv /opt/owasp-crs/* /usr/local/bootstrap/owasp-crs/; \
chown nginx:nginx \
/opt/owasp-crs \
/etc/modsecurity.d \
"${NGINX_HOME}" \
/usr/local/bootstrap/*

USER nginx

Expand Down
13 changes: 13 additions & 0 deletions nginx/docker-entrypoint.d/0-move-writables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu

LC_ALL=C
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

cp -r /usr/local/bootstrap/nginx/* "${NGINX_HOME}/"
cp -r /usr/local/bootstrap/modsecurity.d/* /etc/modsecurity.d/
cp -r /usr/local/bootstrap/owasp-crs/* /opt/owasp-crs/

exit 0
Loading