Docs/Build from a pushed image
#

Build from a pushed image

Build your app's image in your own CI and push it to Cofactor with no repository access, then push a fresh one every night.

Set up sandboxes from a pushed image

You push your app to Cofactor with its cofactor.sandbox.yaml: either the Dockerfile and its build context, which Cofactor builds for you, or an image you built yourself. Each push becomes a version that anyone can claim as a live copy (a sandbox, also called a test environment).

Use this page if you can't connect GitHub. An application takes pushed images or a GitHub repository. The first push makes it a pushed-image application. The examples use an invented app, Project Tracker, served on port 8080.

Push your first image

  1. Choose or write a Dockerfile. Cofactor builds it for you, so you don't need Docker. Pick one:

    • The app's own Dockerfile, if it meets the image rules below.
    • A new Dockerfile, if the app has none.
    • A separate sandbox Dockerfile, such as sandbox/Dockerfile, when the production one doesn't fit. Use it to package more or different things (sandbox scripts, seed tooling, test dependencies) or to layer the image differently. Push it with --file, and add --target <stage> to stop at one stage of a multi-stage build.

    Whichever you pick, the image must be linux/amd64 and glibc-based with bash, it needs its own WORKDIR such as /app, and it must not end on a non-root USER. Run cofactor sandbox guide upload for the full rules.

  2. Plan before you build.

    bash
    cofactor sandbox guide plan
    
  3. Write cofactor.sandbox.yaml. It matches the GitHub version without setup, because the image is already built. lifecycle.build is optional and runs only on cofactor sandbox sync --build.

    yaml
    version: 1
    resources: { vcpus: 2, memory_mib: 4096 }
    ports:
      web: { port: 8080, access: public }
    params:
      web_url: { port: web }
    env:
      PUBLIC_URL: ${params.web_url}
    services:
      app:
        run: exec ./start.sh
        health: { http: 8080, path: /health }
        reload_on_prepare: true
    personas:
      owner: { email: owner@example.test }
    lifecycle:
      prepare:
        - bash sandbox/personas.sh
    environments:
      default:
        description: One owner login and no data yet.
    

    Ship your sandbox scripts in the image. Seed data and logins covers personas, actions and seeds.

  4. Check the file.

    bash
    cofactor sandbox validate --upload cofactor.sandbox.yaml
    
  5. Push. From the directory with your Dockerfile, Cofactor builds it for you.

    bash
    cofactor sandbox push --application "Project Tracker" \
      --definition cofactor.sandbox.yaml --tag nightly --wait
    

    This uploads the files in the current directory that Cofactor doesn't have yet (.dockerignore applies) and shows the build log. Use --context <dir>, --file <Dockerfile> and --target <stage> if your layout differs. For a sandbox Dockerfile in a subfolder, add --file sandbox/Dockerfile. It must sit inside the context.

    Already build the image yourself? Push it instead. Cofactor receives the built image and not your build context:

    bash
    docker buildx build --platform linux/amd64 -t tracker:sandbox --load .
    cofactor sandbox push --application "Project Tracker" tracker:sandbox \
      --definition cofactor.sandbox.yaml --tag nightly --wait
    

    Either way, push uploads only what Cofactor doesn't have. With --wait it follows the import until every service is healthy.

    The push output: layers uploaded, the image imported, then "ready [nightly, 2026-11-25]" and the claim command.
  6. Claim and prove it.

    bash
    cofactor sandbox claim --application "Project Tracker" --wait
    cofactor sandbox verify-env --application "Project Tracker"
    cofactor sandbox destroy <sandbox id>
    

Push a new image every night

  1. Create an API key in your own shell. The secret prints once, so keep it out of chat.

    bash
    cofactor api-key create --name "CI image push" --scope sandbox:read --scope sandbox:publish --scope property:read
    
  2. Store it as the repository secret COFACTOR_API_TOKEN.

  3. Save this workflow as .github/workflows/cofactor-sandbox-nightly.yml. Replace Acme Tracker with your application's name.

    yaml
    name: Cofactor sandbox nightly
    on:
      schedule:
        - cron: "0 6 * * *"
      workflow_dispatch:
    permissions:
      contents: read
    jobs:
      push:
        runs-on: ubuntu-latest
        timeout-minutes: 60
        steps:
          - uses: actions/checkout@v7
          - uses: docker/setup-buildx-action@v4
          - uses: docker/build-push-action@v7
            with:
              context: .
              platforms: linux/amd64
              load: true
              push: false
              tags: app:sandbox
              cache-from: type=gha
              cache-to: type=gha,mode=max
          - run: npm install -g @runcofactor/cli
          - env:
              COFACTOR_API_TOKEN: ${{ secrets.COFACTOR_API_TOKEN }}
            run: |
              cofactor sandbox validate --upload cofactor.sandbox.yaml
              cofactor sandbox push --application "Acme Tracker" app:sandbox \
                --definition cofactor.sandbox.yaml --tag nightly --wait
    
  4. Run it once from the Actions tab, or with gh workflow run cofactor-sandbox-nightly.yml.

