Skip
Arish's avatar

12. Building Docker Images in CI


Basic Docker Build

yaml
1jobs:
2  build:
3    runs-on: ubuntu-latest
4    
5    steps:
6      - uses: actions/checkout@v4
7      
8      - name: Build image
9        run: docker build -t myapp:${{ github.sha }} .

With Docker Buildx

yaml
1- name: Set up Docker Buildx
2  uses: docker/setup-buildx-action@v3
3
4- name: Build and push
5  uses: docker/build-push-action@v5
6  with:
7    context: .
8    push: false
9    tags: myapp:${{ github.sha }}
10    cache-from: type=gha
11    cache-to: type=gha,mode=max

Push to Registry

yaml
1jobs:
2  build:
3    runs-on: ubuntu-latest
4    
5    steps:
6      - uses: actions/checkout@v4
7      
8      - name: Login to GHCR
9        uses: docker/login-action@v3
10        with:
11          registry: ghcr.io
12          username: ${{ github.actor }}
13          password: ${{ secrets.GITHUB_TOKEN }}
14      
15      - name: Build and push
16        uses: docker/build-push-action@v5
17        with:
18          context: .
19          push: true
20          tags: |
21            ghcr.io/${{ github.repository }}:${{ github.sha }}
22            ghcr.io/${{ github.repository }}:latest

Multi-Platform Builds

yaml
1- name: Build multi-platform
2  uses: docker/build-push-action@v5
3  with:
4    platforms: linux/amd64,linux/arm64
5    push: true
6    tags: ghcr.io/${{ github.repository }}:latest