---
title: Introduction to GitHub Actions with Docker
linkTitle: GitHub Actions and Docker
summary: |
Learn how to automate image build and push with GitHub Actions.
params:
tags: [devops]
time: 10 minutes
---
This guide provides an introduction to building CI pipelines using Docker and
GitHub Actions. You will learn how to use Docker's official GitHub Actions to
build your application as a Docker image and push it to Docker Hub. By the end
of the guide, you'll have a simple, functional GitHub Actions configuration for
Docker builds. Use it as-is, or extend it further to fit your needs.
## Prerequisites
If you want to follow along with the guide, ensure you have the following:
- A Docker account.
- Familiarity with Dockerfiles.
This guide assumes basic knowledge of Docker concepts but provides explanations
for using Docker in GitHub Actions workflows.
## Get the sample app
This guide is project-agnostic and assumes you have an application with a
Dockerfile.
If you need a sample project to follow along, you can use [this sample
application](https://github.com/dvdksn/rpg-name-generator.git), which includes
a Dockerfile for building a containerized version of the app. Alternatively,
use your own GitHub project or create a new repository from the template.
{{% dockerfile.inline %}}
{{ $data := resources.GetRemote "https://raw.githubusercontent.com/dvdksn/rpg-name-generator/refs/heads/main/Dockerfile" }}
```dockerfile {collapse=true}
{{ $data.Content }}
```
{{% /dockerfile.inline %}}
## Configure your GitHub repository
The workflow in this guide pushes the image you build to Docker Hub. To do
that, you must authenticate with your Docker credentials (username and access
token) as part of the GitHub Actions workflow.
For instructions on how to create a Docker access token, see
[Create and manage access tokens](/manuals/security/for-developers/access-tokens.md).
Once you have your Docker credentials ready, add the credentials to your GitHub
repository so you can use them in GitHub Actions:
1. Open your repository's **Settings**.
2. Under **Security**, go to **Secrets and variables > Actions**.
3. Under **Secrets**, create a new repository secret named `DOCKER_PASSWORD`,
containing your Docker access token.
4. Next, under **Variables**, create a `DOCKER_USERNAME` repository variable
containing your Docker Hub username.
## Set up your GitHub Actions workflow
GitHub Actions workflows define a series of steps to automate tasks, such as
building and pushing Docker images, in response to triggers like commits or
pull requests. In this guide, the workflow focuses on automating Docker builds
and testing, ensuring your containerized application works correctly before
publishing it.
Create a file named `docker-ci.yml` in the `.github/workflows/` directory of
your repository. Start with the basic workflow configuration:
```yaml
name: Build and Push Docker Image
on:
push:
branches:
- main
pull_request:
```
This configuration runs the workflow on pushes to the main branch and on pull
requests. By including both triggers, you can ensure that the image builds
correctly for a pull request before it's merged.
## Extract metadata for tags and annotations
For the first step in your workflow, use the `docker/metadata-action` to
generate metadata for your image. This action extracts information about your
Git repository, such as branch names and commit SHAs, and generates image
metadata such as tags and annotations.
Add the following YAML to your workflow file:
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Extract Docker image metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ vars.DOCKER_USERNAME }}/my-image
```
These steps prepare metadata to tag and annotate your images during the build
and push process.
- The **Checkout** step clones the Git repository.
- The **Extract Docker image metadata** step extracts Git metadata and