-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDockerfile
More file actions
114 lines (90 loc) · 4.33 KB
/
Dockerfile
File metadata and controls
114 lines (90 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# ---------- Base: Node.js 22 on UBI 9 ----------
FROM registry.access.redhat.com/ubi9/ubi:9.7 AS node-base
RUN dnf module enable nodejs:22 -y && \
dnf install -y --nodocs nodejs npm && \
dnf clean all
# ---------- Stage 1: Install npm dependencies ----------
FROM node-base AS deps
WORKDIR /app
COPY package.json package-lock.json ./
COPY scripts/ scripts/
RUN npm ci
# ---------- Stage 2: Build Next.js ----------
FROM node-base AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ARG GIT_SHA=unknown
ARG APP_VERSION=dev
ENV GIT_SHA=${GIT_SHA}
ENV NEXT_PUBLIC_APP_VERSION=${APP_VERSION}
# Reference APP_VERSION in the RUN to bust Docker build cache when it changes
RUN echo "Building version: ${APP_VERSION} (${GIT_SHA})" && npm run build
# ---------- Stage 3: Build minimal rootfs ----------
FROM registry.access.redhat.com/ubi9/ubi:9.7 AS rootfs-builder
# Install only the shared libraries Node.js needs at runtime
RUN mkdir -p /mnt/rootfs && \
dnf install --installroot /mnt/rootfs --releasever 9 \
--setopt install_weak_deps=0 --nodocs -y \
glibc-minimal-langpack \
ca-certificates \
libstdc++ \
openssl-libs \
zlib \
brotli \
&& dnf --installroot /mnt/rootfs clean all \
&& rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/* /mnt/rootfs/tmp/*
# Create non-root user and app directory
RUN echo 'nextjs:x:1001:0:Next.js:/app:/sbin/nologin' >> /mnt/rootfs/etc/passwd && \
echo 'nodejs:x:1001:' >> /mnt/rootfs/etc/group && \
mkdir -p /mnt/rootfs/app /mnt/rootfs/usr/local/bin && \
chown -R 1001:0 /mnt/rootfs/app
# ---------- STIG hardening (applicable subset for ubi-micro) ----------
# Derived from DISA STIG controls applied in registry.access.redhat.com/ubi9/ubi-stig
# Crypto policy: FIPS-grade defaults for OpenSSL
RUN if [ -f /mnt/rootfs/etc/pki/tls/openssl.cnf ]; then \
echo -e "\n[algorithm_sect]\ndefault_properties = fips=yes" >> /mnt/rootfs/etc/pki/tls/openssl.cnf; \
fi
# Disable core dumps (xccdf_org.ssgproject.content_rule_disable_users_coredumps)
RUN mkdir -p /mnt/rootfs/etc/security/limits.d && \
echo "* hard core 0" > /mnt/rootfs/etc/security/limits.d/50-coredump.conf
# Max concurrent login sessions (xccdf_org.ssgproject.content_rule_accounts_max_concurrent_login_sessions)
RUN echo "* hard maxlogins 10" > /mnt/rootfs/etc/security/limits.d/50-maxlogins.conf
# Ensure no empty passwords (xccdf_org.ssgproject.content_rule_no_empty_passwords)
RUN sed -i 's/\bnullok\b//g' /mnt/rootfs/etc/pam.d/* 2>/dev/null || true
# Restrictive umask (xccdf_org.ssgproject.content_rule_accounts_umask_etc_login_defs)
RUN if [ -f /mnt/rootfs/etc/login.defs ]; then \
sed -i 's/^UMASK.*/UMASK\t\t077/' /mnt/rootfs/etc/login.defs; \
fi
# Clean machine-id (regenerated at runtime)
RUN rm -f /mnt/rootfs/etc/machine-id && \
touch /mnt/rootfs/etc/machine-id && \
chmod 0444 /mnt/rootfs/etc/machine-id
# Ensure GPG checking for packages (xccdf_org.ssgproject.content_rule_ensure_gpgcheck_local_packages)
RUN if [ -f /mnt/rootfs/etc/dnf/dnf.conf ]; then \
sed -i 's/^localpkg_gpgcheck.*/localpkg_gpgcheck=1/' /mnt/rootfs/etc/dnf/dnf.conf || \
echo "localpkg_gpgcheck=1" >> /mnt/rootfs/etc/dnf/dnf.conf; \
fi
# Remove leftover cache/tmp artifacts
RUN rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/* /mnt/rootfs/tmp/*
# ---------- Stage 4: UBI 9 Micro runtime ----------
FROM registry.access.redhat.com/ubi9/ubi-micro:9.7
# Copy minimal rootfs (glibc, libstdc++, openssl, brotli, ca-certs, user/group)
COPY --from=rootfs-builder /mnt/rootfs /
# Copy Node.js binary and shared libraries from build stage (compiled against same RHEL 9 glibc)
COPY --from=node-base /usr/bin/node /usr/local/bin/
COPY --from=node-base /usr/lib64/libnode.so* /usr/lib64/
WORKDIR /app
ENV NODE_ENV=production \
HOSTNAME=0.0.0.0 \
PORT=3000 \
BACKEND_URL=http://backend:8080
# Copy Next.js standalone output (root-owned, read-only for all users)
COPY --from=build --chown=root:root --chmod=555 /app/public ./public
COPY --from=build --chown=root:root --chmod=555 /app/.next/standalone ./
COPY --from=build --chown=root:root --chmod=555 /app/.next/static ./.next/static
# Next.js needs a writable cache directory for image optimization at runtime
RUN mkdir -p .next/cache && chown 1001:0 .next/cache
USER 1001
EXPOSE 3000
CMD ["node", "server.js"]