on the server: git pull ──▶ docker compose up --build (RAM, cache, source on prod)
via a registry: git push ──▶ CI: checks + build ──▶ registry (GHCR) ──▶ server: pull + up -d
Why you shouldn’t build on a production server
- RAM and the OOM killer. A Node frontend build or dependency compilation takes 700 MB to several gigabytes of RAM. On a 2–4 GB VPS, that pushes live databases and containers into swap or triggers an OOM kill. In CI, the build runs on an ephemeral runner and never touches the production host.
- Disk space. Every build leaves behind BuildKit cache, intermediate stages, and old layers. Without periodic cleanup (
docker builder prune), they fill the partition and services fail withno space left on device. - Source code and build secrets on prod. Running
docker buildon the host means keeping the source repo, compilers, and private package tokens there. A finished multi-stage image (Multi-stage build) holds only the binary or static files with nginx, while build secrets stay inside the CI step (Build secret via --secret). - Checks and fast rollbacks. CI runs tests, type checks, and linters before publishing — broken code never reaches the registry (GitHub Actions to GHCR). Each build gets a commit tag (
sha-a1b2c3d) and its own digest: if a release breaks the service, rolling back takes changing theimage:line and runningdocker compose up -d, with no rebuild.
In docker-compose.yaml: image: instead of build:
In production /opt/<stack>/docker-compose.yaml, drop the build: section and point to the finished image in the registry.
Before — the server builds the image itself from local source:
services:
site:
build: ./src
restart: unless-stopped
After — the server only pulls the built image:
services:
site:
image: ghcr.io/example-user/site:latest
restart: unless-stopped
For manual updates, run docker compose pull && docker compose up -d: up -d alone won’t contact the registry if a local latest image is already cached. When stacks deploy from git on a timer (Deploy without SSH), Renovate appends the digest to the tag (ghcr.io/example-user/site:latest@sha256:…) and opens a pull request for each new build — the image: line changes in the commit, and up -d pulls the new image automatically (Pin images by digest).
For step-by-step GitHub Actions setup, latest and sha-<commit> tags, BuildKit caching, and server login to private GHCR with a read:packages token, see GitHub Actions to GHCR.
When local build: makes sense
On your workstation during development, docker compose up --build is faster than waiting for CI on every edit. Building on your own hardware also fits a dedicated build host (self-hosted runner) or an isolated test box with no other services, when free CI minutes run short or you don’t need a registry.
If it doesn’t work
docker compose up -d finished without errors, but the site stayed on the old version even though CI built a new image
- Cause
docker-compose.yamlpins thelatesttag without a digest: a local image with that name is already on disk, soup -dnever checks the registry for a newer build.- Fix
- Run
docker compose pull && docker compose up -d(ordocker compose up -d --pull always). For git-driven deploys, pin images by digest with Renovate (Pin images by digest): theimage:line changes in a commit, soup -dpulls the new image on its own.
docker compose pull returns denied or unauthorized even though the GHCR package built fine
- Cause
- A private package requires registry login on the server, or
docker loginwas run as a regular user whilepullruns asroot(for example, from a systemd timer). - Fix
- Run
sudo docker login ghcr.io -u example-userwith a classic token carryingread:packages(GitHub Actions to GHCR), or switch a public package’s visibility to Public in Package settings.
The image downloaded, but the container crashes on startup with exec format error
- Cause
- CI built the image for
amd64while the server runsarm64(or the reverse). - Fix
- Check the server architecture with
uname -mand set the matching platform inbuild-push-action(platforms: linux/arm64orlinux/amd64,linux/arm64).
Removed build: from Compose, yet server disk space didn’t come back
- Cause
- Docker still holds the BuildKit build cache and intermediate layers from earlier
docker buildruns. - Fix
- Check the
Build Cacherow indocker system dfand clear it withdocker builder prune(ordocker builder prune -afor a full wipe).
How to check
No production Compose file on the server contains a build: section:
grep -rn '^\s*build:' /opt/*/docker-compose.yaml /opt/*/compose.yaml 2>/dev/null
The command returns nothing.
The running image came from a registry — its RepoDigests field is populated:
docker image inspect ghcr.io/example-user/site:latest --format '{{index .RepoDigests 0}}'
# ghcr.io/example-user/site@sha256:...
An empty RepoDigests means the image was built locally (docker build). If images were previously built on the server, check the Build Cache row in docker system df: after docker builder prune, its size drops to 0B.