basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Let CI build the image, let the server pull from a registry

    Don’t build Docker images on a production server: docker compose up --build burns RAM and disk space right next to live services, and leaves source code and build keys on prod. Build the image in CI after checks pass, push it to a registry, and have the server pull the finished artifact.

    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 with no space left on device.
    • Source code and build secrets on prod. Running docker build on 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 the image: line and running docker 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.yaml pins the latest tag without a digest: a local image with that name is already on disk, so up -d never checks the registry for a newer build.
    Fix
    Run docker compose pull && docker compose up -d (or docker compose up -d --pull always). For git-driven deploys, pin images by digest with Renovate (Pin images by digest): the image: line changes in a commit, so up -d pulls 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 login was run as a regular user while pull runs as root (for example, from a systemd timer).
    Fix
    Run sudo docker login ghcr.io -u example-user with a classic token carrying read: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 amd64 while the server runs arm64 (or the reverse).
    Fix
    Check the server architecture with uname -m and set the matching platform in build-push-action (platforms: linux/arm64 or linux/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 build runs.
    Fix
    Check the Build Cache row in docker system df and clear it with docker builder prune (or docker builder prune -a for 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.