Cache docker images

Hello,

in my GitHub Action Workflow I am starting various containers with docker-compose. This costs me tremendous 20 minutes, because it has to pull the docker images in the first place - on every single run. So it is really important for me to get the containers or at least the used images (e.g. nginx:alpine or mariadb:latest) cached!

The only solution that can be found on the internet seems to be this action from the GitHub marketplace: whoan/docker-build-with-cache-action.

And let me tell you I have played with it A LOT. Like weeks… And it did actually pull the images from cache at some point, but not as I needed it… it did not work out let’s say! This might be because I really do not understand how this action works. It is just working very different than I would expect!

How I expect the docker-build-with-cache-action to work:

I define the image nginx:alpine to be cached. Is this image used in a Dockerfile or to build a container, pull it from cache (or not at all, just use it (depending on how it works)). Pull it from public docker registry first, if it does not exist in the cache yet.

How it works or what I don’t understand:

  1. Docker registry credentials are required.

My username and password to a docker registry are required fields when using the action. How that? I do not want to push any images nor do I pull from private repositories. How is this required and not optional?

  1. It will look for a Dockerfile.

When trying to use this action, it will always look for a Dockerfile to build the image. And I don’t understand that. nginx:alpine is already a very valid docker image on itself. I am using it in a Dockerfile as a basis to build my actual container, but this is none of the action’s business?

I have really tried to work around with 1 and 2 but it would get a total mess, not make any sense with the idea of a docker-compose.yml and not work at the end. It seems that there are some deep misunderstandings for me using this action, because I seem to be the only one questioning this. Also I am still a beginner when using docker.

Thanks in advance. I really hope I’m getting answers to this. I’d appreciate it.

No matter the package is public or private, you need an access token to publish, install, and delete packages in GitHub Packages. Reference here.
About the detailed functions of Docker build-with-cache action, I recommend you directly report an issue in the action repository here to get more and accurate information from the developers of this action.
In addition, you aslo can consider to use the GitHub’s official cache action actions/cache to cache your docker images. More details, you reference Caching dependencies to speed up workflows.

1 Like

Thanks. You’re right. I forgot that.

Is GitHub’s actions/cache capable of caching docker images? Are there any resources or examples to this?

I searched but did not find examples to cache docker images. However, I tested with a simple project like as below, it seems can work as expected, te resource files of my docker image can be cache/restore in the specific directory under $GITHUB_WORKFLOW.

name: Test

on: push
env:
  CACHE_NAME: cache-image

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Checkout BriRanActions/hello-world-docker-action
        uses: actions/checkout@v2
        with:
          repository: BriRanActions/hello-world-docker-action
          token: ${{ secrets.MY_PAT }}
          path: hello-world-docker-action

      - name: Test Cache
        uses: actions/cache@v1
        with:
          path: hello-world-docker-action
          key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ github.sha }}

  test_cache:
    needs: [test]
    name: test cache
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Cache
        uses: actions/cache@v1
        with:
          path: hello-world-docker-action
          key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.CACHE_NAME }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-
      - name: Check directory
        run: |
          echo " *********************************************************************"
          cd hello-world-docker-action
          echo $PWD
          ls
          echo " *********************************************************************"

In addition, you also can report an issue in the repository of this action here to get more and accurate detailed information about this action.

If you wants to cahce and use this image to setup a container for a job, I did not find any method to do this.

2 Likes

@mg98 ,

Just want to say hi and confirm if you have received my previous reply.

As I mentioned in the previous reply, the cache action can cahce the source files of the Docker images, but it seems can’t cache the build of the Docker images and it also can’t cache the containers for a job.

Is my reply helpful to you? If you have any other question about this ticket, feel free to tell us.

Thanks for the detail - so to be clear -

Caching the image that apply for docker run is not possible? It’d be great not to have to pull every time.