# Container services

Source: https://docs.apphaven.eu/services/container

A `container` service runs your code: either an image AppHaven **builds from
your repository**, or a prebuilt image it pulls from a registry.

```yaml
services:
  web:
    type: container
    image: ghcr.io/example/app:1.4.2    # or use `build` (below) instead
    build:                    # build from source instead of pulling an image
      dockerfile: Dockerfile  # optional, default "Dockerfile"
      context: .              # optional, default repo root "."
      args:                   # optional, docker build args
        KEY: value
    ports:                    # optional, list of ints, 1–65535
      - 3000
    env:                      # optional, map of string -> string
      KEY: value
    volumes:                  # optional, list of mounts
      - name: <volume_name>
        mount_path: /abs/path
    healthcheck:              # optional, lets other services wait for "healthy"
      test: ["CMD-SHELL", "curl -fsS http://localhost:3000/ || exit 1"]
      interval: 10s
    depends_on:               # optional, start ordering
      - service: db
        condition: healthy
```

| Field         | Type                           | Required    | Rules                                                                      |
| ------------- | ------------------------------ | ----------- | --------------------------------------------------------------------------- |
| `image`       | string                         | conditional | Required unless `build` is set. Any image reference that can be pulled. Use a [specific tag or digest](#prebuilt-images-image). |
| `build`       | object                         | conditional | Build from source; see [below](#building-from-source-build). A container service declares `image` or `build`. |
| `ports`       | list of int                    | no          | Ports your service listens on. Each must be in the range `1–65535`, at most 4. Each port gets its own address; see [Ports and addresses](https://docs.apphaven.eu/manifest.md#ports-and-addresses). |
| `env`         | map&lt;string, string&gt;      | no          | Environment variables for the container. See [Environment variables](https://docs.apphaven.eu/environment-variables.md). |
| `volumes`     | list of `{name, mount_path}`   | no          | Storage mounts. See [Volumes](https://docs.apphaven.eu/volumes.md).                                    |
| `healthcheck` | object                         | no          | See [Healthchecks](#healthchecks).                                          |
| `depends_on`  | list of `{service, condition}` | no          | Start ordering. See [Start order](https://docs.apphaven.eu/services.md#start-order-depends_on).        |

## Prebuilt images (`image`)

Point `image` at a specific version, such as `ghcr.io/example/app:1.4.2`, or
at a digest (`ghcr.io/example/app@sha256:…`). The reference in your manifest
is what a [restore](https://docs.apphaven.eu/restore.md#prebuilt-images-name-the-exact-version) runs
again, so a specific version brings back exactly the image you had. A moving
tag such as `latest` brings whatever it points to at that moment.

## Building from source (`build`)

Give a service a `build` block and AppHaven builds its image from your
repository on every deploy, at the exact commit you deploy. No registry or
build pipeline needed. The build log appears in the console's **Builds** tab.

| Field        | Type                      | Required | Rules                                                                  |
| ------------ | ------------------------- | -------- | ----------------------------------------------------------------------- |
| `dockerfile` | string                    | no       | Path to the Dockerfile, relative to `context`. Default `Dockerfile`.    |
| `context`    | string                    | no       | Build context directory, relative to the repo root. Default `.`.        |
| `args`       | map&lt;string, string&gt; | no       | Docker build args.                                                      |

A monorepo can build several services from one repository by giving each its
own `context`:

```yaml
services:
  api:
    type: container
    build:
      context: api
    ports:
      - 8080
  frontend:
    type: container
    build:
      context: frontend
    ports:
      - 3000
```

Both services declare a port, so each gets its own address and its own
access setting. Leave `ports` off a service that only other services call.

If a service sets both `build` and `image`, the image name is applied to the
build result.

## Healthchecks

A container service may declare a healthcheck, so other services can wait for
it with `depends_on: condition: healthy` and the platform can report its
readiness.

```yaml
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:3000/ || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 20s
```

| Field          | Type           | Required | Rules                                                                          |
| -------------- | -------------- | -------- | ------------------------------------------------------------------------------ |
| `test`         | list of string | yes      | `["CMD", ...]` (exec form) or `["CMD-SHELL", "<shell command>"]`. The command runs inside your container. |
| `interval`     | duration       | no       | Time between checks. Default `10s`. Between `1s` and `5m`.                     |
| `timeout`      | duration       | no       | Per-check timeout. Default `3s`. Between `1s` and `5m`.                        |
| `retries`      | int            | no       | Consecutive failures before the service counts as unhealthy. Default `3`. Between `1` and `20`. |
| `start_period` | duration       | no       | Grace period before failures count. Default `0s`. Up to `5m`.                  |

A duration is a positive whole number followed by `s` or `m`, for example
`10s` or `5m` (`start_period` also accepts `0s`).

## Where to go next

- [Database](https://docs.apphaven.eu/services/postgres.md): give your container a managed database.
- [Volumes](https://docs.apphaven.eu/volumes.md): durable storage for uploads, caches, and data.
- [Environment variables](https://docs.apphaven.eu/environment-variables.md): configuration and secrets.
