4.8 KiB
TITLE
A FastAPI backend serving a plain HTML/CSS/JS frontend, in one container. Nothing is compiled: no bundler, no TypeScript, no Sass. The image holds only the runtime. The source lives in a git checkout inside a volume, so a deploy is one command and needs no image rebuild.
Layout
app/main.py FastAPI app: /healthz, /api, static files, index page
app/api.py JSON API routes (under /api)
app/static/ the frontend, served as is (vendor libraries into static/vendor/)
tests/ pytest
container/ Dockerfile, entrypoint (setup + supervisor), update script
compose.yml production
compose.dev.yml development overlay (bind-mounted source, auto reload)
requirements.txt runtime dependencies (pinned); requirements-dev.txt for tests
Develop
The host needs only Docker and git.
docker compose -f compose.yml -f compose.dev.yml up --build # http://127.0.0.1:__PORT__
docker compose -f compose.yml -f compose.dev.yml run --rm app pytest
Python changes reload on save; frontend changes show on the next page load.
Install on a server
On a fresh Linux server (Debian, Ubuntu, Fedora, RHEL and others), paste one line:
# public repo
curl -fsSL __INSTALL_URL__ | bash
# private repo: asks for a read-only token without echoing it
read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "__TOKEN_HEADER__$GIT_TOKEN" __INSTALL_URL__ | bash
install.sh does the whole setup:
- installs git and Docker (Engine and Compose) if they're missing
- checks it can read
__REPO_URL__ - clones it into
/opt/__NAME__ - writes
/opt/__NAME__/.env(the repo URL, branch, credentials and port thatupdateuses) - builds the image, starts the container and waits until it is healthy
Settings such as INSTALL_DIR, REPO_REF, HOST_PORT, GIT_TOKEN and GIT_SSH_KEY can be passed as environment variables; the top of install.sh lists them. For an SSH-only private repo, copy install.sh to the server and run GIT_SSH_KEY="$(base64 -w0 deploy_key)" bash install.sh.
Re-running the installer is safe. It keeps .env, replacing only the keys you pass, then updates the checkout, rebuilds and restarts. Re-run it whenever container/ changes, or to rotate a credential.
After that, every deploy is:
git push # from your machine
docker exec __NAME__ update # on the server
update fetches, checks out the new commit, installs the requirements if they changed, checks that app.main imports, restarts the app process and waits until /healthz reports the new commit. If any step fails it goes back to the previous commit and exits non-zero. Other forms:
docker exec __NAME__ update --check # what an update would bring in
docker exec __NAME__ update v1.4.0 # a tag, branch or commit
docker exec __NAME__ update --rollback # back to the commit before the last update
The app restarts in place, so there is a second or two of downtime while it restarts. The container itself keeps running.
A rebuild is needed only when container/ or the Python version changes. For that, re-run the install command, or run docker compose up -d --build in /opt/__NAME__. update prints a reminder when an update touches container/.
The .env file
.env is gitignored and stays on the machine. It is mounted read-only into the container, which reads it at startup and again on every update. So a changed URL, branch or rotated token takes effect on the next update, with no rebuild or recreate.
| Variable | Meaning |
|---|---|
REPO_URL |
The remote the container pulls from (https or SSH). Keep credentials out of the URL. |
REPO_REF |
The branch (or tag) that update follows. |
GIT_TOKEN, GIT_USERNAME |
Private repo over https: a read-only token. A git credential helper supplies it, so it is never written into the checkout. |
GIT_SSH_KEY |
Private repo over SSH: a read-only deploy key, base64-encoded on one line (base64 -w0 deploy_key). |
| anything else | App settings. The app sees them as environment variables; it never sees the git credentials. |
install.sh writes .env owned by uid 1000 (the container's user) with mode 640. If you set up by hand, do the same, and create .env before the first docker compose up: if it is missing, Docker creates an empty directory in its place, and the container stops with an error saying so.
Data and config
- Persistent app data goes under
$DATA_DIR(/data, thedatavolume). Back up that volume. - Configuration and secrets live in
.env, never in the repo. - Do startup work (creating tables, migrations) in
lifespaninapp/main.py, and make it idempotent. Don't do it at import time:updateimports the app as a check before it restarts it.