Sub topic 13
Testing the image before you publish
This chapter assumes you can write a Dockerfile and a basic GitHub Actions workflow. It also assumes you know what a tag is and how docker/build-push-action works.
Why test the built image
Unit tests check your source code. They do not check the image. The image can still fail because a file is missing from the final stage, NODE_ENV=production changes behavior, or the non-root USER cannot read the build output. These problems appear only when you run the image itself. Run it in CI before anything is pushed.
The working model
Buildx builds in a separate BuildKit builder. By default, the result stays in the builder's cache and is not visible to docker run. Setting load: true copies the single-platform result into the runner's local Docker image store. You can then run it, test it, and scan it.
The usual pattern is to build and load, test, and then build again with push: true. The second build is fast because it reuses the cache from the first.
A smoke test in the workflow
A smoke test starts the container and checks that it responds. This example assumes the app listens on port 3000 and answers GET /health. Adjust both to match your app.
name: test-and-publish
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build image for testing
uses: docker/build-push-action@v6
with:
context: .
load: true
push: false
tags: example-app:test
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Smoke test
run: |
docker run -d --name app -p 3000:3000 example-app:test
for i in $(seq 1 20); do
if curl -fsS http://localhost:3000/health; then
exit 0
fi
sleep 1
done
docker logs app
exit 1
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push image
if: github.event_name != 'pull_request'
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=ghaSteps run in order, and a failing step stops the job. The login and push steps never run if the smoke test fails. The loop retries for up to 20 seconds, because the server needs time to start. Printing docker logs app on failure shows why the container did not come up.
Other checks follow the same shape:
- Run
docker run --rm example-app:test node --versionto confirm the runtime exists. - Use
docker run --rm example-app:test id -uto confirm the process is not root. - Run your test suite inside the image, if it contains the test tools.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `docker run` reports the image is not found | The build did not use `load: true` | Add `load: true` |
| Load fails with a multi-platform build | The local image store holds one platform at a time | Test one platform, then run the multi-platform build to push |
| The test passes, but the pushed image differs | The test image and pushed image were built from different inputs | Build both from the same commit and cache |
| The test fails intermittently | The check runs before the server is ready | Poll with a timeout, as above |
Exercise
Your workflow builds with platforms: linux/amd64,linux/arm64 and load: true, and the build fails. What is the cause, and how do you fix the workflow?
Show answer
The local Docker image store cannot hold a multi-platform result, so load: true cannot be combined with several platforms. Build and load a single platform (for example linux/amd64) for the test step. Then run the multi-platform build with push: true as the final step, reusing the cache.