# GitHub Action Docker image cache

**URL:** <https://community.fly.io/t/github-action-docker-image-cache/311>\
**Category:** Phoenix\
**Created:** [October 17, 2020, 6:41am UTC](https://community.fly.io/t/github-action-docker-image-cache/311 "2020-10-17T06:41:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jared/32/46_2.png) [@jared](https://community.fly.io/u/jared)\
**Post date:** [October 17, 2020, 6:41am UTC](https://community.fly.io/t/github-action-docker-image-cache/311/1 "2020-10-17T06:41:37Z")

</div>

Is there a way to cache the image that’s built by Docker via the GitHub Action? It’s installing Linux CLI tools and all my Ruby gems and Node modules on each deploy which takes a while. I’m not sure if there’s a setting to connect to a specific Docker registry/Hub/etc. Thanks!

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [October 17, 2020, 3:26pm UTC](https://community.fly.io/t/github-action-docker-image-cache/311/2 "2020-10-17T15:26:40Z")

</div>

I’ve looked at Docker caching in GitHub Actions a few times and it’s not obvious how to make it just work.

It seems like it’s possible, though: [https://github.community/t/cache-a-docker-image-built-in-workflow/16260](https://github.community/t/cache-a-docker-image-built-in-workflow/16260)

You can actually do separate build + deploy steps with `flyctl` and `docker`. You might be able to take advantage of some Docker caching actions by separating those steps, this is the simplest version:

```auto
docker build . -t myapp
flyctl deploy -i myapp

```

This will push the image layers directly from the machine and skip the `flyctl` build step.

If you figure something out, let us know, we do like to share instructions with people for this stuff!

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jared/32/46_2.png) [@jared](https://community.fly.io/u/jared)\
**Post date:** [October 24, 2020, 3:43am UTC](https://community.fly.io/t/github-action-docker-image-cache/311/3 "2020-10-24T03:43:36Z")

</div>

Sorry I didn’t reply back. Thanks for the pointers, I’ll look into it further. 🙂

---

<div class="post-metadata">

**Author:** ![ksluszniak](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/ksluszniak/32/1341_2.png) [@ksluszniak](https://community.fly.io/u/ksluszniak)\
**Post date:** [October 8, 2021, 1:41pm UTC](https://community.fly.io/t/github-action-docker-image-cache/311/4 "2021-10-08T13:41:47Z")

</div>

Here’s a working Github Actions workflow with Docker layer caching and image build in separate step for transparency:

```auto
name: Fly

on:
  push:
    branches:
      - main

env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

jobs:
  fly_deploy:
    name: Deploy with Fly

    runs-on: ubuntu-20.04

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Docker layer caching
        uses: satackey/action-docker-layer-caching@v0.0.11
        continue-on-error: true

      - name: Build app Docker image
        run: docker build . -t myapp

      - name: Deploy with Fly
        uses: superfly/flyctl-actions@1.1
        with:
          args: "deploy -i myapp"

```

It has cut my Elixir + Rust + Node multistage builds from ~6min to ~2-3min. The cache restoration step does take ~50s so things could be better if Github one day does this in native way.

---

<div class="post-metadata">

**Author:** ![ksluszniak](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/ksluszniak/32/1341_2.png) [@ksluszniak](https://community.fly.io/u/ksluszniak)\
**Post date:** [October 19, 2021, 1:32pm UTC](https://community.fly.io/t/github-action-docker-image-cache/311/5 "2021-10-19T13:32:05Z")

</div>

Warning: over time the `satackey/action-docker-layer-caching@v0.0.11` has started taking more & more time & space up to unreasonable ~5mins to restore + ~5mins to save cache, so I’ve had to disable it. I’m guessing that old Docker layers are not cleared properly - something that this article about Github Actions Docker caching mentions: [Build images on GitHub Actions with Docker layer caching — Martian Chronicles, Evil Martians’ team blog](https://evilmartians.com/chronicles/build-images-on-github-actions-with-docker-layer-caching). Maybe I’ll revisit caching using the instructions from the above article, but there’s a rumor (sorry, I don’t have a link) that something relevant from Github itself will be out this year, so it may be worth waiting a while.

---

<div class="post-metadata">

**Author:** ![ksluszniak](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/ksluszniak/32/1341_2.png) [@ksluszniak](https://community.fly.io/u/ksluszniak)\
**Post date:** [November 3, 2021, 2:21pm UTC](https://community.fly.io/t/github-action-docker-image-cache/311/6 "2021-11-03T14:21:12Z")

</div>

FYI One thing that I’ve initially missed looking to optimize GH deployments is that `fly deploy` has the `--remote-only` option that allows to build image on Fly machine with dedicated CPU and persistent volume that caches Docker layers, more info here: [New & Improved Remote Builders](https://community.fly.io/t/new-improved-remote-builders/661).

Edit: Along with `--detach`, fly.toml update (99% cached) went **from ~10m to 40s**! 🚀 OMG why isn’t this marvel exposed better? 🙂

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [November 4, 2021, 11:22am UTC](https://community.fly.io/t/github-action-docker-image-cache/311/7 "2021-11-04T11:22:33Z")

</div>

> [@ksluszniak](#):
>
> Edit: Along with `--detach` , fly.toml update (99% cached) went **from ~10m to 40s**! 🚀 OMG why isn’t this marvel exposed better? 🙂

It appears a lot faster, but your deployment still take ~10m to fully happen 🙂. The default is not to detach so users are better aware of rollbacks.

---

<div class="post-metadata">

**Author:** ![ksluszniak](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/ksluszniak/32/1341_2.png) [@ksluszniak](https://community.fly.io/u/ksluszniak)\
**Post date:** [November 4, 2021, 11:40am UTC](https://community.fly.io/t/github-action-docker-image-cache/311/8 "2021-11-04T11:40:37Z")

</div>

No no, `--detach` only makes ~1m difference in my case (1 instance in ewr + 1 in fra). It’s the docker caching that took out the remaining 9m (multi-stage image with Node stage, Rust stage, Elixir stage and final assembly stage). I had easy time spotting the culprit as I was doing `docker build` separately in yaml posted above.

I actually added `--detach` more to avoid occasional hangs of GH Actions during the `fly monitor` stage than to save time as I’d prefer to burn that extra minute but have deployment logs on the CI. Perhaps I shouldn’t have added it here to avoid confusion.

Anyway, remote builders are another deeply hidden solution to my deeply burning problem. 😉

---

<div class="post-metadata">

**Author:** ![sudhir.j](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/sudhir.j/32/39775_2.png) [@sudhir.j](https://community.fly.io/u/sudhir.j)\
**Post date:** [November 4, 2021, 11:44am UTC](https://community.fly.io/t/github-action-docker-image-cache/311/9 "2021-11-04T11:44:06Z")

</div>

I’m actually working on adding notes about the builders to the docs… just to make I’m putting them in the right place, on which page(s) did you / would you have looked for them?

---

<div class="post-metadata">

**Author:** ![ksluszniak](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/ksluszniak/32/1341_2.png) [@ksluszniak](https://community.fly.io/u/ksluszniak)\
**Post date:** [November 4, 2021, 12:24pm UTC](https://community.fly.io/t/github-action-docker-image-cache/311/10 "2021-11-04T12:24:24Z")

</div>

I’d definitely start with [Continuous Deployment with Fly and GitHub Actions](https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/) as that’s what I went through before landing in this thread. Also, the `--remote-only` option is only sparingly documented at [Flyctl](https://fly.io/docs/flyctl/deploy/) - I think it deserves a separate page that would wrap what’s in [New & Improved Remote Builders](https://community.fly.io/t/new-improved-remote-builders/661). If such separate page would be linked both from Github Actions and flyctl doc, it would be perfect IMO.
