🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Deploy a Docker Image

A Docker image packs an app with the runtime it needs to run. A workflow can build that image and push it to a registry, which is a store for images.

The Dockerfile for the demo

This file starts from a small Node image, copies the app in, and runs it.

FROM node:24-alpine
WORKDIR /app
COPY app.js .
CMD ["node", "app.js"]

Sign in to the registry

The login step signs in to ghcr.io with the automatic token.

- uses: docker/login-action@v4
  with:
    registry: ghcr.io
    username: ${{ github.actor }}
    password: ${{ secrets.GITHUB_TOKEN }}

Build and push the image

This step builds the image from the Dockerfile and pushes it with the latest tag.

- uses: docker/build-push-action@v7
  with:
    context: .
    push: true
    tags: ghcr.io/sanjay51/a1learner-actions-demo:latest

Example

name: Deploy a Docker image
on:
  push:
    branches: [main]
permissions:
  contents: read
  packages: write
jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v7
        with:
          context: .
          push: true
          tags: ghcr.io/sanjay51/a1learner-actions-demo:latest

Key points

  • Use a Docker image to ship an app with its runtime in one packed copy.
  • A Dockerfile is a short text file that tells Docker how to build the image.
  • The demo image is a toy, built from node:24-alpine plus app.js.
  • The workflow signs in to the registry before it pushes the image.
  • The image name is ghcr.io/sanjay51/a1learner-actions-demo.
  • The workflow needs the packages write permission to push to the registry.

In this example, the job checks out the code and sets up Node 24, then runs the tests. It signs in to ghcr.io, the GitHub Container Registry, with the automatic token. The build step builds the toy image from the Dockerfile at the repo root, using node:24-alpine plus app.js. It pushes the image with the latest tag. The permissions block gives the token packages write, so the push is allowed.

Note

The Dockerfile lives at the root of the demo repo. The coordinator adds it at verification time, before this workflow runs.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which permission does the workflow need to push an image to ghcr.io?