Images for the stack
Two Dockerfiles: one for the Spring Boot API, one for the Angular front end. Both multi-stage, both non-root, both small enough to pull quickly on every deploy.
Why multi-stage
A single-stage image ships Maven, the JDK, the source code and the local repository — roughly 800 MB, most of it useless at run time and all of it extra attack surface. The runtime stage below is around 220 MB.
The Spring Boot image
# ---------- build ---------- FROM maven:3.9-eclipse-temurin-21 AS build WORKDIR /build # dependencies first: this layer survives every source-only change COPY pom.xml . RUN mvn -B dependency:go-offline COPY src ./src RUN mvn -B clean package -DskipTests # split the fat JAR into layers that change at different rates RUN java -Djarmode=layertools -jar target/*.jar extract --destination extracted # ---------- runtime ---------- FROM eclipse-temurin:21-jre-alpine AS runtime RUN addgroup -S app && adduser -S -G app app \ && apk add --no-cache curl WORKDIR /app # ordered least- to most-volatile: only the last layer changes per commit COPY --from=build --chown=app:app /build/extracted/dependencies/ ./ COPY --from=build --chown=app:app /build/extracted/spring-boot-loader/ ./ COPY --from=build --chown=app:app /build/extracted/snapshot-dependencies/ ./ COPY --from=build --chown=app:app /build/extracted/application/ ./ USER app EXPOSE 8080 ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75 -XX:+ExitOnOutOfMemoryError" HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \ CMD curl -fsS http://localhost:8080/actuator/health/liveness || exit 1 ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
Your application classes are a few hundred kilobytes; the dependencies are tens of megabytes and change rarely. Extracting them into separate layers means a normal commit pushes and pulls only the small top layer. Deploys get dramatically faster.
MaxRAMPercentageModern JVMs read the cgroup limit, but the default heap is only 25% of it — on a 1 GB container that is a 256 MB heap and constant GC pressure. Set 75% and leave the rest for metaspace, threads and native buffers.
The Angular image
# ---------- build ---------- FROM node:20-alpine AS build WORKDIR /build COPY package.json package-lock.json ./ RUN npm ci # ci, not install: respects the lock file exactly COPY . . RUN npm run build -- --configuration=production # ---------- runtime ---------- FROM nginx:1.27-alpine AS runtime COPY --from=build /build/dist/oms/browser /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf # run unprivileged: nginx must not need port 80 RUN sed -i 's/listen\s*80;/listen 8080;/' /etc/nginx/conf.d/default.conf \ && chown -R nginx:nginx /usr/share/nginx/html /var/cache/nginx \ && touch /var/run/nginx.pid && chown nginx:nginx /var/run/nginx.pid USER nginx EXPOSE 8080 HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:8080/ || exit 1 CMD ["nginx", "-g", "daemon off;"]
server {
listen 8080;
server_name _;
root /usr/share/nginx/html;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;
# hashed build artefacts: cache hard
location ~* \.(js|css|woff2|svg|png|jpg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
# index.html must never be cached, or users get a stale app shell
location = /index.html {
add_header Cache-Control "no-store, must-revalidate";
}
# SPA fallback: every unknown path is an Angular route
location / {
try_files $uri $uri/ /index.html;
}
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options DENY always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
}
Without try_files … /index.html, a browser refresh on
/orders/42 asks Nginx for a file that does not exist and gets a 404. Angular
never loads, so its router never sees the URL. Every deep link in the application breaks.
.dockerignore earns its keep immediately
.git .github node_modules target dist **/*.env **/secrets* .idea .vscode README.md docker-compose*.yml
Everything not excluded is sent to the daemon as build context and can be accidentally
COPYed into a layer. Excluding node_modules alone often cuts the
context from hundreds of megabytes to a few.
Runtime configuration for a static front end
environment.ts at build time
That conflicts with "build once, deploy everywhere". Two workable answers: serve the API
under the same origin at /api via a reverse proxy — then no API URL is needed
at all — or write a small config.json at container start and fetch it in an
APP_INITIALIZER. Prefer the first; it also removes CORS entirely.
Hardening checklist
| Practice | Why |
|---|---|
Pin base tags (21-jre-alpine, not latest) | Reproducible builds |
USER app | A container escape starts as an unprivileged user |
| No shell tools you do not need | Fewer things for an attacker to use |
--read-only root filesystem | Blocks writing a payload to disk |
--cap-drop ALL | Removes Linux capabilities the app never uses |
| Scan images in CI | Catches CVEs in the base layer |
docker build -t oms-api:1.4.0 ./api
docker image ls oms-api # size check
docker run --rm oms-api:1.4.0 id # must not print uid=0(root)
docker history oms-api:1.4.0 # no secrets in any layer
docker run -d --name api --read-only --cap-drop ALL \
--tmpfs /tmp -p 8080:8080 oms-api:1.4.0
docker inspect --format '{{.State.Health.Status}}' api # → healthy
Why does the Dockerfile copy pom.xml and run dependency:go-offline before copying src?
A layer is invalidated when its inputs change. Copying source last means editing a Java file only busts the final layers — dependencies are already there. Build time drops from minutes to seconds.