Cache a Docker image built in workflow

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?

8 Likes

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#M630

19 Likes

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
2 Likes

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

name:buildxon:
pull\_request:
branches:masterpush:
branches:mastertags:

jobs:
buildx:
runs-on:ubuntu-lateststeps:
      -
name:Checkoutuses:actions/checkout@v1 -
name:Set up Docker Buildxid:buildxuses:crazy-max/ghaction-docker-buildx@masterwith:
version:latest -
name:Available platformsrun:echo ${{ steps.buildx.outputs.platforms }} -
name:Run Buildxrun:| 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

1 Like

@ethomson Given https://github.com/actions/cache is on, does it apply to the original question? (Caching docker layers)

Or is something different is coming for Docker image layer caching?

6 Likes

Also looking for an answer to this; it is mid-November

9 Likes

It’s already mid of Feb 2020, any update on this?

7 Likes

How is it cached then?

2 Likes

Hey everyone, I found a solution that works pretty well. Unfortunately doesn’t FULLY cache, so you have to do some data transfer but seems to get the job done.

I run docker save to create a compressed image, save it as an artifact, and then load it in the next step. Not sure how this will scale but it will work for some use-cases. Still would be much better to have the ability to keep the same docker registry between steps

jobs:
create-docker-image:
runs-on: ubuntu-latest
steps:
-uses: actions/checkout@master
-name: build image
run: docker build -t foo:bar .
-name: save image
run: docker save -o ./image foo:bar
-name: Upload image
uses: actions/upload-artifact@v1
with:
name: airflow-image
path: image
pull-image:
needs: create-docker-image
runs-on: ubuntu-latest
steps:
-name: Download image
uses: actions/download-artifact@v1
with:
name: airflow-image
-name: load image
run: docker load -i ./airflow-image/image
-name: run image
run: docker run foo:bar
8 Likes

Its mid-May 2020. This still doesn’t exist and no news has been shared about when it might.

2 Likes

@ethomson Hi, im wondering if maybe theres an update on this yet? Built in docker layer caching would be great

3 Likes