
# How your image is built

Every `agc ship` builds one image from the snapshot, and the rehearsal and production run that exact image, by digest. A root `Dockerfile` is used as is; without one, Agent Cloud generates one for Node or a uv (Python) project. Success is the ship passing `building` and ending `"outcome": "live"`.

## Steps

Usually there's nothing to do: ship, and the image is built.

```bash
agc ship
```

To build from a Dockerfile that isn't at the root:

```toml
[service.web]
build = "deploy/web/Dockerfile"   # a file: the repo root is the build context
# build = "deploy/web"            # a folder: its Dockerfile, with the folder as the context
```

A worker always runs from the web service's image. Leave `build` out of `[service.worker]`, or set it to the same value.

## How it works

**Which Dockerfile.** In order:

1. `[service.web] build`, when it names a file or a folder. A file must be named like a Dockerfile: `Dockerfile`, `Dockerfile.<x>`, `<x>.Dockerfile` or `Containerfile`. `"."` (or no key) means the default.
2. A root `Dockerfile`.
3. A uv project (root `pyproject.toml` with `[project]` dependencies): the Python default. See [Python apps](/docs/build/python-apps.md).
4. A pnpm app (a root `pnpm-lock.yaml`, and `packageManager` choosing pnpm when `package-lock.json` is there too): the pnpm default.
5. Anything else: the Node default.

**The Node default image.**

```dockerfile
FROM node:24.22.1-slim@sha256:…
RUN apt-get update -qq && … apt-get install … ca-certificates …
WORKDIR /app
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
COPY package*.json ./
RUN if [ -f package-lock.json ]; then npm ci; elif [ -f package.json ]; then npm install; fi
COPY . .
RUN npm run build --if-present && npm prune --omit=dev
ENV NODE_ENV=production
USER 1000:1000
CMD <your command, or agc-start>
```

**The pnpm default image.** The same base, with one pnpm fetched from the npm registry and checked against its integrity, then the whole app copied in:

```dockerfile
RUN pnpm install --frozen-lockfile …
RUN pnpm run --if-present build && pnpm prune --prod …
CMD <your command, or agc-start, which runs pnpm start>
```

That pnpm is `packageManager`'s exact version (checked against its hash, if it has one), else an exact `engines.pnpm`, else Agent Cloud's for the lockfile's `lockfileVersion` (`9.0`, `6.1`, `6.0`, `5.4`). No other pnpm version takes over during the build. `agc ship` refuses before building when both lockfiles are there and `packageManager` doesn't choose (`deps.two_lockfiles`) or the lockfile version is unknown (`deps.pnpm_lockfile_unknown`).

**PID 1 and signals.** A shell as PID 1 ignores SIGTERM, so a release would kill your app mid-request after the grace period. Agent Cloud avoids that two ways:

- With `[service.web] command` (and for every worker), the command runs inside `sh -c` with a trap that forwards TERM and INT to it and exits with its status.
- Without `command`, the image installs a launcher, `agc-start`. If `scripts.start` is plain `node <file>` (optionally after `NAME=value` assignments) and there's no `prestart` or `poststart`, the launcher stays PID 1 and runs the command. It forwards TERM, INT and HUP, and sets `npm_package_name` and `npm_package_version` as npm would. Any other `start` runs as `npm start`.

Web pods get 30 seconds between SIGTERM and SIGKILL; workers get 60.

**File modes.** Files arrive with whatever modes your machine gave them, and some agent sandboxes write owner-only files. The build rewrites every file to 0644 (0755 if anyone could execute it) and every folder to 0755, so the non-root user can read the app.

**BuildKit.** Every build runs with `DOCKER_BUILDKIT=1`: Cloud Build on Agent Cloud's own cloud, CodeBuild on an AWS target. `RUN --mount`, `COPY --chmod` and other BuildKit-only syntax work the same on both.

