How to build a Docker image in GitHub Actions
docker build command, Dockerfile basics, context path, image tagging, build args, multi-stage builds in CI, build output
Building Images in CI
Building a Docker image in CI ensures your image is always built from clean code, not a developer's local environment. The image becomes a verified, deployable artifact.
Basic Docker Build Workflow
jobs:
build-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: |
docker build \
--tag myapp:${{ github.sha }} \
--tag myapp:latest \
--file Dockerfile \
.github.sha is a built-in context variable containing the full commit SHA. Tagging with it makes every image uniquely traceable to a commit — critical for rollbacks.
Using docker/build-push-action
- uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: false
tags: myapp:${{ github.sha }}The official docker/build-push-action is the recommended approach for production workflows. It supports BuildKit by default, enabling parallel layer builds and better caching. Set push: false to build without pushing — useful in PR checks where you only want to verify the image builds successfully.
