Skip to content

Keep app code read-only for www-data in Dockerfile examples - #758

Closed
Abdulmajeed-Jamaan wants to merge 2 commits into
serversideup:mainfrom
Abdulmajeed-Jamaan:docs/read-only-app-code
Closed

Abdulmajeed-Jamaan wants to merge 2 commits into
serversideup:mainfrom
Abdulmajeed-Jamaan:docs/read-only-app-code

Conversation

@Abdulmajeed-Jamaan

@Abdulmajeed-Jamaan Abdulmajeed-Jamaan commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The production Dockerfile examples copy the whole app with COPY --chown=www-data:www-data . /var/www/html. That makes all the code writable by www-data, the user PHP runs as: app/, vendor/, config/, routes/, and public/.

The images run unprivileged, but this setup undoes most of the benefit. Any bug that lets an attacker write a file, such as an unsafe upload or path traversal, becomes code execution. The attacker can overwrite an existing PHP file, or drop a new .php into public/ where the web server runs it.

Fix

Copy the code as root, and give www-data only the two directories Laravel must write to at runtime:

ENV LOG_CHANNEL=stderr

COPY . /var/www/html
USER root
RUN chown root:root /var/www/html \
    && chown -R www-data:www-data storage/app storage/framework/views
USER www-data
Writable Why
storage/app/ uploads and other stored files (public/storage links to storage/app/public)
storage/framework/views/ compiled Blade, including inline templates rendered with Blade::render() at runtime

Everything else Laravel writes by default moves elsewhere, and the packaging page documents this in a table:

  • Logs: LOG_CHANNEL=stderr instead of storage/logs/.
  • Cache and sessions: the database or redis driver instead of file.
  • bootstrap/cache: run route:cache and event:cache in the Dockerfile, and turn off the Laravel automations' optimize and cache steps. config:cache stays out of the build because it would save secrets into the image.

Notes on the Dockerfile:

  • COPY without --chown leaves files owned by root, so PHP can read them but not change them.
  • chown root:root /var/www/html is needed because the image creates that directory owned by www-data, and COPY doesn't change ownership of an existing directory.
  • COPY --chown=www-data:www-data <dir> doesn't work as a shortcut. It changes ownership of what's inside the directory, not the directory itself.
  • In the multi-stage example, composer dump-autoload now runs as root (COMPOSER_ALLOW_SUPERUSER=1 is already set in every variation), and the chown runs after it.

Changes

  • 4.deployment-and-production/1.packaging-your-app-for-deployment.md: both examples, a caution against --chown on the whole app, and a section on what Laravel needs with this setup
  • 5.customizing-the-image/5.understanding-file-permissions.md: the production stage
  • 1.getting-started/7.migrating-from-official-php-images.md: the before/after example and the "Give www-data your files" step

6.upgrading/4.upgrade-to-v3.md still shows the old line. I left that historical page unchanged.

Testing

I built a test image from serversideup/php:8.4-frankenphp-trixie with the new pattern. As www-data:

  • Writable: storage/app, storage/app/public, and storage/framework/views.
  • Read-only: /var/www/html, public/, storage/, storage/framework/cache, bootstrap/cache, and the app's PHP files.

The docs told users to COPY the whole app with --chown=www-data:www-data,
which lets the PHP process overwrite its own code, including public/.
Copy code as root instead and give www-data only the directories the app
writes to (storage/ and bootstrap/cache/ for Laravel).
storage/app holds uploads and storage/framework/views holds compiled
Blade, including inline Blade::render() templates. Everything else
stays read-only, so document how Laravel's logs, file cache, sessions,
and bootstrap/cache writes move elsewhere.
@Abdulmajeed-Jamaan

Copy link
Copy Markdown
Contributor Author

Closing in favor of #759, which covers this along with the related image defaults so the approach can be agreed on first.

@Abdulmajeed-Jamaan
Abdulmajeed-Jamaan deleted the docs/read-only-app-code branch November 25, 2026 14:00
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.

1 participant