From dd10e273350c8e232c4b1235dc1ec32f18cb96ff Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 08:21:49 +0900 Subject: [PATCH] =?UTF-8?q?feat(runtime):=20frontend=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=EB=A5=BC=20=EB=B9=84=ED=8A=B9=EA=B6=8C=20nginx?= =?UTF-8?q?=EB=A1=9C=208080=EC=97=90=EC=84=9C=20=EB=8F=8C=EB=A6=B0?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit frontend 이미지는 nginx 공식 이미지라 root로 시작해 80번을 열고 worker를 nginx 사용자로 내린다. 그래서 Kubernetes에서 runAsNonRoot와 drop ALL을 둘 수 없다(k3s 성숙도 점검 2026-11-11, 7.5절). 운영 매니페스트는 allowPrivilegeEscalation: false만 두고 PSA restricted 경고를 낸다. runtime base를 nginxinc/nginx-unprivileged:1.27.5-alpine(digest 고정)으로 바꾼다. 예전 base와 같은 NGINX 1.27.5·Alpine이고 wget도 있어 HEALTHCHECK가 그대로 돈다. 파일을 고치는 동안만 root로 있다가 USER 101로 돌아간다. 생성되는 server 블록은 listen 8080이고, EXPOSE·HEALTHCHECK·dev-stack 포트 매핑도 8080으로 맞춘다. 버린 대안: Debian판 nginx-unprivileged. wget이 없어 HEALTHCHECK가 늘 실패한다. 80번을 그대로 두고 NET_BIND_SERVICE만 남기는 안도 버렸다. drop ALL을 못 하고 PSA restricted도 통과하지 못한다. 위험·한계: 이 이미지는 8080을 열므로 운영 매니페스트의 포트를 같이 바꿔야 한다. deploy/production/frontend-nonroot가 그 일을 하고, 기본 경로에는 넣지 않는다. dev-stack의 443·/etc/nginx/tls 부분은 이미지에 없는 파일을 쓰는 예전 상태 그대로다. 이 repo의 pnpm 게이트와 전체 docker build는 돌리지 않았다(heavy lock 대기가 길어 취소). 검증: desktop에서 이 Dockerfile의 runtime 단계를 그대로 쓰고, 빌드 단계만 운영 이미지 7a4e201의 dist와 conf(listen 8080으로 바꿈)로 대신해 이미지를 만들었다. --user 101 --cap-drop ALL --security-opt no-new-privileges --sysctl net.ipv4.ip_unprivileged_port_start=1024로 띄우면 GET /config.json 200, GET / 200, CapEff 0, NoNewPrivs 1이고, Docker health는 healthy다. --- Dockerfile | 17 +++++++++++++---- docker-compose.dev-stack.yml | 2 +- scripts/generate-nginx-config.ts | 5 +++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67e39ce..5851aef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,7 +68,14 @@ RUN corepack pnpm build \ # --------------------------------------------------------------------------- # runtime # --------------------------------------------------------------------------- -FROM nginx@sha256:65645c7bb6a0661892a8b03b89d0743208a18dd2f3f17a54ef4b76fb8e2f2a10 AS runtime +# Unprivileged NGINX 1.27.5 on Alpine, the same release and distribution as the +# previous nginx base: the master runs as uid 101 and listens on 8080, so the +# container needs no root and no capability. Kubernetes runs it with +# runAsNonRoot and capabilities drop ALL (deploy/production/frontend-nonroot). +FROM nginxinc/nginx-unprivileged@sha256:65e3e85dbaed8ba248841d9d58a899b6197106c23cb0ff1a132b7bfe0547e4c0 AS runtime + +# The file edits below need root; the image drops back to uid 101 at the end. +USER root # Replaces the packaged default server block; the generated file is the whole # server definition, including the BFF proxy locations. @@ -81,16 +88,18 @@ COPY --from=build /src/dist/ /usr/share/nginx/html/ # not a page either. RUN rm -f /usr/share/nginx/html/nginx.conf /usr/share/nginx/html/server.mjs \ && rm -rf /usr/share/nginx/html/.vite \ - # The build writes config.json 0600, which nginx (running as `nginx`) cannot + # The build writes config.json 0600, which nginx (running as uid 101) cannot # read — the container came up healthy and answered 403 for the one file the # SPA needs before it can boot. Normalise what is served to world-readable. && chmod -R a+rX /usr/share/nginx/html -EXPOSE 80 +USER 101 + +EXPOSE 8080 # No `nginx -t` here: proxy_pass names are resolved when the config loads, and # `backend`/`keycloak` only exist on the compose network. The container's own # startup is the check, and it fails loudly. HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://127.0.0.1${VITE_ROUTER_BASE_PATH:-/}config.json || exit 1 + CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:8080${VITE_ROUTER_BASE_PATH:-/}config.json || exit 1 diff --git a/docker-compose.dev-stack.yml b/docker-compose.dev-stack.yml index 9e11040..780dd53 100644 --- a/docker-compose.dev-stack.yml +++ b/docker-compose.dev-stack.yml @@ -163,7 +163,7 @@ services: - "cp /etc/nginx/tls/server.crt /tls-public/server.crt && exec nginx -g 'daemon off;'" ports: - - "${PUBLIC_HTTP_PORT:-8088}:80" + - "${PUBLIC_HTTP_PORT:-8088}:8080" - "${PUBLIC_PORT:-8443}:443" depends_on: backend: { condition: service_started } diff --git a/scripts/generate-nginx-config.ts b/scripts/generate-nginx-config.ts index f17cd89..3b27e2d 100644 --- a/scripts/generate-nginx-config.ts +++ b/scripts/generate-nginx-config.ts @@ -136,10 +136,11 @@ ${secure} # config/hosting/cache-policy.json # # Plain HTTP on purpose: the edge terminates TLS and this container is only ever -# reached from inside the deployment network. +# reached from inside the deployment network. 8080, not 80: the runtime image is +# unprivileged NGINX, which cannot bind a port below 1024 without a capability. server { - listen 80; + listen 8080; server_name _; root /usr/share/nginx/html; index index.html;