Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache docker-compose build inside github-action

Is there any way to cache docker-compose so that it will not build again and again? here is my action workflow file:

name: Github Action
on:
  push:
    branches:
      - staging
jobs:
  test:
    runs-on: ubuntu-18.04

    steps:
      - uses: actions/checkout@v1

      - name: Bootstrap app on Ubuntu
        uses: actions/setup-node@v1
        with:
          node-version: '12'


      - name: Install global packages
        run: npm install -g yarn prisma


      - name: Install project deps
        if: steps.cache-yarn.outputs.cache-hit != 'true'
        run: yarn


      - name: Build docker-compose
        run: docker-compose -f docker-compose.test.prisma.yml up --build -d

I want to cache the docker build step. I have tried using if: steps.cache-docker.outputs.cache-hit != 'true' then only build but didn't work.

like image 556
Ashik Avatar asked Apr 28 '20 23:04

Ashik


People also ask

Does Docker compose cache?

Compose. If you're using Docker Compose, you can add the cache_from option to the compose file, which maps back to the docker build --cache-from <image> command when you run docker-compose build . To take advantage of BuildKit, make sure you're using a version of Docker Compose >= 1.25.

What is GitHub actions Docker?

GitHub Action to build and push Docker images with Buildx with full support of the features provided by Moby BuildKit builder toolkit. This includes multi-platform build, secrets, remote cache, etc. and different builder deployment/namespacing options.

What is Docker layer caching?

With Docker layer caching, you can save individual layers of the Docker images you build so that they can be reused in subsequent pipeline runs. Docker layer caching can save your team significant time during the build process by bypassing some or all of the image build steps.


Video Answer


3 Answers

What you are referring to is called "docker layer caching", and it is not yet natively supported in GitHub Actions.

This is discussed extensively in several places, like:

  • Cache docker image forum thread
  • Cache a Docker image built in workflow forum thread
  • Docker caching issue in actions/cache repository

As mentioned in the comments, there are some 3rd party actions that provide this functionality (like this one), but for such a core and fundamental feature, I would be cautious with anything that is not officially supported by GitHub itself.

like image 115
DannyB Avatar answered Oct 25 '22 21:10

DannyB


For those arriving here via Google, this now "supported". Or at least it is working: https://github.community/t/use-docker-layer-caching-with-docker-compose-build-not-just-docker/156049. The idea is to build the images using docker (and its cache) and then use docker compose to run (up) them.

like image 34
Vaal Avatar answered Oct 25 '22 21:10

Vaal


If using docker/bake-action or docker/build-push-action & want to access a cached image in subsequent steps -

  • Use load:true to save the image
  • Use the same image name as the cached image across steps in order to skip rebuilds.

Example:

...
        name: Build and push
        uses: docker/bake-action@master
        with:
          push: false
          load: true
        set: |
            web.cache-from=type=gha
            web.cache-to=type=gha
      -
        name: Test via compose
        command: docker compose run web tests
...
services:
  web:
    build:
      context: .
    image: username/imagename
    command: echo "Test run successful!"

See the docker team's responses;

  • How to access the bake-action cached image in subsequent steps?
  • How to use this plugin for a docker-compose?
  • How to share layers with Docker Compose?`
  • Experiment on caching docker compose images in GitHub Actions
like image 1
rdmolony Avatar answered Oct 25 '22 21:10

rdmolony