GitHub Actions: How can I cache the Docker images for Testcontainers?
Asked Answered
T

1

15

I execute some tests in GitHub Actions using Testcontainers.

Testcontainers pulls the images which are used in my tests. Unfortunately the images are pulled again at every build.

How can I cache the images in GitHub Actions?

Triune answered 18/2, 2022 at 21:38 Comment(0)
P
14

There's no official support from GitHub Actions (yet) to support caching pulled Docker images (see this and this issue).

What you can do is to pull the Docker images, save them as a .tar archive and store them in a folder for the GitHub Actions cache action to pick it up.

A sample workflow can look like the following:

 build-java-project:
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v2

        - run: mkdir -p ~/image-cache

        - id: image-cache
          uses: actions/cache@v1
          with:
              path: ~/image-cache
              key: image-cache-${{ runner.os }}

        - if: steps.image-cache.outputs.cache-hit != 'true'
          run: |
              docker pull postgres:13
              docker save -o ~/image-cache/postgres.tar alpine

        - if: steps.image-cache.outputs.cache-hit == 'true'
          run: docker load -i ~/image-cache/postgres.tar

        - name: 'Run tests'
          run: ./mvnw verify

While this is a little bit noisy, you'd need to adjust the pipeline every time you're depending on a new Docker image for your tests. Also be aware of how to do cache invalidation as I guess if you plan to use a :latest tag, the solution above won't recognize changes to the image.

The current GitHub Actions cache size is 10 GB which should be enough for a mid-size project relying on 5-10 Docker images for testing.

There's also the Docker GitHub Cache API but I'm not sure how well this integrates with Testcontainers.

Prisilla answered 19/2, 2022 at 8:28 Comment(2)
Thanks! That helps a lot, although I agree that it's not optimal.Triune
So I tried this, and it wasn’t any faster. Seems like the biggest amount of time spent is actually at the load command. Whether you’re loading an image from docker or GitHub actions cache you still gotta perform some kind of transfer to the runtime container.Zacarias

© 2022 - 2024 — McMap. All rights reserved.