Check it worked

The push ends with Push <id>: ready [nightly]. cofactor sandbox envs --application "Project Tracker" shows the version. After the first nightly run, claim last night's build with --at nightly. The claimed URL serves the app and each login signs in.

Performance

Cofactor boots your image when you push. The first claim of an environment runs lifecycle.prepare and the seeds and saves a snapshot of the running sandbox. Later claims are copies of that snapshot. These practices follow from that.

Keep pushes small

  • Add a .dockerignore. A push hashes every file the ignore file keeps and uploads the new ones, and refuses a context over 100,000 entries or 8 GiB. An unchanged context reuses the finished build, so keep out what the image doesn't use, such as .git, test output and docs. A <Dockerfile>.dockerignore beside a sandbox Dockerfile wins over the context's .dockerignore.
  • Put what rarely changes first, if you build the image yourself. A pushed image's layers are named by the digest of their bytes, and push uploads only the ones Cofactor doesn't hold. Order system packages, then the dependency manifest and install, then your source. A Dockerfile push sends changed files in any order, and Cofactor's own build keeps no layer cache between builds. If your install steps are slow, build in CI with the cache and push the image.
  • Keep the build cache in CI. A runner with an empty cache rebuilds every step, and every rebuilt layer is new to Cofactor. See Build cache under Advanced.

Keep claims fast

  • Build everything into the image. Nothing builds at claim, so install dependencies, compile assets and install any database server in the Dockerfile.
  • Create data in seeds, not in prepare. Seeds run once per version and environment, before the snapshot, and every copy shares the result. prepare runs for the snapshot and again for each copy, so keep it to logins and a few quick steps.
  • Skip the per-claim prepare with live port params. When every port param is live: true, a copy only receives its URLs: no prepare, no restart. Your app reads them at request time from /var/lib/cofactor/live-env.json. Run cofactor sandbox guide definition for the rules.
  • Make services start fast and answer health early. Without live params, each copy restarts the services marked reload_on_prepare and waits for them to be healthy, checking every 200 ms for up to 5 minutes. A cheap /health that answers as soon as the app is ready keeps each claim short. Mark only the services that read per-sandbox values.
  • Warm on disk, not in memory. Compile, migrate and fill on-disk caches in the Dockerfile or in a seed, and they are in every copy. A service that restarts on each claim starts with cold memory.
  • Size resources to the app. Every sandbox of the application gets that machine (1 to 32 vCPUs, 1,024 to 65,536 MiB). A size the fleet can't place fails the claim.

Keep the image lean

  • Use a multi-stage build. Compile in a build stage and copy only the output into the final stage, so tools stay out of the image. Each layer can be up to 5 GB and an image up to 128 layers.
  • Use --target for variants. One Dockerfile can hold the production stage and a sandbox stage that adds your scripts and seed tooling. Push the sandbox stage with --target sandbox.

Go deeper

Advanced

Image rules, API key scopes, pull request tags and common errors.

Image rules. ENTRYPOINT and CMD are not used, so start each process with a services.<name>.run command. Everything runs as uid 1000. Run cofactor sandbox guide upload for the rest.

Tags. A push takes up to 10 tags. A tag names the newest ready push that carries it, so pushing --tag nightly again moves it. A claim without --at uses the newest ready push tagged main or nightly, or the newest ready push if none carries either tag.

Pull requests. An image gets no automatic PR preview. Push each pull request with its own tag, like --tag pr-123, and claim it with --at pr-123.

API key scopes.

The jobScopes
Pushes imagessandbox:read, sandbox:publish, property:read
Creates the application on first pushadd property:create
Claims and destroys a sandboxadd sandbox:create, sandbox:delete
Runs actionsadd sandbox:run
Runs a suite on the new pushadd test-run:run, test-run:read

Build cache. Use docker/build-push-action with type=gha. The cache works only from that action.

Errors. If push or a claim fails, the message names the cause and ends with the failing service's log or the script's stderr. Run cofactor upgrade if it says the server takes pushes from a newer CLI.