Deployment
Deploy with Docker
Package Next.js, FastAPI, and Postgres as three Docker Compose services, clone the repo on the VPS with a deploy key, and run it on the server's IP.
By the end of this page the app runs on your server. Anyone can open a signing link, and the admin page asks for your password.
| Service | What | Port |
|---|---|---|
web | Next.js, the admin page, the signing page, and the PDF route | 80, published |
api | FastAPI | none |
db | Postgres | none |
Only Next.js is published. FastAPI and Postgres are reachable only on the Compose network, so the browser can only get to them through Next.js.
Write the backend image
FROM python:3.13-slim
COPY --from=ghcr.io/astral-sh/uv:0.12.23 /uv /bin/
ENV PYTHONUNBUFFERED=1 UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=never
WORKDIR /srv
COPY pyproject.toml uv.lock ./
RUN uv sync --locked --no-install-project
COPY app ./app
CMD [".venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"].venv
__pycache__
evalsThe uv image is pinned, and uv sync --locked installs exactly the versions in uv.lock. PYTHONUNBUFFERED makes log lines show up right away in docker compose logs. The evals stay on your laptop.
Write the frontend image
FROM node:24-slim AS build
WORKDIR /app
RUN corepack enable
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile
COPY . .
# Rewrites are fixed at build time: /api goes to the api service.
ENV API_URL=http://api:8000
RUN pnpm build
FROM node:24-slim
WORKDIR /app
ENV NODE_ENV=production HOSTNAME=0.0.0.0 PORT=3000
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/public ./public
CMD ["node", "server.js"]node_modules
.nextThe first stage installs and builds. The second stage copies only the standalone server, the static files, and public/ with the stylesheet and the fonts, so the image has no node_modules.
Rewrites are fixed at build time. That is why the Dockerfile sets API_URL=http://api:8000 before pnpm build, so /api goes to the api service on the Compose network.
Replace frontend/next.config.ts:
import type { NextConfig } from "next";
// Local runs read the project .env, like the backend. In Docker, Compose sets the variables.
try { process.loadEnvFile("../.env"); } catch {}
const config: NextConfig = {
output: "standalone",
serverExternalPackages: ["@react-pdf/renderer"],
// The browser talks only to Next.js. Other /api calls go on to FastAPI.
rewrites: async () => [
{ source: "/api/:path*", destination: `${process.env.API_URL || "http://localhost:8000"}/api/:path*` },
],
};
export default config;output: "standalone" makes next build write a small server.js with only the files it needs.
Write the Compose file
Replace docker-compose.yml. The highlighted lines are new.
# Three services, one network. Only the Next.js app is published.
services:
web:
build: ./frontend
environment:
ADMIN_PASSWORD: ${ADMIN_PASSWORD}
RENDER_TOKEN: ${RENDER_TOKEN}
ports:
- "80:3000"
depends_on:
- api
restart: unless-stopped
api:
build: ./backend
env_file: .env
environment:
DATABASE_URL: postgresql+psycopg://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
RENDER_URL: http://web:3000/api/internal/render
BASE_URL: ${BASE_URL:-http://localhost} # Set BASE_URL in .env on the server.
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 5s
retries: 10
restart: unless-stopped
volumes:
postgres:api reads your .env, and the environment: entries override values from it. Inside Compose, the database is the db service and the PDF route is on the web service, not on localhost. web only needs the admin password and the render token.
BASE_URL is the start of the signing links in the emails. Without a BASE_URL in .env, Compose uses http://localhost, which is where the full stack runs on your laptop. On the server you set it to the server's address.
The hPanel firewall is the one that counts
Docker writes its own firewall rules for published ports, and they bypass ufw on the server. Only publish what must be public, and control access in the hPanel firewall.
Run the full stack locally
Stop pnpm dev and the API, so only Docker runs. From the project root:
docker compose up -d --buildOpen http://localhost, log in, and process and send a contract. The signing link in the email starts with http://localhost, the same as the link in the admin page.
Push the repo to GitHub
Create a private repository named contract-drafting on GitHub, without a README. Then point your clone at it and push:
git add .
git commit -m "Contract drafting"
git branch -M main
git remote set-url origin git@github.com:<your-github-user>/contract-drafting.git
git push -u origin mainRun git status first and make sure .env is not listed.
Add a deploy key
A deploy key gives the server read access to this one repository and nothing else. On the server:
ssh-keygen -t ed25519 -f ~/.ssh/github-deploy -N "" -C "contracts-vps"
cat >> ~/.ssh/config <<'EOF'
Host github.com
IdentityFile ~/.ssh/github-deploy
IdentitiesOnly yes
EOF
cat ~/.ssh/github-deploy.pubOn GitHub, open the repository, then Settings, Deploy keys, Add deploy key. Paste the public key, name it contracts-vps, leave Allow write access unchecked, and save.
Clone the repo
cd ~
git clone git@github.com:<your-github-user>/contract-drafting.git
cd ~/contract-draftingType yes when SSH asks about GitHub's fingerprint. GitHub publishes its fingerprints in its docs if you want to compare.
The clone includes docker-compose.dev.yml, but Compose only reads it when you name it with -f. On the server you never do, so Postgres gets no port.
Create the environment file
The .env file never goes through Git. On the server, start from the example and fill it in:
cp .env.example .env
chmod 600 .env
nano .envchmod 600 makes the file readable only by deploy, because it holds your secrets. nano is a simple editor that runs in the terminal.
Error opening terminal
Some terminals, such as Ghostty, use a name the server does not know. Run export TERM=xterm-256color and then nano .env again. In Ghostty you can fix it for good. Add shell-integration-features = ssh-env to the Ghostty config and restart Ghostty.
Fill in every value with the same value as in the .env on your laptop, with one change. Find the line # BASE_URL=http://<server-ip>, remove the #, and put in the address of the server, so the links in the emails work:
BASE_URL=http://<VPS_IPV4>Leave DATABASE_URL as it is. Compose sets the right one for the api service.
| Key | What it does |
|---|---|
| Arrow keys | Move the cursor |
Cmd+V (macOS) or right-click (Windows) | Paste |
Ctrl+K | Delete the whole line |
Ctrl+O, then Enter | Save |
Ctrl+X | Close the editor |
Check that no value is empty:
grep -c '=$' .env
# 0Without Gmail, the four email values stay empty, and the emails show up in the API log.
Start it
On the server:
docker compose up -d --build
docker compose psThe first build takes a few minutes. All three services show the status Up, and only web lists a port, 80->3000/tcp.
permission denied while trying to connect to the docker API
deploy is not in the docker group, or you are still in an SSH session from before you added it. Group changes only apply to new logins. Fix it as root, then log in again as deploy. On your laptop:
ssh -o IdentitiesOnly=yes -i ~/.ssh/hostinger-contracts root@<VPS_IPV4>On the server:
usermod -aG sudo,docker deploy
id deploy
exitid deploy must list sudo and docker. Then log in as deploy again:
ssh -o IdentitiesOnly=yes -i ~/.ssh/hostinger-contracts deploy@<VPS_IPV4>Run the commands above again.
Follow the API's logs:
docker compose logs -f apiPlain HTTP
The admin password and the signing data travel unencrypted over plain HTTP. That is fine for this demo. For real contracts, put HTTPS in front with a domain.
Check
Run the whole user story from outside:
- Open
http://<VPS_IPV4>and log in withadminand yourADMIN_PASSWORD. - Pick sample
01, put your own email address in the notes, and click Process. - When it is Drafted, click Send.
- Open the email on your phone. The link starts with
http://<VPS_IPV4>/sign/. - Consent, initial every page, sign, and Finish.
- The signed PDF arrives in your inbox.
From your laptop, http://<VPS_IPV4>:8000 does not load. That is correct.
Ship an update
After you push a change to GitHub:
cd ~/contract-drafting
git pull
docker compose up -d --buildCompose rebuilds the images and restarts the services. The contracts and the signed PDFs live in the Postgres volume, so a restart loses nothing. A contract that was processing during the restart shows as failed, and Retry finishes it.