dockergithub-actions

How do I use Docker with GitHub Actions?


When I create a GitHub Actions workflow file, the example YAML file contains runs-on: ubuntu-latest. According to the docs, I only have the options between a couple versions of Ubuntu, Windows Server and macOS X.

I thought GitHub Actions runs inside Docker. How do I choose my Docker image?


Solution

  • A job (as part of a workflow) runs inside a virtual machine. You choose one of the environments provided by them (e.g. ubuntu-latest or windows-2019).

    A job consists of one or more steps. A step may be a simple shell command, using run. But it may also be an action, using uses

    name: CI
    
    on: [push]
    
    jobs:
      myjob:
        runs-on: ubuntu-18.04 # linux required if you want to use docker
        steps:
        # Those steps are executed directly on the VM
        - run: ls /
        - run: echo $HOME
        - name: Add a file
          run: touch $HOME/stuff.txt
        # Those steps are actions, which may run inside a container
        - uses: actions/checkout@v1
        - uses: ./.github/actions/my-action
        - uses: docker://continuumio/anaconda3:2019.07
    

    Keep in mind that you need to select a linux distribution as the environment if you want to use Docker.

    Take a look at the documentation for uses and run for further details.

    It should also be noted that there is a container option, allowing you to run any steps that would usually run on the host to be runned inside a container: https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idcontainer