# Add a Dockerfile to a Blazor Web App

Yes. An agent writes a multi-stage Dockerfile for a Blazor Web App, solves its database and migration startup, runs the container and opens a pull request.

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

## What the agent does

A Blazor Web App that renders on the server is an ASP.NET Core process with a database behind it, so the Dockerfile is the short part and the startup is the real work. In an app like `10.0/BlazorWebAppMovies` in dotnet/blazor-samples, the agent reads `BlazorWebAppMovies.csproj`, which targets `net10.0` and references the SQL Server provider for EF Core and the QuickGrid adapter, and `Program.cs`, which registers a context factory with `UseSqlServer`, adds interactive server components, and calls `SeedData.Initialize` on every start, before the request pipeline is configured. `appsettings.json` holds one connection string, to `(localdb)\mssqllocaldb` with `Trusted_Connection=True`. There is a `Migrations` folder with three migrations, no `global.json`, no Dockerfile and no `.dockerignore`.

Three facts in those files decide whether a container starts at all. LocalDB exists only on Windows and Windows authentication does not travel into a Linux container, so the connection string has to come from the environment as `ConnectionStrings__BlazorWebAppMoviesContext`, with SQL authentication. `SeedData.Initialize` runs `context.Movie.Any()` against whatever database it finds, and nothing in `Program.cs` applies migrations, so on an empty database the process throws before it listens. And `UseHttpsRedirection` is in the pipeline while the container serves plain HTTP on the runtime image's default port, 8080. The agent writes each of these down in the pull request instead of hiding them behind a wrapper script.

The Dockerfile it writes has three stages. The build stage starts from `mcr.microsoft.com/dotnet/sdk:10.0`, copies the project file alone and runs `dotnet restore` so that layer is reused until a package changes, then copies the source and runs `dotnet publish -c Release`; publishing is what produces the manifest `MapStaticAssets` reads. The same stage installs the `dotnet-ef` tool and runs `dotnet ef migrations bundle` to turn the three migrations into one executable. The final stage is `mcr.microsoft.com/dotnet/aspnet:10.0`, runs as the image's non-root user, exposes 8080 and starts `BlazorWebAppMovies.dll`. A separate `migrate` target carries the bundle on the same runtime image and nothing else, so applying migrations is a one-off container run before the app starts and the application code does not change. A `.dockerignore` keeps `bin`, `obj` and `.git` out of the build context.

Then it proves the image. The sandbox has no Docker, so the agent installs it with `sudo apt-get install -y docker.io` and starts `dockerd`. It creates a network, starts Microsoft's SQL Server image with `ACCEPT_EULA` and a generated `MSSQL_SA_PASSWORD`, runs the `migrate` target with a connection string that sets `TrustServerCertificate=True`, and starts the app with the same string. It requests `/` and `/movies`, opens `/movies` in the sandbox's browser to confirm the grid lists the seeded films and that the search box filters them over the interactive connection, and reads the container log for the HTTPS redirection and Data Protection warnings. The exact `docker` commands go into a README section.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add a Dockerfile for this Blazor Web App. Read the .csproj, Program.cs, appsettings.json, appsettings.Development.json, Properties/launchSettings.json, Data/SeedData.cs, the Migrations folder and the Components folder first. Write a multi-stage Dockerfile next to the project file: an SDK stage matching the target framework that restores from the project file alone, publishes in Release and builds an EF Core migrations bundle; a migrate target on the runtime image that contains only the bundle; and an aspnet runtime stage that runs as the non-root user on the image's default port. Add a .dockerignore for bin, obj and .git. Do not change Program.cs: take the connection string from the ConnectionStrings environment variable, and note in the pull request how seeding at startup depends on migrations having run, what UseHttpsRedirection does behind plain HTTP, and where Data Protection keys are stored. Install Docker in the sandbox, start SQL Server from Microsoft's Linux image on a private network, run the migrate target, run the app, request / and /movies, open /movies in the browser and use the search box, and quote the container log. Add the build and run commands to the README and open a pull request with the build output and what each check returned.",
  "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

