11 KiB
| name | description |
|---|---|
| web-project-scaffold-containerized-python | Scaffold a new web project with a Python (FastAPI) backend and a no-build HTML/CSS/JS frontend, packaged in a Docker container that deploys by pulling source from a remote git repo — `docker exec <name> update` fetches the latest commit, syncs requirements, restarts the app in place and rolls back on failure, with no image rebuild. Servers are set up by one copy-paste install command (curl … | bash) that installs Docker, clones the repo, writes the .env with repo URL and credentials, and starts the container. Requires an existing remote git repo URL up front. Use when the user wants to start/create/bootstrap a new web app, site, API or internal tool that should be Python-based, containerized, or easy to deploy and update on a server. |
Scaffold a containerized Python web project
Goal: hand back a project where scripts/verify.sh prints VERIFY: PASS before any feature work. The run proves the image builds, the container clones from the repo named in .env, the tests pass, and update deploys, refuses broken code and rolls back without a rebuild. A passing run is the contract. Don't build features on a skeleton that hasn't passed.
Principles (why the skeleton looks the way it does)
-
Nothing is compiled. The backend is Python (FastAPI + uvicorn). The frontend is plain HTML, CSS and ES modules served from
app/static/, with no bundler, TypeScript, Sass, JSX or npm. Third-party browser libraries are vendored intoapp/static/vendor/as files with the pinned version in a comment. A deploy is therefore just new files. -
Containerized. The host needs only Docker and git. Python, the venv and every tool run in the container, for dev and prod alike.
-
The image is the runtime, not the app. The image holds Python, git, tini and three scripts in
container/. The source is a git checkout in the/srvvolume, cloned fromREPO_URLon first start. A rebuild is needed only whencontainer/or the Python version changes. -
Update is one command.
/usr/local/bin/updateis on PATH, sodocker exec <name> updateworks. It reads.env, fetchesREPO_REFfromREPO_URL, checks out, reinstalls requirements if they changed, checks thatapp.mainimports, sends SIGHUP to the in-container supervisor (which restarts uvicorn), and waits for/healthzto report the new commit. Any failure restores the previous commit.--check,--rollbackand<ref>cover the rest. -
Remote and credentials live in
.env. It is gitignored and mounted read-only at/run/config/app.env. The container parses it (it doesn't execute it) on every start and every update, so a new URL or rotated token needs no rebuild or recreate.- Variables:
REPO_URL,REPO_REF,GIT_TOKEN/GIT_USERNAME(https) orGIT_SSH_KEY(base64 deploy key), plus any app settings. - Token: a git credential helper supplies it from the environment. It never goes into
.git/configor the URL. - Isolation: the uvicorn process gets the app settings but not the git credentials.
- Variables:
-
Install is one pasted line. The project ships
install.sh. On a fresh Linux server, the user pastescurl -fsSL <raw install.sh URL> | bash, Homebrew-style, and the installer does the rest:- installs git and Docker (Engine + Compose) if they're missing
- checks it can read the repo, asking for a token on the terminal if the https repo is private
- clones into
/opt/<name> - writes
.envfrom.env.examplewith the repo URL, branch, credentials and port, owned by uid 1000 with mode 640 - builds the image, starts the container and waits for
/healthz
Re-running it is safe, and is also how the image gets rebuilt: it keeps
.envand replaces only the keys passed in. Everything is insidemain(), so a partial download runs nothing. It uses sudo only for steps that need it.
Step 0 — Get the remote repo URL first (required)
Servers install from and update from the remote. The install command is that repo's raw install.sh URL, and the URL is the default REPO_URL baked into install.sh and .env.example. So don't scaffold until the user has given the remote git repo URL. If they haven't, ask for it before anything else. Ask whether it is private: over https it needs a read-only token, and over SSH a deploy key.
- Have them create an empty repo first if it doesn't exist (GitHub, GitLab, Gitea, …).
- GitHub and GitLab raw URLs are derived. For any other host, ask for the raw URL of
install.shon the default branch and pass--install-url. - The URL must not contain credentials. Those go only in the server's
.env, through the installer.
Step 1 — Check the environment (never install silently)
bash "$SKILL_DIR/scripts/doctor.sh"
It checks git, docker, compose v2, buildx and daemon access. If it exits non-zero, show the user its fix lines and stop. Let them run the installs, suggesting the ! <command> prefix. Don't scaffold blind.
Step 2 — Decide the few inputs
--name: a lowercase slug. It becomes the container name (sodocker exec <name> update), the image name and the compose project.--title: the display name.--python: default 3.14. Use the user's version if they give one.--port: the host port on 127.0.0.1, default 8000.--branch: defaultmain.--repo-url(required, from Step 0): the remote the servers clone from. It goes intoinstall.sh,.env.exampleand the localorigin.--install-url: only for hosts other than GitHub and GitLab.
Ask only for what you can't default. The name is usually obvious from the request.
Step 3 — Generate
bash "$SKILL_DIR/scripts/scaffold.sh" --dir <project-dir> --name <slug> --repo-url <url> [--title "…"] [--python 3.14] [--port 8000] [--branch main] [--install-url <url>]
- Pins: it resolves current
fastapi,uvicorn[standard],pytestandhttpx2versions in a throwawaypython:<ver>-slimcontainer and pins them==X.Y.*. Use--no-resolvewhen offline. - Templates and tokens: it copies
templates/project. If theweb-frontendskill is installed, it copies that skill'stokens.cssintoapp/static/css/. .envand git: it writes a local.env(mode 600) for dev, runsgit initand addsorigin, then prints the install command. It doesn't commit, push, build or start anything.
Resulting layout (details in the generated README.md):
app/main.py, app/api.py FastAPI: /healthz {status, version}, /api/*, /static (no-cache), /
app/static/ index.html, css/tokens.css + app.css, js/main.js + api.js, vendor/
tests/test_app.py pytest + TestClient
install.sh server installer: curl … | bash (Docker, clone, .env, build, start, health)
container/ Dockerfile, lib.sh, entrypoint.sh (setup + supervisor), update.sh
compose.yml prod: volumes runtime:/srv + data:/data, ./.env mounted, loopback port, read-only rootfs
compose.dev.yml dev overlay: source bind-mounted, DEV=1 → dev deps + --reload, update disabled
.env.example / .env REPO_URL, REPO_REF, GIT_TOKEN/GIT_USERNAME, GIT_SSH_KEY, HOST_PORT, app settings
Step 4 — Verify the contract
bash "$SKILL_DIR/scripts/verify.sh" <project-dir> # add --keep to leave containers for debugging
It works on a throwaway copy with a local bare repo as the "remote" and a dummy token in its .env. Your project, its .env and its git history are untouched. It checks:
- the build and a healthy start
- the app sees
.envsettings but notGIT_TOKEN, and the token is never written to disk - pytest passes
updatedeploys a commit that changes requirements, with the same image and the same container start time- an import-breaking commit is refused and a startup-crashing commit is rolled back, with the old commit still serving
update --rollbackinstall.shpiped into bash, as on a fresh server, against agit://remote: it brings the app up healthy and reachable on the host port, writes.env(uid 1000, mode 640) and never calls sudo when it isn't needed; a re-run is safe and replaces only the keys passed in
It doesn't exercise the Docker-install branch, because Docker is already present. That branch was tested on a bare Debian image: git, ssh, Docker and Compose all installed.
It must end with VERIFY: PASS. If it fails, fix the template or the project until it passes, then report the versions used.
Then tell the user the flow:
- Push the project to the remote. Offer to make the first commit and push; don't do either unless they ask. The install command only works once
install.shis on the remote branch. - Install: paste the printed command on the server. For a private https repo, use the
read -rsp … && curl -H …form from the README, so the token is never echoed or saved in shell history. - Deploy:
git push, thendocker exec <name> updateon the server. - Rebuild (only when
container/changes): run the install command again.
Building on the skeleton
- UI: follow the
web-frontendskill for the look (tokens, layout, components), within the no-build rule: plain CSS on the tokens, ES modules, vendored libraries, no CDN. Web fonts are vendored intostatic/vendor/fonts/or left on the system stack. - API: routes go in
app/api.py, or anapp/api/package as it grows, under/api. Use Pydantic models for request and response bodies. - Persistence: keep it under
$DATA_DIR(/data, its own volume). SQLite is the default for small apps. Create the schema and run migrations idempotently inlifespan, never at import time, becauseupdateimportsapp.mainas its pre-flight check. - Dependencies: add each one to
requirements.txtwith a==X.Y.*pin.updateinstalls it on the next deploy. Test-only dependencies go inrequirements-dev.txt. - Settings and secrets: new ones go in
.envand.env.example, which holds names and placeholders only. Read them withos.environin the app. - System packages (e.g.
ffmpeg,libpq) mean acontainer/Dockerfilechange, and so a rebuild. Prefer pure-Python wheels. When a system package is needed, say a rebuild is required.
Guardrails
- Every "it works" claim comes from a
verify.shrun that printedVERIFY: PASS, never from reading the files. - No build step, ever. Don't add
package.json, a bundler, TypeScript or a CSS preprocessor. If the user needs one, they are asking for a different project shape; say so. - Never bake app source into the image.
.dockerignorelets in onlycontainer/. Copyingapp/into the image would makeupdateand the image disagree. - No remote, no scaffold. Get the repo URL first (Step 0). Don't invent a placeholder URL to fill in later.
- The server is set up only through
install.sh. Keep it idempotent and insidemain(). When a project needs more server setup (a system package, a firewall rule, a volume path), add it to the installer rather than to manual README steps. - Never commit
.env, tokens or keys. Don't put credentials inREPO_URL. Don't print.envvalues. Name variables, never their values. - Keep the scripts in
container/generic. Project-specific logic belongs in the app (lifespan), not inupdate.sh, which can only change with a rebuild. - Used ⇒ declared: every import maps to a pinned line in
requirements*.txt, and every vendored file has its version noted.