buildkite-plugins / docker-compose-buildkite-plugin

🐳⚡️ Run build scripts, and build + push images, w/ Docker Compose
MIT License
172 stars 139 forks source link

Issue using image cache for new agents #398

Closed brianstorti closed 1 year ago

brianstorti commented 1 year ago

I'm trying to reuse a previously built docker image so new agents don't need to build the image from scratch every time (docs), but that doesn't seem to be working as expected.

It seems like the image is being pulled, and the docker-compose file is being modified with a cache_from that should make it use the image, but it always builds the image again:

Screenshot 2023-06-16 at 10 35 49

Here's the config I have:

steps:
  - label: ":docker: Build image"
    plugins:
      - docker-compose#v4.13.0:
          cli-version: 2
          build: rails
          image-repository: us-east4-docker.pkg.dev/myrepo/core-buildkite
          cache-from:
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:${BUILDKITE_BRANCH}
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:latest

  - wait

  - label: ":docker: Push to final repository"
    plugins:
      - docker-compose#v4.13.0:
          cli-version: 2
          push:
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:${BUILDKITE_BRANCH}
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:latest

  - wait

  - name: "Test image cache"
    plugins:
      - docker-compose#v4.13.0:
          run: rails
          cli-version: 2
    command: echo 'it works'

I can see the image is being correctly pushed to the remote registry, and it's also pulled as expected. I can even see that when I'm pushing this image, all the layers already exist, so I'm not sure why they are not being used when I'm building the image: Screenshot 2023-06-16 at 10 41 37

Am I missing something?

brianstorti commented 1 year ago

I was missing the BUILDKIT_INLINE_CACHE build arg:

steps:
  - label: ":docker: Build image"
    plugins:
      - docker-compose#v4.13.0:
          cli-version: 2
          build: rails
          image-repository: us-east4-docker.pkg.dev/myrepo/core-buildkite
          args:
            - BUILDKIT_INLINE_CACHE=1 # <-- This
          cache-from:
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:${BUILDKITE_BRANCH}
            - rails:us-east4-docker.pkg.dev/myrepo/core-buildkite:latest