How can I use my "gha" Docker cache to speed up Docker pull, as well as Docker build on Github Actions?
Asked Answered
S

2

18

On Github Actions, I'd like to avoid having to pull my newly built Docker image from the registry when I have it in a cache (and this is the slowest part of my jobs)

My workflow is something like

  1. Build an image (with all my dependencies baked in)
  2. Run a command within the above image

As per the Docker Build Push Action docs, setting up the cache-to and cache-from to point to gha has helped speed up step 1 a lot.

However, when I run docker run ghcr.io/org/image:new-tag command, it always starts with

Unable to find image 'ghcr.io/org/image:new-tag' locally
new-tag: Pulling from org/image
...
5402d6c1eb0a: Pulling fs layer
...

which takes around a 50 seconds (of around a total job time of ~75 seconds).

This seems unnecessary when there's a cache sat within reach that contains this information, however I don't know how to tell my docker run command how to make use of this cache as, as far as I can see, there's no --cache-from=gha equivalent option for docker run.

How can I tell docker to look in the gha cache for an image when I call docker run on Github Actions?

Stalkinghorse answered 23/10, 2021 at 11:51 Comment(0)
M
4

We faced similar situation some time back but we recently found a github-actions which actually helps in caching the docker-layers & images b/w subsequent runs.

I am sure that your problem can also be solved with it. Here is the link to the gh-action https://github.com/satackey/action-docker-layer-caching.

Configuration Example You can add the following lines above the docker run step to ensure caching to be done in gha

    - uses: satackey/[email protected]
      continue-on-error: true
Marutani answered 26/10, 2021 at 20:43 Comment(1)
This turned out to be slower than my current solution (pull from cache). Nice idea though.Stalkinghorse
B
-1

However, you lose all the advantages of caching once you start building your images on a virtual machine that gets destroyed after every run.

Please refer this tutorial will help you. https://evilmartians.com/chronicles/build-images-on-github-actions-with-docker-layer-caching

Brynn answered 2/11, 2021 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.