# Add a Dockerfile to an aiohttp App

Yes. An agent writes a Dockerfile and compose file for your aiohttp app with Postgres, points its YAML config at the container network, runs it, and opens a PR.

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

## What the agent does

The agent reads how the app is installed, configured and started before it writes a single `FROM`, because an aiohttp app's container is only as right as its config. In a project laid out like the polls demo in aio-libs/aiohttp-demos, `demos/polls/` is one of nine demos sharing a root `Makefile` whose `install` target pip-installs every demo's requirements and whose `test` target runs `pytest demos/polls` among the others; `.github/workflows/ci.yml` runs that on Python 3.12 with `postgres` and `redis` services. The demo's `requirements-dev.txt` pins `aiohttp==3.13.5`, `aiohttp-jinja2==1.6`, `asyncpg==0.32.0`, `trafaret_config==2.0.2` and `SQLalchemy==2.0.54`, then the test tools, while `setup.py`'s `install_requires` still names `aiopg[sa]`, which the code no longer imports. The app starts with `python -m aiohttpdemo_polls`, whose `__main__.py` calls `main`, which reads `config/polls.yaml` through `trafaret_config` with a `-c` option and calls `web.run_app` on the configured `host` and `port`; the config says `127.0.0.1`, `8080`, and a Postgres at `localhost:5432` named `aiohttpdemo_polls`. `db.py` builds a `postgresql+asyncpg://` URL from that block in a `cleanup_ctx`, and `init_db.py` creates the role and database through an admin connection whose host is hardcoded to `localhost`. Templates and static files ship as `package_data`. Three other demos have a `docker-compose.yml`; this one has nothing.

The agent writes `demos/polls/Dockerfile` on `python:3.12-slim`, the version CI runs. It first writes `demos/polls/requirements.txt` with the five runtime pins copied from `requirements-dev.txt` and makes `requirements-dev.txt` start with `-r requirements.txt`, so the image installs no pytest and the two files cannot drift; it fixes `setup.py`'s `install_requires` to the same five names so `pip install .` matches. The image copies `requirements.txt` first for layer caching, then the `aiohttpdemo_polls` package, `config/` and `init_db.py`, installs the package, creates a non-root user, exposes 8080, and runs `python -m aiohttpdemo_polls -c config/polls.docker.yaml`. That second config is the real change: `host` becomes `0.0.0.0`, which `trafaret`'s `T.IP` accepts, and `postgres.host` becomes `db`, the compose service name; nothing else moves, so `polls.yaml` keeps working on a laptop. `init_db.py` takes its admin host from the same config block instead of the literal `localhost`, so the one-off `docker compose run app python init_db.py -c config/polls.docker.yaml` reaches the container network too.

`demos/polls/docker-compose.yml` follows the layout of the three demos that have one: a `db` service on the `postgres` image with a `POSTGRES_PASSWORD` and a `pg_isready` healthcheck, and an `app` service built from the Dockerfile that depends on `db` being healthy and publishes 8080. A `.dockerignore` keeps `tests/`, `tox.ini`, `__pycache__` and the `.git` of the monorepo out of the build context. The root `Makefile`, the test configuration in `tests/conftest.py` that reads `config/polls_test.yaml`, and the shared CI job are untouched; the agent adds one `docker` job to `ci.yml` that builds the polls image with `docker build demos/polls` and pushes nothing, so a change that breaks the build fails a pull request.

To check it, the agent installs Docker in its sandbox first (`sudo apt-get install -y docker.io`, then starts `dockerd`) and the `docker-compose` package, since the `docker compose` plugin is not in `docker.io`, then runs `docker build demos/polls`, `docker-compose -f demos/polls/docker-compose.yml up -d`, the `init_db.py` one-off, and `curl http://localhost:8080/` to see the sample questions rendered by the `index.html` template, then `curl -o /dev/null -w '%{http_code}'` on `/poll/1` and a `POST` to `/poll/1/vote` to see the redirect to results. It runs `pytest demos/polls` with the test database from CI's Postgres service to show the suite is unchanged, and `flake8 demos/polls` as the root `lint` target does, since `init_db.py` changed.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add a Dockerfile and a compose file to the polls app in this repository. Read the root Makefile and .github/workflows/ci.yml, then demos/polls/setup.py, requirements-dev.txt, config/polls.yaml, aiohttpdemo_polls/main.py, settings.py, utils.py, db.py, routes.py, init_db.py and tests/conftest.py, and the docker-compose files of the other demos first. Split the runtime pins into demos/polls/requirements.txt that requirements-dev.txt includes, and fix setup.py's install_requires to match. Write a Dockerfile on python:3.12-slim that installs the package, runs as a non-root user, exposes 8080 and starts python -m aiohttpdemo_polls with a config/polls.docker.yaml whose host is 0.0.0.0 and whose postgres host is the compose service name. Make init_db.py take its admin host from the config. Add a docker-compose.yml with a healthy Postgres and the app, a .dockerignore, and a CI job that builds the image without pushing. Build and start it, initialise the database, request the index, a poll and a vote, run the demo's tests and flake8, and open a pull request with the output.",
  "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 `demos/polls/Dockerfile`, `docker-compose.yml`, `.dockerignore`, `requirements.txt` and `config/polls.docker.yaml`, and changes `requirements-dev.txt`, `setup.py`, `init_db.py` and `.github/workflows/ci.yml`. Its description pastes the build output, the compose start-up with the healthcheck passing, the three HTTP exchanges against the running container, and the pytest and flake8 output.

