Run docker inside GitHub actions
Photo by unsplash

How to use docker in GitHub actions for ubuntu, windows, and mac.

TL;DR

All you need is

- name: Updating and upgrading brew
  if: matrix.os == 'macos-latest'
  run: |
    bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    brew --version    
- name: Install and start docker
  if: matrix.os == 'macos-latest'
  run: |
    # for the docker cli
    brew install docker
    # installed by default
    colima start
    docker ps    

The challenge

I found out that ubuntu, windows this very straightforward as the daemon and cli are preinstalled and you can easily do the following in your .github/workflows/test.yaml:

jobs:
  info:
    steps:
      - name: Test docker
        run: |
          docker version
          docker info          

However, for mac this is not the case. So how do we get this set up?

My first investigation brought me to the following snippet found in this comment

brew install --cask docker
sudo /Applications/Docker.app/Contents/MacOS/Docker --unattended --install-privileged-components
open -a /Applications/Docker.app --args --unattended --accept-license # <-- NOT working any more
while ! /Applications/Docker.app/Contents/Resources/bin/docker info &>/dev/null; do sleep 1; done

This did the job in 2021, however, started breaking in late 2022, as docker made some changes.

The solution

This brings me to the solution, which is colima. After hearing from it in another comment. During the research, I documented the process in this PR

Hence the final yaml looks as follows:

jobs:
  info:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Updating and upgrading brew
        if: matrix.os == 'macos-latest'
        run: |
          bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
          brew --version          
      - name: Install and start docker
        if: matrix.os == 'macos-latest'
        run: |
          # for the docker cli
          brew install docker
          # installed by default
          colima start
          docker ps          
      - name: Test docker
        run: |
          docker version
          docker info          

Conclusion

I was surprised that for ubuntu, windows docker runs out-of-the-box in GitHub actions and not for mac. However, I got to know the colima project on the way and have my PoC ready.

You find the repository here: https://github.com/mavogel/docker-gh-action-test

Cheers

Like what you read? You can hire me, or drop me a message to see which services 💻 may help you 👇