0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker in GitHub Actions: Simple Guide

To use docker in GitHub Actions, add steps in your workflow YAML file that run docker commands inside a job with runs-on set to a Linux runner. Use the actions/checkout action to get your code, then run docker build and docker run commands as needed.
📐

Syntax

In GitHub Actions, you define jobs and steps in a YAML file. To use Docker, you typically:

  • Set runs-on to a Linux runner that has Docker installed.
  • Use actions/checkout to get your repository code.
  • Run docker commands inside run steps.

Example syntax for a step running Docker commands:

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t my-image:latest .
      - name: Run Docker container
        run: docker run --rm my-image:latest
💻

Example

This example workflow builds a Docker image from your repository's Dockerfile and runs a container that prints a message.

yaml
name: Docker Build and Run

on: [push]

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t hello-world-image .
      - name: Run Docker container
        run: docker run --rm hello-world-image
Output
Sending build context to Docker daemon 2.048kB Step 1/2 : FROM alpine ---> a24bb4013296 Step 2/2 : CMD echo "Hello from Docker in GitHub Actions!" ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789xyz123uvw Successfully built 789xyz123uvw Successfully tagged hello-world-image:latest Hello from Docker in GitHub Actions!
⚠️

Common Pitfalls

Common mistakes when using Docker in GitHub Actions include:

  • Not using a Linux runner (ubuntu-latest) because Windows or Mac runners may not have Docker installed.
  • Forgetting to check out the repository before building the Docker image.
  • Not tagging the Docker image properly, causing confusion or overwriting.
  • Running Docker commands without --rm on containers, which can leave stopped containers consuming space.
yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Build without checkout (wrong)
        run: docker build -t my-image .

# Correct way:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t my-image:latest .
📊

Quick Reference

Tips for using Docker in GitHub Actions:

  • Always use ubuntu-latest runner for Docker support.
  • Use actions/checkout@v3 before Docker commands.
  • Tag images clearly with versions or latest.
  • Use docker run --rm to clean up containers automatically.
  • Use multi-stage Dockerfiles to keep images small.

Key Takeaways

Use a Linux runner like ubuntu-latest to run Docker commands in GitHub Actions.
Always check out your code before building Docker images with actions/checkout.
Tag Docker images clearly to avoid confusion and overwriting.
Use --rm flag with docker run to automatically remove containers after execution.
Test your Docker commands locally before adding them to your workflow.