Sub topic 9
Building a Docker container action
This chapter assumes you know basic workflow syntax, can read a Dockerfile, and are comfortable with a POSIX shell. You already know how action.yml declares inputs and outputs. Here you will build the same "hello" action as a container.
When a container fits
A Docker container action packages your code and its tools into an image. The runner builds or pulls the image and runs it as a container. This gives you a fixed environment: any language, any system tool, any version.
The cost is platform. Container actions run only on Linux runners. They also start more slowly than JavaScript actions, because the image must be built or pulled first.
The action metadata
The runs block is what makes this a container action. Set using: 'docker' and point image at a Dockerfile in the action's directory.
name: 'Hello World (Docker)'
description: 'Greet someone from a container'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'World'
outputs:
time:
description: 'The time we greeted you'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.who-to-greet }}The args list becomes the arguments passed to the container's entrypoint. The expression ${{ inputs.who-to-greet }} is evaluated before the container starts, so the entrypoint sees the final value as $1.
The Dockerfile
FROM alpine:3.20
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]Use the exec form of ENTRYPOINT (a JSON array), as shown. The runner appends your args to it. A small base image such as Alpine keeps the build and pull time low.
The entrypoint script
#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"Two things happen here. The greeting comes from the first argument. The output is set by appending a name=value line to the file whose path is in GITHUB_OUTPUT. The runner reads that file after the container exits.
How data moves
Testing it in a workflow
Use uses: ./ to run the action from the repository root. Check out the code first, or the runner cannot find action.yml.
name: Test hello action
on: [push]
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Show the output
run: echo "The time was ${{ steps.hello.outputs.time }}"The id: hello on the action step is what lets the later step read steps.hello.outputs.time.
Common errors
- Permission denied on the entrypoint. The script is not executable. The
chmod +xline in the Dockerfile fixes it. - Output is empty. The script wrote to standard output instead of appending to
$GITHUB_OUTPUT. Printingtime=...alone does nothing. - Container action fails on Windows or macOS. Container actions need a Linux runner. Use
runs-on: ubuntu-latest. - Input not seen. You declared the input but did not list it under
args. The container does not receive inputs automatically as arguments.
Exercise
Your container action prints time=12:00 but steps.hello.outputs.time is empty in the next step. What is wrong, and how do you fix it?
Show answer
The script wrote the line to standard output. The runner only reads outputs from the file named by GITHUB_OUTPUT. Change the line to echo "time=12:00" >> "$GITHUB_OUTPUT". Also confirm the action step has an id so later steps can reference it.
Exercise
You add a new input shout to action.yml but the script never sees it. Name two ways to pass it to the container.
Show answer
Add ${{ inputs.shout }} to the args list, where it becomes $2. Or read it from the environment variable the runner sets for it, INPUT_SHOUT.