# Add a Dockerfile to a Slim 4 App

Yes. An agent writes a two-stage Dockerfile for a Slim 4 app, Composer then Apache serving public/, builds and runs it in its sandbox, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-dockerfile/slim/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

A Slim app's image is decided by three files, the Composer manifest, the front controller and whatever already describes how to run it, so the agent reads those first. In an app laid out like slimphp/Slim-Skeleton, `composer.json` requires `php` `^7.4 || ^8.0`, `ext-json`, `slim/slim` `^4.12`, `slim/psr7` `^1.6`, `php-di/php-di` `^6.4` and `monolog/monolog` `^2.9`, with PHPUnit, PHPStan and PHP_CodeSniffer in `require-dev`, and defines `start` as `php -S localhost:8080 -t public` and `test` as `phpunit`. `.gitignore` lists `composer.lock`, so no lock is committed and every install resolves afresh. `public/index.php` builds a PHP-DI container from `app/settings.php`, `app/dependencies.php` and `app/repositories.php`, registers middleware and routes, and leaves `enableCompilation` behind an `if (false)` with a comment that it should be true in production. `public/.htaccess` rewrites every path that is not a file or directory to `index.php`, which is written for Apache's `mod_rewrite`. `app/settings.php` sends Monolog to `php://stdout` when `$_ENV['docker']` is set and to `logs/app.log` otherwise, and `app/dependencies.php` builds that logger with a `UidProcessor` and a `StreamHandler`.

There is a `docker-compose.yml` but no `Dockerfile`: it runs the stock `php:8-alpine` image with the repository bind-mounted at `/var/www`, the command `php -S 0.0.0.0:8080 -t public`, a `logs` volume and `docker: "true"` in the environment, which is what flips the logger to stdout. That is a development setup, with the PHP built-in server and `vendor/` expected on the host. `.github/workflows/tests.yml` runs PHPUnit on PHP 7.4, 8.0, 8.1 and 8.2 with `shivammathur/setup-php` and `ramsey/composer-install`, and runs `phpcs` and `phpstan` on 8.1, so 8.2 is the newest version the suite is known to pass on.

The agent writes a `Dockerfile` in two stages. The first is `composer:2`, which copies `composer.json` alone, runs `composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction --no-scripts`, and so caches the dependency layer until the manifest changes; because no lock is committed, the pull request says that committing `composer.lock` is what makes this layer reproducible, and installs with `--ignore-platform-reqs` only if the Composer image's PHP differs from the runtime's. The second stage is `php:8.2-apache`: it enables `mod_rewrite` with `a2enmod rewrite`, points Apache's document root at `/var/www/html/public` by editing the `sites-available` and `apache2.conf` entries the way the image's documentation shows, copies `vendor/` from the first stage and then `app/`, `public/` and `src/`, sets `ENV docker=true` so `app/settings.php` logs to stdout, creates `var/cache` and `logs` with ownership for `www-data`, and copies `php.ini-production` to `php.ini`. A `HEALTHCHECK` requests `/` with `curl`, which `app/routes.php` answers with `Hello world!`. A `.dockerignore` keeps `vendor/`, `logs/`, `var/cache/`, `tests/`, `.git` and `.github` out of the build context, since the first stage builds `vendor/` itself.

Then it builds and runs the image. The sandbox has no Docker, so the agent installs it (`sudo apt-get install -y docker.io`, then starts `dockerd`), runs `docker build -t slim-app .`, starts a container on port 8080 and requests `/`, `/users` and `/users/1`, which `ListUsersAction` and `ViewUserAction` answer from the five users in `InMemoryUserRepository`, and `/users/99`, which the `HttpErrorHandler` turns into a JSON 404. It reads the container's logs to show the Monolog line arriving on stdout with the processor's uid, and runs `docker image ls` so the pull request can quote the image size it measured. It also runs `composer install` with dev dependencies on the host side of the sandbox and `vendor/bin/phpunit`, so the pull request can say the suite still passes with nothing in `src/` or `app/` changed. `docker-compose.yml` is left as the development setup, with a note on adding a `build:` key for teams that want Compose to use the new image.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a production Dockerfile to this Slim 4 app. Read composer.json, .gitignore, public/index.php, public/.htaccess, app/settings.php, app/dependencies.php, app/routes.php, docker-compose.yml and .github/workflows/tests.yml first. Write a two-stage Dockerfile: a composer:2 stage that copies composer.json and runs composer install with --no-dev --prefer-dist --optimize-autoloader --no-interaction --no-scripts, then a php:8.2-apache stage that enables mod_rewrite, sets the document root to public, copies vendor from the first stage and the app, public and src directories, sets ENV docker=true so the logger writes to stdout, gives www-data ownership of var/cache and logs, uses php.ini-production, and has a HEALTHCHECK on GET /. Add a .dockerignore for vendor, logs, var/cache, tests, .git and .github. Install Docker in the sandbox, build the image, run it on port 8080, request /, /users, /users/1 and /users/99, show the stdout log line and the image size, run vendor/bin/phpunit, and open a pull request that explains why committing composer.lock would make the dependency layer reproducible. Do not change docker-compose.yml or any PHP file.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `Dockerfile` and `.dockerignore` and changes nothing else. Its description shows the `docker build` output with the two stages, the responses for `/`, `/users`, `/users/1` and the JSON 404 for `/users/99`, the Monolog line from the container's stdout, the image size from `docker image ls` and the PHPUnit summary, and explains the `composer.lock` point and the `enableCompilation` flag the agent left alone.

