Script Valley
CI/CD with GitHub Actions
Docker and Container WorkflowsLesson 3.1

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

Docker build flow 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.

Up next

How to push Docker images to GitHub Container Registry

Sign in to track progress

How to build a Docker image in GitHub Actions โ€” Docker and Container Workflows โ€” CI/CD with GitHub Actions โ€” Script Valley โ€” Script Valley