The `CI` workflow runs on the pull request with the new `docker` job beside `lint`, `test` and `doc`, so a reviewer sees the image build on a GitHub runner and the existing jobs unchanged. The run's record keeps the container logs from the sandbox, including the `web.run_app` line showing the bound address.

Before merging, a reviewer decides whether the Postgres image in compose should be pinned to a major version, whether the non-root user and the `HEALTHCHECK` are wanted in the shared demo style, and whether `init_db.py` should run as a compose entrypoint rather than a one-off command. The views, the templates and the tests are unchanged.

## Run it on every aiohttp service without a Dockerfile

A team with several aiohttp services sends this body once per repository to `POST /o/{org}/sessions`, each with its own 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 rather than opening a second pull request. Each run reads its own start-up: a service configured by environment variables gets the host and database address from `environment` in compose instead of a second YAML file, and one on `gunicorn` with `aiohttp.GunicornWebWorker` gets that as the command.

A `webhook_url` on the key reports each `run.completed` with the pull request in `result`, and a `run.failed` names a service whose container the agent could not start, for instance one that binds to a loopback address it could not reconfigure.

## Why run it on Rush?

A Dockerfile for an aiohttp app is short, but the parts that break are around it: a config that binds to loopback, a database host that is a literal, an admin script that cannot reach the container network, a `setup.py` that installs a library the code dropped. The agent reads the config schema and the start-up path, changes the two values that have to change, and runs the whole thing in its own sandbox before it opens anything.

The build log, the compose start-up and the HTTP exchanges stay with the run and are pasted into the pull request, so the review is reading a container that ran rather than a Dockerfile that looks right. Several services can get the same change at once, each built and started on its own.

## Frequently asked questions

### Why a second config file instead of editing polls.yaml?

Because `polls.yaml` is what the README's local setup and `init_db.py` expect, with Postgres on `localhost` and the app on `127.0.0.1`. A container needs `0.0.0.0` and the compose service name, and nothing else, so `polls.docker.yaml` holds those two differences and the Dockerfile passes it with the `-c` option the settings already support.

### Does the image include the tests and the dev tools?

No. The agent splits the runtime pins into `requirements.txt`, which `requirements-dev.txt` includes, so the image installs the five packages the app imports and none of pytest, flake8 or their plugins, and `.dockerignore` keeps `tests/` out of the build context.

### What about the other demos in the repository?

They are untouched. The job is scoped to `demos/polls`, the new CI job builds only that image, and the root `Makefile` and the shared `lint` and `test` targets run exactly as before. The same prompt on another demo's path gives it its own files.

### Why fix setup.py?

Because the image runs `pip install .`, and `install_requires` still names `aiopg`, a driver the code no longer imports, while leaving out `asyncpg` and `SQLAlchemy`, which it does. Matching the five pins means the installed package and the pinned requirements agree, and a reviewer sees the drift fixed rather than papered over.

## More aiohttp jobs

- [Add Health Checks to an aiohttp App](https://getrush.ai/run/add-health-checks/aiohttp/)
- [Add Input Validation to an aiohttp App](https://getrush.ai/run/add-input-validation/aiohttp/)
- [Add OpenTelemetry Tracing to an aiohttp App](https://getrush.ai/run/add-opentelemetry/aiohttp/)

## The same job on other stacks

- [Add a Dockerfile to a Blazor Web App](https://getrush.ai/run/add-dockerfile/blazor/)
- [Write a Dockerfile for a CodeIgniter 4 App](https://getrush.ai/run/add-dockerfile/codeigniter/)
- [Write a Dockerfile for a discord.js Bot](https://getrush.ai/run/add-dockerfile/discord-js/)

## 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/)
