Skip to content

docs: improve Dockerfile for hardened images#2270

Open
dunglas wants to merge 2 commits intomainfrom
fix/mercure-hardened
Open

docs: improve Dockerfile for hardened images#2270
dunglas wants to merge 2 commits intomainfrom
fix/mercure-hardened

Conversation

@dunglas
Copy link
Member

@dunglas dunglas commented Mar 11, 2026

Prevents errors like this one when using Mercure: php-1 | Error: loading initial config: loading new config: loading frankenphp app module: provision frankenphp: failed to provision caddy http: loading http app module: provision http: server srv0: setting up route handlers: route 2: loading handler modules: position 2: loading module 'mercure': provision http.handlers.mercure: provision http.handlers.mercure.bolt: "": invalid transport: open /data/caddy/mercure.db: permission denied

Copilot AI review requested due to automatic review settings March 11, 2026 16:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the hardened/distroless Dockerfile example in the Docker documentation to copy Caddy’s writable directories differently, aiming to ensure the Caddy state/config paths are present and owned correctly when running as nonroot.

Changes:

  • Adjusts the distroless example to COPY /data and /config from the builder stage (instead of only /data/caddy and /config/caddy).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@henderkes
Copy link
Contributor

I don't understand how this fixes the error. Does the nonroot user not have execute permissions on /data?

@dunglas dunglas changed the title docs: fix Mercure support with Docker hardened images docs: improve Dockerfile for hardened images Mar 12, 2026
@dunglas dunglas requested a review from Copilot March 12, 2026 13:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +226 to +228
RUN apt-get update; \
apt-get install -y --no-install-recommends libtree; \
mkdir -p /tmp/libs; \
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RUN line chains commands with ; rather than &&, so a failed apt-get update (or apt-get install) won’t reliably stop the build at the point of failure. For Dockerfile examples, it’s better to use && (or enable set -e) so failures abort immediately and don’t produce confusing downstream errors.

Suggested change
RUN apt-get update; \
apt-get install -y --no-install-recommends libtree; \
mkdir -p /tmp/libs; \
RUN apt-get update && \
apt-get install -y --no-install-recommends libtree && \
mkdir -p /tmp/libs && \

Copilot uses AI. Check for mistakes.
Comment on lines +254 to +255
# Copy your app and Caddyfile
COPY --chown=nonroot:nonroot . /app
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies the entire app tree into /app owned by nonroot. For a hardening example, it’s safer to keep application code owned by root (read-only to nonroot) and only chown the specific writable paths (e.g., /data, /config, /tmp). Owning /app as nonroot makes it easier for a compromised app to modify its own code if the filesystem isn’t mounted read-only.

Suggested change
# Copy your app and Caddyfile
COPY --chown=nonroot:nonroot . /app
# Copy your app (kept root-owned) and Caddyfile
COPY . /app

Copilot uses AI. Check for mistakes.
Comment on lines +237 to 241
# Distroless Debian base image, make sure this matches the Debian version of the builder
FROM gcr.io/distroless/base-debian13
# Docker hardened image alternative
# Docker hardened image alternative:
# FROM dhi.io/debian:13

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Dockerfile snippet changed (removed PATH_TO_APP/PATH_TO_CADDYFILE args and switched -c to --config), but the translated docs/*/docker.md files still document the old version. That leaves the hardening instructions inconsistent across languages; consider updating the translations (or documenting that only the English page is current) to avoid confusing users following non-English docs.

Copilot uses AI. Check for mistakes.
@dunglas
Copy link
Member Author

dunglas commented Mar 12, 2026

@henderkes actually, it's an edge case I found in dunglas/symfony-docker#909, where data and config directories are mounted as volumes. But this is quite common.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants