It is 18:40 on a Thursday, the release branch is frozen at 19:00, and the pipeline is red for the third time. On your MacBook the same docker build and docker run finish in under a minute and the health endpoint answers. In the job log there is one line: exec /usr/local/bin/app: exec format error. Somebody suggests clearing the CI cache. Somebody else suggests retrying. Both are guesses, and you have twenty minutes.

The useful move is to stop asking "what is different about CI" in general and ask two narrower questions in order. First: is CI running the same image you ran? Second: if it is, what about the environment around that image differs? Almost every case of a Docker image that works locally but fails in CI falls cleanly into one of those two buckets, and each bucket has a short list of suspects.

Two buckets explain nearly every local-versus-CI failure

Docker promises that an image behaves the same wherever it runs. That promise is narrower than people remember. It covers the filesystem and metadata frozen inside the image. It does not cover how the image was produced, which CPU architecture it targets, what gets mounted over it, what environment variables are injected, how much memory the kernel grants, or which other containers are reachable on the network.

So split the problem.

Bucket A, a different image. The bytes CI runs are not the bytes you ran. Causes include a different build context, a moving base image tag, a stale local cache that hides a broken step, a build argument that differs, or an image built for a different platform. In this bucket the fix lives in the Dockerfile, the .dockerignore file or the build command.

Bucket B, the same image in a different place. The image is byte-identical, yet it behaves differently because the runtime differs: bind mounts, missing secrets or .env values, memory limits, the user it runs as, service hostnames and readiness timing. Here the fix lives in the pipeline definition or in the assumptions your application makes about its surroundings.

Why insist on the split before touching anything? Because the two buckets call for opposite responses. Clearing caches and pinning tags helps in bucket A and does nothing in bucket B. Adding a healthcheck or a secret helps in bucket B and does nothing in bucket A. Teams that skip the split often apply three fixes at once, the pipeline turns green, and nobody learns which change mattered. The next time it breaks, the same 40 minutes are spent again.

One honest caveat: a small number of failures sit in both buckets at once. A cached layer can bake in an environment variable at build time, for example. Those are rare enough that starting with the split still pays off.

Three terms worth pinning down before you compare anything

The checks below depend on three words that get used loosely in pipeline chat. Getting them precise saves arguments.

Build context: the set of files the Docker client sends to the builder when you run docker build .. Every COPY and ADD instruction can only see files inside it, filtered by .dockerignore. On your laptop the context is your working directory, including untracked and gitignored files. In CI it is a fresh checkout, which contains only what is committed.

Image digest and image ID: two different hashes. The image ID is what docker image inspect IMAGE | jq -r '.[0].Id' prints locally: with the classic image store it is a SHA-256 of the image configuration, and with the containerd image store that newer Docker Desktop installs use, it is the manifest or index digest.

The repository digest is a SHA-256 of the manifest stored in a registry, printed as name@sha256:.... If two image IDs match, the images are the same. On the classic store, a local ID and a registry digest look different even for identical content, which has misled more than one team into chasing a ghost. Compare the same kind of hash on both sides.

Platform: the operating system and CPU architecture an image targets, written as linux/amd64 or linux/arm64. A Mac with an M1, M2, M3 or M4 chip builds linux/arm64 by default. Most hosted CI runners, including the standard ubuntu-latest runners on GitHub Actions and the default shared runners on GitLab.com, are x86_64, which Docker calls linux/amd64. When an arm64 binary lands on an amd64 kernel without emulation, you get exactly the exec format error from the opening scene.

With these three terms, "it works locally" becomes testable. You can ask whether the contexts match, whether the IDs match and whether the platforms match, and each question has a command behind it.

A worked example: one gitignored file and a 186 MB context

Here is an illustrative case, built from a pattern that shows up repeatedly in Python and Node services. The numbers are realistic but invented.

A team ships a FastAPI service. The Dockerfile is twelve lines: a python:3.12-slim base, COPY requirements.txt, pip install, then COPY . . and a CMD. Locally the build log says transferring context: 186.4MB and the container starts in 2 seconds. In GitHub Actions the log says transferring context: 9.1MB, the build succeeds, and the container exits with code 1 after 0.4 seconds: KeyError: 'DATABASE_URL'.

The context size is the first clue. A 177 MB gap means the laptop is sending files CI never sees. Listing the context makes it concrete. Running a throwaway build with COPY . /ctx and RUN find /ctx -maxdepth 2 | sort on both sides showed three extra items locally: a 168 MB .venv folder, a 9 MB data/fixtures dump and a 2 KB config/settings.local.yaml.

The first two were harmless noise. The third one was the bug. The application loaded settings.local.yaml if present and fell back to environment variables otherwise. Locally, the file supplied DATABASE_URL. In CI, the file did not exist because it was gitignored, and the pipeline never set the variable.

The cost arithmetic explains why this matters beyond one evening. The pipeline took 7 minutes per run. The team had retried it 6 times across two days before anyone compared context sizes, so 42 minutes of runner time and roughly 3 engineer-hours of attention went into a 2 KB file.

The fix took 4 lines: add .venv, data/ and config/*.local.* to .dockerignore, and set DATABASE_URL as a job-level environment variable from the CI secret store. After the change the local context dropped to 8.8 MB, close to CI's 9.1 MB, and the remaining 0.3 MB difference turned out to be compiled __pycache__ files, which were added to .dockerignore as well.

The lesson generalises. When local and CI context sizes differ by more than a few percent, you are almost certainly in bucket A, and the difference is a list of files you can print.

Platform and base-image drift: the quiet bucket A causes

Context differences are loud once you look. Two other bucket A causes are quiet, because the Dockerfile and the context can be identical and the image still differs.

The first is platform. On Apple Silicon, docker build produces an arm64 image. If you push that image and CI pulls it to run tests on an amd64 runner, the result depends on whether QEMU emulation is registered on the runner. Without it, binaries fail with exec format error. With it, they run, often several times slower, and some JIT-heavy runtimes behave oddly.

The reverse case also exists now that GitHub offers arm64 Linux runners and many teams use Graviton instances on AWS. Check it directly with docker image inspect IMAGE | jq -r '.[0].Os + "/" + .[0].Architecture' on both sides. The fix is an explicit --platform linux/amd64 on docker buildx build, or a multi-platform build that pushes both variants under one tag.

The second is base-image drift. A tag such as node:20 or python:3.12-slim is a pointer that the maintainers move when they publish patch releases or rebuild for security fixes. Your laptop may hold a copy pulled weeks ago, while the runner pulls today's. Most of the time the newer image is fine.

Sometimes a system library changes, a default user changes, or a package is removed from the slim variant, and a step that worked on the old pointer breaks on the new one. The reverse is also possible: CI on a warm cache uses an old base while you pulled the new one.

Pinning by digest removes the ambiguity: FROM python:3.12-slim@sha256:.... The trade-off is real, because a pinned digest also stops security patches until someone bumps it. Teams that pin usually pair it with an automated update tool such as Dependabot or Renovate, which opens a pull request when the upstream digest moves, so the change is visible and tested instead of silent. If you do not want to pin, at least pass --pull in CI and locally when debugging, so both sides resolve the tag at the same moment.

The cache that lies to you

Docker's layer cache is the most common reason a broken Dockerfile appears to work on a laptop. A layer is reused when its instruction and inputs look unchanged. "Look unchanged" is the problem. For RUN apt-get install -y libpq-dev, the cache key is the instruction text, not the state of the package repositories.

For RUN pip install -r requirements.txt, the key includes the file but not the remote index. If a package was yanked, a mirror changed, or a URL inside a RUN curl now returns 404, your cached layer still exists locally and the build completes in seconds. CI, starting cold or from a different cache, executes the step for real and fails.

The diagnostic is cheap: run docker build --no-cache --pull . locally once. If that local build fails the same way CI does, you have reproduced the bug and can fix it at the source. If it passes, the cache was not the cause and you can move on with one suspect eliminated. Either result is useful, which is why this test belongs early in the sequence.

There is an opposite failure too. Some pipelines restore a remote BuildKit cache from a registry or from the GitHub Actions cache backend (type=gha). A poisoned or outdated cache entry can make CI produce a stale layer that your fresh local build does not. The symptom is a CI image that behaves like an older commit. Comparing image IDs, and inspecting docker history to see which layers were marked as cached, shows it quickly.

A practical rule for the build itself: order instructions from least to most frequently changing, copy dependency manifests before source code, and never rely on the cache for correctness. The cache should make builds faster, not decide whether they work. If a build only succeeds because of a cached layer, it is already broken.

Bucket B: the same image meets a different runtime

Suppose the image IDs match. CI pulled exactly the image you tested, and it still fails. Now the environment is the suspect, and five differences account for most cases.

  • Bind mounts that hide the image. A local docker-compose.yml often mounts the source tree with .:/app for live reload. That mount covers whatever the image contains at /app. If the Dockerfile forgot to copy a directory, the mount quietly supplies it on your laptop, and CI, which runs the image without the mount, cannot find it. The same mount on macOS also exposes a case-insensitive filesystem, so an import of ./Utils that should be ./utils works locally and fails on Linux.
  • Configuration that only exists on your machine. Docker Compose reads a .env file from the project directory automatically. Shell exports, a local credentials helper and files under ~/.aws are also invisible to the runner. Print the environment inside the failing container (env | sort, with secrets masked) and diff it against yours.
  • Resource limits. Hosted runners usually have less memory and fewer cores than a developer laptop. Exit code 137 means the process received SIGKILL, which in containers is very often the out-of-memory killer. A test suite that spawns one worker per core can also behave differently on 2 cores than on 10.
  • User and permissions. A script committed without the executable bit runs fine if you invoked it as sh script.sh locally but fails as an entrypoint. Git on Windows can drop the bit, and git update-index --chmod=+x sets it in the index. Windows checkouts can also add CRLF line endings, so a shebang becomes #!/bin/sh\r and the kernel reports no such file or directory for a file that clearly exists.
  • Networking and timing. In GitLab CI with a docker:dind service, containers you start are published on the docker service host, not on localhost. In GitHub Actions, a job running directly on the runner reaches service containers on localhost with mapped ports, while a job running in a container reaches them by service name. Locally, your database was already warm when tests started. In CI, it may still be initialising, so the first connection is refused.

Each of these leaves a fingerprint: a file not found that exists in your source tree, a missing key, exit 137, a refused connection on the first attempt only. Read the exact error with that list in mind before changing anything.

An eight-question decision tree for the next red pipeline

Work through the questions in order and stop at the first "yes" that matches your error. Each answer points to one fix rather than three.

  1. Does the error say exec format error, or does the job log show an emulation warning about the requested platform? Yes: compare the image platform on both sides and build with an explicit --platform. No: go to 2.
  2. Do the local and CI build logs show context sizes that differ by more than about 5 percent? Yes: list the context on both sides, then fix .dockerignore and anything that depends on uncommitted files. No: go to 3.
  3. Does docker build --no-cache --pull . fail locally the same way CI fails? Yes: the cache was hiding a broken step, so fix that step. No: go to 4.
  4. Do the image IDs differ even though the context and Dockerfile match? Yes: look for differing build arguments, a remote cache restoring stale layers, or base-tag drift; pin the base by digest while testing. No: you are in bucket B, go to 5.
  5. Does the local setup mount source code or config into the container? Yes: run the image locally without mounts, exactly as CI does, and see what is missing. No: go to 6.
  6. Is the failure a missing variable, file or credential? Yes: diff the container environment and add the value through the CI secret store, not a committed file. No: go to 7.
  7. Did the container exit with 137 or slow to a crawl before failing? Yes: measure peak memory with docker stats locally, then cap workers or request a larger runner. No: go to 8.
  8. Does the first connection to a database or service fail while a retry succeeds? Yes: add a healthcheck and wait for it, using depends_on with condition: service_healthy in Compose or a readiness loop in the job. No: capture the full container log and runner metadata, and open the case with that evidence.

Nothing in this sequence is clever, and that is on purpose. Its value is that it forces a measurement before each fix, so the pipeline turns green for a reason you can write in the commit message. For engineers interviewing for DevOps roles, this ordering also makes a strong answer to the familiar "it works on my machine" question, because it shows a method rather than a list of tricks.

Questions people ask when the CI image breaks

Does docker save and docker load prove two images are identical? Only if you compare the image IDs after loading, on machines that use the same image store. Two loads of the same tarball then give the same ID. Comparing a local ID with a registry digest proves nothing on the classic store, because one hashes the config and the other the pushed manifest.

Can I run the CI job on my laptop to reproduce it? Partly. GitLab lets you run a job's image with docker run and the same script by hand, and the open source tool act replays many GitHub Actions workflows locally inside containers. Neither reproduces the hosted runner's kernel, CPU count or network policy, so treat a local pass from these tools as weak evidence.

Why does the failure sometimes disappear on a retry? Retries change timing and sometimes the runner. If a retry passes, suspect a race with a dependent service, a flaky network fetch during the build, or a pool of runners where only some machines have a warm cache or a different architecture. Record the runner name in the job log so you can see the pattern.

Is it safe to push an image built on my Mac to production? It is safe only if it was built for the platform production runs on. Images built on Apple Silicon default to linux/arm64. If your servers are amd64, build with an explicit platform or a multi-platform manifest, and better still let CI build the release image so the artifact comes from a clean, recorded checkout.

There are cases where chasing parity is the wrong goal. Do not try to make CI mirror every quirk of a laptop, with the same mounts, the same .env file and the same warm cache: you would only move the hidden state into the pipeline.

Do not add --no-cache to every CI build as a permanent fix either, because it can multiply build time by five or more and hides nothing you could not catch with one local cold build. And if the failure appeared once in 200 runs and never again, a timeboxed note in the incident log is worth more than a day of digging. Make the laptop look more like CI, not the other way round.