**Assets** declared with `[[asset]]` are unpacked at their paths before `docker build`, so your Dockerfile `COPY`s them like any file. See [Large build inputs](/docs/build/what-goes-into-a-snapshot.md#large-build-inputs-assets).

**Your own Dockerfile, on a private target.** Targets run apps under Pod Security "restricted": the image must run as a non-root user given by number. `USER node` or no `USER` fails to start with cause `image_user`, so end the Dockerfile with `USER 1000:1000`. In the default images the app's files belong to root, so user 1000 can't write inside `/app` (a crash with cause `write_denied`). Write to `/tmp` or the database.

### Why was my ship refused with `ship.image_user_root`?

Apps run only as a non-root user, checked before the build:

- `agc doctor`'s `build` check reads the final stage's last `USER` (a root builder stage doesn't count). `USER root` or `USER 0` fails. No `USER`, or a named user (`USER app`), warns: most public base images, JRE and JDK ones among them, run as root. `USER 1000:1000` passes. `agc init --bring` warns the same in `data.notes`.
- `agc ship` refuses a final stage with `USER root` or `USER 0` with `ship.image_user_root` (exit 2) before anything is uploaded or built. No `USER`, or a named user, ships with the warning in `data.warnings`.
- Once built, Agent Cloud reads the image's real user, base image default included. On a platform target, root ends the ship with `ship.image_user_root` before any rehearsal or release.

The fix: end the final stage with `USER 1000:1000`, a numeric non-root user that can read the app's files, or remove the Dockerfile to use Agent Cloud's image. Then `agc ship` again.

**Stuck builds.** Two control-side rules keep one build from holding an app:

- A ship whose operation is still `building` 30 minutes on fails with `ship.build_timeout`, and frees the app's ship lock.
- A newer ship with a different snapshot replaces a build that has been `building` for more than 10 minutes, ending it with `ship.superseded`. A younger build makes the newer ship answer `ship.locked`.

**The mirror doesn't use the image.** `agc up` runs your `dev` command natively on your machine. The image's first real start is in the rehearsal, which runs it before anyone is asked to approve.

## Reference

| `[service.web] build` value | Dockerfile | Context |
|---|---|---|
| absent, `"."`, `"./"` | root `Dockerfile`, else generated | repo root |
| `"deploy/web/Dockerfile"` | that file | repo root |
| `"deploy/web"` | `deploy/web/Dockerfile` | `deploy/web` |

## Errors

| code | exit | cause | fix |
|---|---|---|---|
| `ship.bad_build` | 1 | `build` isn't a quoted path, names no path, isn't a plain path inside the app, or names a file that isn't a Dockerfile | Point it at a Dockerfile or a folder with one, relative to `agentcloud.toml` |
| `ship.build_missing` | 1 | `build` names no Dockerfile in this snapshot | Commit the Dockerfile, or remove `build` |
| `ship.worker_build` | 1 | `[service.worker] build` differs from `[service.web] build` | Remove it from the worker, or make them equal |
| `ship.build_timeout` | 1 | The build didn't finish within 30 minutes | Ship again; keep dependencies and build output out of the source |
| `ship.superseded` | 1 | A newer ship replaced this build after 10 minutes | Nothing; the newer release builds |
| `ship.locked` | 1 | Another release of the app is in progress | `agc wait` on it, or ship again later |
| `deps.two_lockfiles` | 2 | `pnpm-lock.yaml` and `package-lock.json` both ship, and `packageManager` doesn't choose | Add `"packageManager": "pnpm@<version>"` (or `npm@`), or delete the unused lockfile |
| `deps.pnpm_lockfile_unknown` | 1 | A pnpm lockfile version Agent Cloud has no pnpm for, and no pinned pnpm | Regenerate the lock with a known pnpm, or pin one in `packageManager` |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "ship",
  "error": {
    "retryable": false,
    "code": "ship.build_missing",
    "message": "agentcloud.toml's [service.web] build = \"deploy/web\" names no Dockerfile in this snapshot.",
    "fix": "Point build at a Dockerfile that is committed, or a folder with a Dockerfile in it, or remove build to use the root Dockerfile or the detected default, then ship again."
  },
  "next": [{ "cmd": "agc status", "why": "see the current state before retrying" }]
}
```

## Limits

| What | Limit |
|---|---|
| Image build on Cloud Build or CodeBuild | 15 min |
| Operation in `building` before `ship.build_timeout` | 30 min |
| Age of a build a newer ship may replace | over 10 min |
| Source unpacked for a build | 320 MiB ("the source unpacks to more than 320 MiB") |
| `build` path | 200 characters; letters, digits, `.`, `_`, `-` per segment |

## Agent notes

- `npm prune --omit=dev` (`pnpm prune --prod`) runs after `build`, so packages the server imports at run time must be in `dependencies`.
- A Dockerfile at the root always wins over detection. Delete it if you want the default image.
- The build context follows the root `.dockerignore`. A `.dockerignore` that drops build output your Dockerfile needs fails inside the build, not at ship.
- Secrets reach the app as environment variables when it runs, in production and its rehearsal, not during the build, so read them at start-up rather than baking them into the image.
- Cloud Build and CodeBuild stop an image build at 15 minutes; the 30-minute `ship.build_timeout` counts from the operation's last update and catches a build that never reports.

## Related

- [What goes into a snapshot](/docs/build/what-goes-into-a-snapshot.md)
- [Python apps](/docs/build/python-apps.md)
- [Large build inputs](/docs/build/what-goes-into-a-snapshot.md#large-build-inputs-assets)
- [agc ship, end to end](/docs/ship/agc-ship-end-to-end.md)
- [Sizing and capacity](/docs/targets/sizing-and-capacity.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/how-your-image-is-built: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
