Writing a GitHub Actions Workflow that Uses a Docker Image
By Pete Freitag
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
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.
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 ↯
Tweet Follow @pfreitagComments
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.
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:
...