Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty big cache #252

Open
MarcelCoding opened this issue Dec 14, 2020 · 30 comments
Open

Pretty big cache #252

MarcelCoding opened this issue Dec 14, 2020 · 30 comments
Labels
area/buildkit area/cache kind/upstream Changes need to be made on upstream project

Comments

@MarcelCoding
Copy link

MarcelCoding commented Dec 14, 2020

Description

The cache can grow very quickly with large images, since old entries are not deleted.

Configuration

[...]

      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

[...]

      - name: Build
        uses: docker/build-push-action@v2
        with:
          push: false
          tags: ${{ steps.prepare.outputs.image }}
          platforms: ${{ env.DOCKER_PLATFORMS }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
          context: .

[...]

Logs

logs_37.zip

My solution

Add a clean-cache configuration option that runs the following command before exporting the layers docker system prune -f --filter "until=5h".

@crazy-max
Copy link
Member

crazy-max commented Dec 14, 2020

@MarcelCoding

The cache can grow very quickly with large images, since old entries are not deleted.

Yes you're right atm caches are copied over the existing cache so it keeps growing. Can you open an issue on buildkit repo about that please? In the meantime you can do this:

[...]

      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

[...]

      - name: Build
        uses: docker/build-push-action@v2
        with:
          push: false
          tags: ${{ steps.prepare.outputs.image }}
          platforms: ${{ env.DOCKER_PLATFORMS }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new
          context: .

[...]

      - name: Move cache
        run:
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache

cc. @tonistiigi

@crazy-max crazy-max added the kind/upstream Changes need to be made on upstream project label Dec 14, 2020
@MarcelCoding
Copy link
Author

@crazy-max that means that the cache-to option does not export the entire Docker cache, just the one for the current images?

@crazy-max
Copy link
Member

@MarcelCoding

that means that the cache-to option does not export the entire Docker cache, just the one for the current images?

cache-to exports the build cache for the current image being built with buildkit yes. If you are implying that the cache can be shared between different images, this is not the case. See #153 (comment).

@MarcelCoding
Copy link
Author

MarcelCoding commented Dec 14, 2020

Ok, thanks for the help, I will create an issue in the builtkit repo. For implementing an option to clean the cache/remove old versions of the cache.

@zero88
Copy link

zero88 commented Dec 22, 2020

Hi @crazy-max
Can I use buildkit registry cache within local registry by GitHub service container, then caching registry volume as GitHub cache.
After each run, just prune cache in local registry to keep as small as possible.

However, I'm not sure about GitHub service container is able to create volume for service container and your GitHub action support that (because I'm only see caching inline option)

andersfischernielsen added a commit to Islanders-Game/Islanders that referenced this issue Feb 17, 2021
andersfischernielsen added a commit to Islanders-Game/Islanders that referenced this issue Feb 17, 2021
This is done using Docker's own released actions in combination with GitHub's caching actions.
A local package registry is used, and has been added to Dockerfile(s) as ARGs.
These ARGs do not interfere with local Docker builds in my testing.
Individual caches are used to speed up caching (see: docker/build-push-action#252 (comment))
andersfischernielsen added a commit to Islanders-Game/Islanders that referenced this issue Feb 17, 2021
This is done using Docker's own released actions in combination with GitHub's caching actions.
A local package registry is used, and has been added to Dockerfile(s) as ARGs.
These ARGs do not interfere with local Docker builds in my testing.
Individual caches are used to speed up caching (see: docker/build-push-action#252 (comment))
oittaa added a commit to oittaa/imagemagick-docker that referenced this issue Feb 27, 2021
* Explicit platform

* Temp cache fix docker/build-push-action#252
@malobre
Copy link

malobre commented Mar 23, 2021

docker/buildx#535 should fix this and make using github cache a breeze:

[...]

      - name: Build
        uses: docker/build-push-action@v2
        with:
          tags: user/app:latest
          cache-from: type=gha
          cache-to: type=gha

@zero88
Copy link

zero88 commented Apr 7, 2021

@rafis
Copy link

rafis commented Jun 12, 2021

@malobre how to fix this?

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@master
        with:
          install: true

moby/buildkit:buildx-stable-1 => buildkitd github.com/moby/buildkit v0.8.3 81c2cbd8a418918d62b71e347a00034189eea455

error: failed to solve: rpc error: code = Unknown desc = unknown cache exporter: "gha"
102
Error: buildx call failed with: error: failed to solve: rpc error: code = Unknown desc = unknown cache exporter: "gha"

@malobre
Copy link

malobre commented Jun 12, 2021

The server side is not implemented yet: moby/buildkit#1974.

@jauderho
Copy link

FWIW, there's a syntax error in the code block below. There is a missing "|" that took me a while to find and correct.

This should be the correct invocation:

  - name: Move cache
    run: |
      rm -rf /tmp/.buildx-cache
      mv /tmp/.buildx-cache-new /tmp/.buildx-cache

@MarcelCoding

The cache can grow very quickly with large images, since old entries are not deleted.

Yes you're right atm caches are copied over the existing cache so it keeps growing. Can you open an issue on buildkit repo about that please? In the meantime you can do this:

[...]

      - name: Move cache
        run:
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache

cc. @tonistiigi

@crazy-max
Copy link
Member

@jauderho https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#github-cache

@ahgraber
Copy link

Given that moby/buildkit#1974 was just merged, what does the timeline look like to use cache type=gha in build-push-action?

@crazy-max
Copy link
Member

crazy-max commented Jul 13, 2021

You can test the gha cache exporter using this workflow while waiting for buildx 0.6 and BuildKit 0.9 to be GA. Feel free to give us your feedback, thanks!

@jauderho
Copy link

I just tried this out on one of my workflows. See https://github.com/jauderho/dockerfiles/actions/workflows/cloudflared.yml

Looks like the "Setup Buildx" step now takes longer but I'm assuming that's due to the rest not yet being merged in. More importantly, the "Build and push" step looks to be much faster.

Nice job!

@crazy-max
Copy link
Member

@jauderho

Looks like the "Setup Buildx" step now takes longer but I'm assuming that's due to the rest not yet being merged in. More importantly, the "Build and push" step looks to be much faster.

Yes that's it, buildx is built on-fly atm, that's why it takes more time for the setup step.

@jauderho
Copy link

Awesome. Looking forward to everything being merged in.

@crazy-max
Copy link
Member

@jauderho

Looks like the "Setup Buildx" step now takes longer

Buildx 0.6.0-rc1 has been released. I've updated the workflow to use it so now it should be faster than building from source.

@jauderho
Copy link

Hmm, not sure if I am doing something wrong here but after updating to call buildx 0.6.0-rc1, it does not seem to trigger the caching.

Here is my action: https://github.com/jauderho/dockerfiles/blob/main/.github/workflows/cloudflared.yml

With buildx 0.6.0-rc1

Compare this to 2 days ago (which has the expected behavior)

@crazy-max
Copy link
Member

@jauderho

Use image=moby/buildkit:master instead of image=moby/buildkit:v0.9.0-rc1.

@jauderho
Copy link

jauderho commented Jul 15, 2021

@crazy-max

Per your suggestion, updated to use image=moby/buildkit:master but it does not appear to make a difference.

@crazy-max
Copy link
Member

@jauderho

As you're on a monorepo building multi Docker images with different context, you should use a specific scope for each one of them to avoid cache collision/invalidation. For example in your cloudflared workflow:

cache-from: type=gha,scope=cloudflared
cache-to: type=gha,scope=cloudflared

mdegat01 added a commit to mdegat01/addon-promtail that referenced this issue Nov 10, 2021
Appears there is an issue where the build cache grows unbounded until it hits Github's limit (docker/build-push-action#252, moby/buildkit#1896). Clear the cache after builds to prevent this
mdegat01 added a commit to mdegat01/addon-sharry that referenced this issue Nov 10, 2021
Appears there is an issue where the build cache grows unbounded until it hits Github's limit (docker/build-push-action#252, moby/buildkit#1896). Clear the cache after builds to prevent this
mdegat01 added a commit to mdegat01/addon-loki that referenced this issue Nov 10, 2021
Appears there is an issue where the build cache grows unbounded until it hits Github's limit (docker/build-push-action#252, moby/buildkit#1896). Clear the cache after builds to prevent this.
mdegat01 added a commit to mdegat01/promtail-journal that referenced this issue Nov 10, 2021
Appears there is an issue where the build cache grows unbounded until it hits Github's limit (docker/build-push-action#252, moby/buildkit#1896). Clear the cache after builds to prevent this.
rra added a commit to lsst-sqre/vo-cutouts that referenced this issue Feb 17, 2022
Otherwise, the cache is effectively discarded since the same images
aren't used for both builds and the cache therefore assumes they're
no longer in use and garbage-collects them.

See docker/build-push-action#252 (comment)
rushilsrivastava added a commit to rushilsrivastava/python-grpcio-alpine-docker that referenced this issue Feb 18, 2022
nkuba added a commit to keep-network/keep-core that referenced this issue Aug 8, 2022
This is a workaround for a known problem docker/build-push-action#252
nkuba added a commit to keep-network/keep-core that referenced this issue Aug 8, 2022
This is a workaround for a known problem docker/build-push-action#252
@MichaelVoelkel
Copy link

#756 mentions a comment where the "old" way is used. I switched to cache-from/cache-to:gha BUT if the whole job fails (or the build is not entirely successful), the cache does not seem to be used.

In that regard, it seems still suboptimal or am I potentially doing anything wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/buildkit area/cache kind/upstream Changes need to be made on upstream project
Projects
None yet
Development

No branches or pull requests

10 participants