Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 4

Cache a Docker image built in workflow

Solved! Go to Solution.

We're currently building a Docker image every workflow run. This takes 10-15 minutes. It's faster to fetch the same image from Docker hub, but nicer to only be dependent on the Dockerfile in the repo. Is there any way to speed up subsequent builds by doing some kind of Docker build caching?

3 Replies
Solution
GitHub Staff
Message 2 of 4

Re: Cache a Docker image built in workflow

We're adding caching.  It will arrive by mid-November.

 

See also https://github.community/t5/GitHub-Actions/Caching-files-between-GitHub-Action-executions/m-p/30974#...

Ground Controller Lvl 1
Message 3 of 4

Re: Cache a Docker image built in workflow

For those who found this Subject looking for a way to use cache in Workflows, you can use this action that does just what @nergnezor mentioned: fetches the layers from a Registry and use (multiple) --cache-from to hint docker where to look for cache.

 

The action supports multi-stage builds as it pushes each stage to a registry, and pull them when a new build is triggered.

 

This is a minimal example:

 

- uses: whoan/docker-build-with-cache-action@v1
  with:
    username: whoan
    password: "${{ secrets.DOCKER_PASSWORD }}"
    image_name: whoan/node
Pilot Lvl 1
Message 4 of 4

Re: Cache a Docker image built in workflow

Using buildx and ghaction-docker-buildx does not require to set cache or to push to registry:

 

name: buildx

on:
  pull_request:
    branches: master
  push:
    branches: master
    tags:

jobs:
  buildx:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v1
      -
        name: Set up Docker Buildx
        id: buildx
        uses: crazy-max/ghaction-docker-buildx@master
        with:
          version: latest
      -
        name: Available platforms
        run: echo ${{ steps.buildx.outputs.platforms }}
      -
        name: Run Buildx
        run: |
          docker buildx build \
            --platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x \
            --output "type=image,push=false" \
            --file ./test/Dockerfile ./test

 

Live example: https://github.com/crazy-max/swarm-cronjob/runs/231926567