Pete Freitag Pete Freitag

Writing a GitHub Actions Workflow that Uses a Docker Image

Published on May 14, 2020
By Pete Freitag
web

When working with Github Actions there are a few different ways to write a workflow yaml that uses a docker image or a docker container.

Using uses

The easiest way you can use a docker container in your GitHub Actions workflow is with the uses option. This is combined with a docker URI, here's an example Github Actions Job that uses docker:

jobs:
  docker-example:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run box version	    
      uses: docker://foundeo/minibox:latest
      with:
        entrypoint: /opt/box/box
        args: version

In this example we tell GitHub Actions that this job uses the docker image foundeo/minibox:latest. You can also specify an entrypoint (the command to run inside the container) and args (command line arguments to pass to the command specified in entrypoint). You might be able to omit entrypoint if the container already species the entrypoint you want to use.

Can the Docker Container Access My Github Code?

This was a question I had when I first started playing with GitHub Actions. It turns out, you can access your github repository code because when the workflow job executes uses: docker://container it also mounts a volume, and passes several environment variables. The GitHub Actions workflow workspace is mounted to the path: /github/workspace/

Note that the code checked out of our github repository because we ran uses: actions/checkout@v2 as a prior step.

Another way to use a docker image

Another way to go run commands inside a docker image as part of your Github Actions workflow is to use the container and image options. Here's an example of specifying a docker image in the workflow yaml:

jobs:
  testbox:
    runs-on: ubuntu-latest
    container:
      image: azul/zulu-openjdk-alpine:8-jre
    steps:  
    - uses: actions/checkout@v2
    - name: What OS is running
      run: uname -a
    - name: What java version do we have
      run: java -version

In this example we are running multiple commands inside the container image. In this case we are running Java / OpenJDK inside an Alpine Linux container.

Hopefully you find these examples useful to show the different ways you can run inside a docker container as part of your GitHub Actions workflow.



docker git github ci

Writing a GitHub Actions Workflow that Uses a Docker Image was first published on May 14, 2020.

If you like reading about docker, git, github, or ci then you might also like:

Discuss / Follow me on Twitter ↯

Comments

I ran the second example with and without the container command and the results were the same. How do I know the steps were run in the container?
by Jason on 02/02/2021 at 4:14:04 PM UTC
Jason - you should have different output, eg when running in the container the java version will contain: "Zulu" when running without a container the default java would be "AdoptOpenJDK"
by Pete Freitag on 07/29/2021 at 8:53:09 PM UTC
Hi Pete,

while using docker in a step should the image be a public docker image. Can i use an image from local repostiory or private repostiory if yes , how do I pass the auth info?

also is there any way that we can use a local image that is already pulled, in case we cannot pull the image from the private CR.
by Gopi on 02/04/2022 at 7:25:15 AM UTC
Hi Gopi

You can use a private dockerhub image, you just have to add under secrets your docker username and password, the code is like this :
container:
image: id/img:version
credentials:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

steps:
...
by Issam on 03/02/2022 at 2:23:07 PM UTC