Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 941
Oliver Avatar asked Dec 08 '25 19:12

Oliver


1 Answers

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.

like image 66
rieckpil Avatar answered Dec 10 '25 11:12

rieckpil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!