Files
test-assignment/docs/deployment.md
T

6.1 KiB

Production containers on the VPS

This is deployment step 2: build and run the app behind the existing host Nginx. The containers listen only on the VPS loopback interface. The public domain will work after step 3 installs its certificate and host Nginx virtual host. Gitea Actions automation is a separate step; these scripts also support manual deployment.

Prerequisites

Use the Linux VPS with Docker Engine, Docker Compose v2 supporting up --wait, Git, OpenSSL, flock (util-linux), and ss (iproute2). Run application commands as a deployment user that can use Docker. Docker group membership grants administrative access to the host. Node and pnpm are installed inside the images; they are not required on the VPS.

Commit and push the new deployment files from the development machine before cloning/pulling on the VPS. Use the existing Gitea repository credentials when prompted; don't embed a token in a remote URL.

First installation

Run these commands on the VPS as the intended deployment user. The install command needs sudo and gives that user ownership of the application directory:

sudo install -d -m 750 -o "$(id -un)" -g "$(id -gn)" /opt/merchantflow
git clone https://git.prvt.you/gitadmin/test-assignment.git /opt/merchantflow/app
cd /opt/merchantflow/app
bash deploy/init-env.sh
bash deploy/start.sh --seed-demo

If already cloned, use the existing checkout and pull the committed deployment changes instead of cloning again. The scripts locate the checkout themselves.

init-env.sh generates a random 64-character hexadecimal database password and writes /opt/merchantflow/production.env with mode 600. The same password is used in POSTGRES_PASSWORD and DATABASE_URL. It refuses to overwrite an existing file. Never commit this file or put it inside the source checkout. Hexadecimal passwords need no URL escaping. deploy/production.env.example documents the configuration without containing a real secret.

The API uses NODE_ENV=production, API_HOST=0.0.0.0, port 3001, and APP_ORIGIN=https://merchantflow.prvt.you. Production Compose fixes the first three values; its origin and database credentials come from the explicit env file. Local development retains the loopback API bind default. Do not export a development DATABASE_URL, APP_ORIGIN, or POSTGRES_PASSWORD in the deployment shell: shell variables take precedence over Compose's env file.

start.sh serializes deployments with a lock, checks host ports, builds both images before changing services, waits for PostgreSQL, writes a pg_dump -Fc backup, and applies checked-in migrations using prisma migrate deploy. The explicit --seed-demo option then populates an empty user table with the demo accounts and fictional records. It refuses to reseed an existing populated database. API and frontend start only after these commands succeed.

There are no public PostgreSQL ports. The project has its own persistent merchantflow_database volume, separate from the local development database. Container logs rotate at 10 MB with three files per service. API runs as the unprivileged Node user. Its image retains the workspace dependency layout and Prisma CLI so migrations don't require installing tools at deployment time. The web image contains only Nginx configuration and built static assets.

Verify on the VPS

bash deploy/compose.sh ps
curl --fail http://127.0.0.1:13001/api/health
curl --fail -I http://127.0.0.1:18080/
curl --fail -I http://127.0.0.1:18080/login

All three containers should be healthy. The API health response is {"status":"ok","database":"connected"}. Frontend routes return the SPA on refresh. Hashed /assets/ files get immutable caching; HTML requires revalidation, and missing assets return 404. /api paths on the frontend container deliberately return 404: host Nginx must forward those paths directly to the API, preserving the /api prefix.

The next step's host Nginx upstreams are:

Route Upstream
/ http://127.0.0.1:18080
/api and /api/* http://127.0.0.1:13001

Browser sign-in should be checked at the final HTTPS origin after step 3. The current production origin check and secure session cookies intentionally don't support signing in via plain HTTP on the loopback ports.

Subsequent updates

cd /opt/merchantflow/app
git pull --ff-only origin main
bash deploy/start.sh

Omit --seed-demo on updates. Migrations run without resetting data or changing existing demo credentials. Backups go to /opt/merchantflow/backups; copy these off-server and manage retention separately. The script aborts on build, backup, or migration failure. A failed container health check reports an error and leaves logs available; this step does not implement automatic application or database rollback. Brief downtime while containers restart is expected.

For commit-tagged images, set APP_IMAGE_TAG consistently on commands:

export APP_IMAGE_TAG=$(git rev-parse HEAD)
bash deploy/start.sh

The default image tag is local. The later Actions workflow can supply its commit SHA instead. MERCHANTFLOW_ENV_FILE overrides the env-file path and MERCHANTFLOW_PROJECT overrides the Compose project name, primarily for isolated verification. Keep the production project name stable to reuse its database volume. Host port mappings remain fixed.

Operations

bash deploy/compose.sh logs --tail=100 api web db
bash deploy/compose.sh restart api web
bash deploy/compose.sh stop
bash deploy/compose.sh up -d --wait

Do not run down -v: that deletes the database volume. Changing POSTGRES_PASSWORD in the env file does not rotate the password in an existing PostgreSQL volume; a rotation must update the database role and DATABASE_URL together. Do not use the development pnpm db:setup command on this server.

The Compose configuration waits for database readiness using a health check; ordinary container startup order alone doesn't establish readiness. Docker startup-order documentation