dockergithub-actions

Use local Docker image in a GitHub action?


I would like to setup a GitHub Action to build my project and run tests locally. As I use .devcontainer I have a Dockerfile in .devcontainer/Dockerfile that provide everything I need to build my project.

Now I would like to write a GitHub Action to build the project on every push. Locally I would have done this:

docker build -t local - < .devcontainer/Dockerfile
docker run -it -v $(pwd):/srv -w/srv local make test

GitHub actions looks cumbersome, but I eventually wrote this:

on: push
jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      - name: check out repository
        uses: actions/checkout@v2
      - name: build project
        container:
          image: ".devcontainer/Dockerfile"
          volumes:
            - .:/srv
        run: make test

Unfortunately it does not like the container keyword.

Any clues?


Solution

  • The container key is designed for running publicly available dockerized actions, and is available under the job.<job_id> key, not the steps key. You do not need it to accomplish your task.

    The GitHub runners have an extensive list of software installed already, including docker and docker compose.

    You can easily run the same commands you run locally in your GitHub workflow.

    steps:
    - name: Check out code
      uses: actions/checkout@v3
    - name: Build docker images
      run: docker build -t local < .devcontainer/Dockerfile
    - name: Run tests
      run: docker run -it -v $PWD:/srv -w/srv local make test
    

    I am running most of my workflows like this, only using docker-compose, so you end up with cleaner workflows:

    steps:
    - name: Check out code
      uses: actions/checkout@v3
    - name: Build docker images
      run: docker compose build
    - name: Setup database
      run: docker compose run setup
    - name: Run tests
      run: docker compose run test