A pull request with a `Dockerfile`, a `.dockerignore` and a README section; no C# file changes. The description shows the `docker build` output per stage, the migration bundle's output listing the three migrations it applied, the status codes for `/` and `/movies`, a screenshot of the movie grid filtered by the search box, and the lines from the container log that matter. It ends with a short operator's list: supply the connection string through the environment, run the `migrate` target before each deploy, terminate TLS in front of the container, and mount a volume for the Data Protection keys so antiforgery tokens survive a restart.

A reviewer checks the restore layer copies only the project file, that the final image contains neither the SDK nor the `dotnet-ef` tool, and that no password is baked into an image layer or the README. They also decide two things the agent only describes: whether migrations should stay a separate step or move into startup with `Database.Migrate()`, and whether `UseHttpsRedirection` should be paired with forwarded headers for their proxy. Either can be requested as a follow-up to the same session, which wakes the parked sandbox with its disk as the run left it.

## Run it on every new app repository

GitHub signs its own webhook deliveries and cannot send the bearer secret a Rush `webhook` trigger expects, so the per-repository start goes through a small handler you run. It receives the organisation's `repository` event, checks GitHub's signature, waits until the repository is one the Rush GitHub App can reach, and sends this body to `POST /o/{org}/sessions` with the new repository in `resources`.

Send a `request_id` that is a UUID v5 of the repository's id, so a redelivered event returns the run already started. The API key's webhook then reports `run.completed` for each of these runs, with the `session_url` to read the pull request from.

## Why run it on Rush?

An untested Dockerfile for this kind of app fails on first contact with a real environment, and not in the Dockerfile: the process exits because the database is empty, or the connection string still points at a Windows-only instance. Finding that out takes a database server, a network, a migration step and a browser on the page. The agent assembles all of it in its own sandbox, with Docker installed there, and the build log, the container log and the screenshot stay in the run's record.

Interactive server rendering adds a check a plain HTTP probe misses. The page can return 200 while the live connection behind it never opens, so the run drives the page in a browser and types into it. A developer hands this off from the terminal and returns to a pull request that names every assumption the image makes about its surroundings.

## Frequently asked questions

### Does this work for Blazor WebAssembly too?

The same run adapts. A standalone WebAssembly app publishes static files, so the agent writes a final stage that serves `wwwroot` from a web server image, with no .NET runtime in it. A Blazor Web App with a `.Client` project keeps the ASP.NET Core runtime stage, and the publish step builds both projects.

### Why not call Database.Migrate() at startup?

It is a legitimate choice for a single instance, and a risky one when several replicas start together and race to migrate. The run keeps the app's code as it is and ships a migration bundle as its own target. If you prefer startup migration, ask in a follow-up and the agent adds it as a separate commit.

### Do I need sticky sessions behind a load balancer?

With interactive server rendering, yes, once there is more than one replica, because each user's circuit lives in the memory of the instance that created it. The proxy also has to pass WebSocket upgrades. The pull request states both; one container behind one proxy needs neither.

### Can it target PostgreSQL in the container instead?

Not by changing the image alone. The provider, the connection code and the migrations are all specific to SQL Server here. Switching database is its own change, and the Dockerfile from this run works unchanged afterwards because the connection string already comes from the environment.

## More Blazor jobs

- [Set Up GitHub Actions CI for a Blazor Web App](https://getrush.ai/run/add-github-actions-ci/blazor/)
- [Add Health Checks to a Blazor Web App](https://getrush.ai/run/add-health-checks/blazor/)
- [Add i18n to a Blazor Web App](https://getrush.ai/run/add-i18n/blazor/)

## The same job on other stacks

- [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/)
- [Add a Dockerfile to a .NET Web App](https://getrush.ai/run/add-dockerfile/dotnet/)

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