Files
983da03878 build(docker): add the Compose HTTP service laya-serve (#234)
The image could not serve HTTP at all. `Dockerfile:16` was `pip install . && pip check`,
so the `serve` extra — and therefore the `laya-serve` console script — was not in the
image, and `compose.yaml` only ran the one-shot SDK quickstart. `compose.cuda.yaml:1`
pointed at the gap: it described `compose.http.yaml` as living "on the HTTP preview
branch", and that file has never existed in any commit (`git log --all -- compose.http.yaml`
is empty).

`#45` named this as the follow-up: "A natural follow-up now that #31 has landed: a Compose
service that runs `laya-serve`, so the container speaks the Jev-compatible
`/v1/systemone` out of the box."

```
docker compose -f compose.yaml -f compose.http.yaml up --build laya-serve
curl -s localhost:8000/health
curl -s localhost:8000/v1/systemone -H 'content-type: application/json' \
  --data @examples/docker/request.json
```

End-to-end verified against a real container, not just a config parse:

```
$ docker compose -f compose.yaml -f compose.http.yaml ps
repo-laya-serve-1  Up  0.0.0.0:18000->18000/tcp, [::]:18000->18000/tcp

$ curl -s localhost:18000/health
{"status":"ok","loaded":[],"device":"cpu"}

$ curl -s -o /dev/null -w '%{http_code}' -X POST localhost:18000/v1/systemone -d @examples/docker/request.json
422                       # no weights cached and the container is --network none

no header      HTTP 401
wrong token    HTTP 401
correct token  HTTP 422   # auth passed, then the offline model load fails as designed
```

Deliberate choices tested:

- **The CUDA override repeats its build args and device reservation for `laya-serve`.** A Compose override replaces a service's `build` block whole rather than merging it, so adding `-f compose.cuda.yaml` without repeating them leaves `laya-serve` on CPU while `laya` gets the GPU. Verified both services receive `TORCH_INDEX: cu128`, `LAYA_DEVICE: cuda` and the same `driver: nvidia` reservation.
- **`compose.http.yaml` never touches the `laya` service.** It only adds `laya-serve`, so `docker compose run --rm laya` and `docs/docker.md`'s existing examples behave exactly as before. `tests/test_packaging.py` now asserts that, so a later edit cannot quietly publish a port on the quickstart service.
- **`LAYA_PRELOAD` defaults to `0` here**, against the package default of `1`. Preloading makes the first container boot download all three checkpoints, which is a bad first impression for a quickstart; the docs say to set it to `1` for a long-running deployment.
- **One port variable, not two.** `LAYA_PORT` sets both the published host port and the port `laya-serve` binds (it already reads `LAYA_PORT`), so they cannot drift. The test asserts the two sides of the mapping are the same variable with the same default.
- **`LAYA_API_KEY_FILE` rather than only `LAYA_API_KEY`.** `docker/entrypoint.py` already had the `*_FILE` mechanism for `HF_TOKEN`, so `LAYA_API_KEY` joins `SECRET_NAMES`; the file is read once at startup and the `_FILE` variable is removed before the server execs. This is the first secret the server has, and a bearer token belongs in a file or a Compose secret, not in the environment.
- **`restart: unless-stopped` for the server, and none for the quickstart.** The quickstart is a one-shot command; a server that exits on a transient error should come back.

Known limitations:

- `/health` stays unauthenticated, so it reports `{"status":"ok","loaded":[]}` to anyone. That is the shipped behaviour of `laya.serve` and this PR does not change it; the health probe is what an orchestrator polls.
- The image grows by fastapi and uvicorn for every user of the quickstart, including those who never serve HTTP. The alternative was a second Dockerfile or a build arg, and one image that can do both is what #45 asked for. It is worth ~15 MB.
- I could not run `docker build --pull` to completion here: the daemon resolves `auth.docker.io` over IPv6 and that path times out on this machine, so the build used the cached base image. The `FROM` line and everything after it built and ran, and `docker.yml`'s `--pull` build is the check that covers this.

Also included, both from the same gap:

- `docs/docker.md`'s "HTTP serving" section said serving was "pending #3 or #31" with the server command still to come. It now documents the service, the seven `LAYA_*` server variables, the port override and the token-from-file pattern, and the two new variables are in the main configuration table.
- `tests/test_packaging.py` gains 19 checks over the Compose layout: the service and its command, the port mapping, that the quickstart service is untouched, that the Dockerfile installs `.[serve]` and still runs `pip check`, that the CUDA override covers both services, that no file references a missing Compose file, and that all four Compose files exist.

Verified:

```
$ python tests/test_packaging.py            34 passed, 0 failed    (15 before)
$ python tests/test_criteria.py             72 passed, 0 failed
$ python tests/test_router.py               all routing tests passed
$ python tests/test_email.py                46 passed, 0 failed
$ docker compose config --quiet             OK for all six combinations,
                                            including the new -f compose.http.yaml
$ docker run --rm -v "$PWD:/workspace:ro" -w /workspace repo-laya-serve \
    python tests/test_docker_entrypoint.py  Ran 5 tests ... OK
$ python -m ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
All checks passed!
```

`tests/test_docker_entrypoint.py` fails 1 of 5 on Windows because `os.execvp` rebuilds
the command line without the quoting `subprocess` applied, which is why #212 skips that
suite on its Windows job; it passes on Linux, which is where `ci.yml` and `docker.yml`
run it and where the run above was made.

Co-authored-by: PerryLink <[email protected]>
Co-authored-by: NandhaKishorM <[email protected]>
2026-09-23 19:13:34 +05:30

31 lines
1003 B
Python

"""Load supported file-backed secrets, then replace this process with the command."""
import os
from pathlib import Path
import sys
# Both names are read as `<NAME>_FILE` and then moved into `<NAME>`, so a secret can be
# mounted as a file instead of passed in the environment.
SECRET_NAMES = ("HF_TOKEN", "LAYA_API_KEY")
def main():
for name in SECRET_NAMES:
filename = os.environ.get(name + "_FILE")
if not filename:
continue
try:
value = Path(filename).read_text(encoding="utf-8").strip()
except (OSError, UnicodeError, ValueError):
sys.exit(f"Cannot read {name}_FILE")
if not value or "\0" in value:
sys.exit(f"Invalid empty or NUL-containing secret in {name}_FILE")
os.environ[name] = value
os.environ.pop(name + "_FILE", None)
if len(sys.argv) < 2:
sys.exit("A container command is required")
os.execvp(sys.argv[1], sys.argv[1:])
if __name__ == "__main__":
main()