The `Tests` workflow runs PHPUnit across its four PHP versions on the pull request as before, since no PHP file changed; nothing in CI builds the image yet, and the pull request names `docker/build-push-action` as the follow-up for a team that wants it built on every push. The run's record keeps the Docker install, the build, the four requests and the log read, so a reviewer can check each line of the description against the sandbox.

Before merging, a reviewer decides whether the base should move to a newer PHP than the workflow tests, whether `displayErrorDetails` in `app/settings.php` should be false for this image, and whether to turn on container compilation in `public/index.php`. Both are settings the Dockerfile cannot change without touching PHP, so the pull request names them rather than editing them.

## Run it on every Slim repository without a Dockerfile

An organisation with several PHP services starts one run per repository from its own server: a `POST /o/{org}/sessions` each, with the repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own `composer.json` and front controller, so a service that needs `ext-pdo_pgsql` gets `docker-php-ext-install` lines for it, and one with a committed `composer.lock` gets it copied into the first stage for a reproducible install.

A team that wants the image kept honest after it lands runs the follow-up as an automation in the Project with a `github` trigger on `pull_request.opened` for changes to `composer.json`. The trigger adds the pull request after the prompt, and the run rebuilds the image on that branch and comments with the build result and the new image size, so a dependency that pulls in a PHP extension is noticed before merge.

## Why run it on Rush?

A Dockerfile for PHP is where small choices decide whether the container works at all: the document root has to be `public/` or every route 404s, `mod_rewrite` has to be on or `.htaccess` is ignored, `var/cache` has to be writable or the container cannot compile, and the logger only writes to stdout because one environment variable is set. The agent builds and runs the image in its own sandbox and requests real routes, so the pull request arrives with responses rather than a file that looks right.

Every command stays with the run, from the Docker install to the log read, and the pull request quotes them. Writing the same image for a dozen Slim services at once costs no more attention than one, because each run has its own sandbox, its own build and its own pull request.

## Frequently asked questions

### Why Apache rather than the PHP built-in server the compose file uses?

`php -S` is a development server, single-process and documented as not for production. `public/.htaccess` is already written for `mod_rewrite`, so `php:8.2-apache` serves the app as the skeleton intends with one `a2enmod` and a document root change. A team that fronts PHP-FPM with nginx can ask for that layout instead.

### Why PHP 8.2 and not the latest?

Because `.github/workflows/tests.yml` runs the suite on 7.4 through 8.2 and no newer version, so 8.2 is the newest the tests are known to pass on. Raising the base is a one-line change once the workflow matrix includes the next version.

### Will logs still go to the logs directory?

No. `app/settings.php` chooses `php://stdout` when the `docker` environment variable is set, and the Dockerfile sets it, so `docker logs` shows every Monolog line. The `logs/` directory is created and owned by `www-data` only so the app does not fail if that variable is ever unset.

### Why does the pull request talk about composer.lock?

Because `.gitignore` excludes it, every image build resolves the `^` ranges in `composer.json` afresh, so two builds a week apart can ship different Slim or Monolog versions. Committing the lock and copying it into the first stage makes `composer install` reproducible; the agent does not commit it for you since that is a repository policy.

## More Slim jobs

- [Add a Health Check Endpoint to a Slim 4 App](https://getrush.ai/run/add-health-checks/slim/)
- [Add Input Validation to a Slim 4 API](https://getrush.ai/run/add-input-validation/slim/)
- [Add JWT Authentication to a Slim 4 API](https://getrush.ai/run/add-jwt-authentication/slim/)

## The same job on other stacks

- [Add a Dockerfile to a Socket.IO Chat Server](https://getrush.ai/run/add-dockerfile/socket-io/)
- [Write a Dockerfile for a Tokio Server](https://getrush.ai/run/add-dockerfile/tokio/)
- [Add a Dockerfile to a tRPC Next.js App](https://getrush.ai/run/add-dockerfile/trpc/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
