Sub topic 9
Building a Docker container action
A Docker container action packages your code and its whole runtime environment into an image. The runner builds or pulls that image, starts a container, and runs your entrypoint inside it. This chapter covers the runs configuration, the Dockerfile, and how the container sees the workflow.
This chapter assumes you can read basic workflow syntax (jobs, steps, uses, with), write a simple Dockerfile, and use a POSIX shell. You should also know what action.yml contains. The next chapter goes deeper on inputs and outputs. Here they appear only as far as the example needs.
When a container action fits
Choose a container action when your action needs a specific OS, a specific tool version, or a language other than JavaScript. The image fixes the environment, so the action behaves the same on every run.
The trade-offs are real:
- Container actions run only on Linux runners. Workflows on
windows-latestormacos-latestcannot use them. - Startup is slower than for a JavaScript action, because the runner must build or pull an image first.
- The container is isolated from the runner's installed tools. Anything the action needs must be in the image.
How a run works
The runner mounts the job's workspace into the container at /github/workspace and sets that directory as the working directory. Files that your action writes there remain after the container exits, so later steps can use them. Files written anywhere else in the container disappear.
The metadata: `runs` for Docker
The action.yml file looks like a JavaScript action's file, except for the runs block.
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 keys under runs that matter most are:
| Key | Purpose |
|---|---|
| `using` | Must be `'docker'`. |
| `image` | Either `'Dockerfile'` (a path relative to the action) or `'docker://<registry>/<image>:<tag>'`. |
| `args` | Array of strings passed to the container. Expressions such as `${{ inputs.who-to-greet }}` are allowed. |
| `entrypoint` | Overrides the image's `ENTRYPOINT`. Optional. |
| `env` | Extra environment variables for the container. Optional. |
| `pre-entrypoint` / `post-entrypoint` | Scripts that run before and after the main entrypoint, in their own containers from the same image. |
The args array becomes the arguments of the entrypoint, in order. If you also set entrypoint, args still follow it. If you omit args, the image's CMD is used.
The Dockerfile
FROM alpine:3.20
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]Each line has a reason:
FROM alpine:3.20uses a small base image with a pinned tag. A pinned tag keeps builds repeatable. A tag likelatestcan change under you.COPYputs the script into the image. The build context is the directory that holds the Dockerfile, so the path is relative to it.RUN chmod +xmakes sure the script is executable, even if the executable bit was lost in Git.ENTRYPOINTuses the exec form (a JSON array). The exec form starts your script as the main process and passesargsto it directly. The shell form (ENTRYPOINT /entrypoint.sh) wraps the command in/bin/sh -cand ignores the arguments.
Avoid two Dockerfile instructions in actions. Do not set WORKDIR, because GitHub sets the working directory to the workspace and overrides it. Do not set USER. The runner needs the container to run as the default user (root) so it can write to the mounted workspace and the files it uses for outputs.
The entrypoint script
#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"$1 is the first entry of args, which is the greeting target. The last line sets the time output by appending name=value to the file whose path is in GITHUB_OUTPUT. The runner reads that file after the container exits. The next chapter covers this mechanism and its edge cases, such as multi-line values.
The shebang #!/bin/sh -l uses sh, not bash, because Alpine ships BusyBox sh and has no bash. If your script uses bash features such as arrays or [[ ]], either install bash in the image or change the shebang and keep the script strictly POSIX.
Check the line endings too. A script saved with Windows (CRLF) line endings fails with exec /entrypoint.sh: no such file or directory, because the kernel looks for an interpreter named /bin/sh\r. This message is misleading, since the file exists.
Directory layout
A minimal container action repository has three files at the root:
.
├── action.yml
├── Dockerfile
└── entrypoint.shThe image: 'Dockerfile' value is resolved relative to the directory that contains action.yml. If the action lives in a subdirectory of a larger repository, the Dockerfile must be next to that subdirectory's action.yml.
Running the action in a workflow
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 }}"Two details stand out. First, actions/checkout must run before uses: ./. The runner looks for action.yml in the checked-out workspace, so without checkout the step fails with a missing metadata error. Second, the id: hello value lets later steps read the output as steps.hello.outputs.time.
Build from a Dockerfile or pull a prebuilt image
With image: 'Dockerfile', the runner builds the image on every run. With a docker:// reference, it pulls an image you published earlier.
| Row | `image: 'Dockerfile'` | `image: 'docker://ghcr.io/owner/name:1.0.0'` |
|---|---|---|
| Startup time | Slower, builds each run | Faster, one pull |
| Setup effort | None, just commit the file | You must build and publish the image |
| Reproducibility | Depends on base image and package repositories at build time | Fixed by the tag or digest you reference |
| Best for | Small images, prototypes | Heavy dependencies, widely used actions |
A common release pattern is to build the image in a workflow, push it to a registry, and point action.yml at that tag. Pin the reference to a version tag, or to a digest (@sha256:...) if you need it to be immutable.
Common errors
- Permission denied on the entrypoint. The script lacks the executable bit and the
chmodline is missing. AddRUN chmod +x. no such file or directoryfor a file that exists. Check for CRLF line endings, or for a shebang that names an interpreter the image does not have, such as#!/bin/bashon Alpine.- Arguments ignored. The Dockerfile uses the shell form of
ENTRYPOINT. Switch to the exec form. - Action not found. The workflow did not check out the repository before
uses: ./. - Files vanish after the step. The script wrote outside
/github/workspace. Write into the working directory instead. - Job fails on a Windows or macOS runner. Docker container actions need a Linux runner.
Exercise
Your container action builds fine, but the greeting prints as an empty string. The Dockerfile ends with ENTRYPOINT /entrypoint.sh, and action.yml passes args: [${{ inputs.who-to-greet }}]. What is wrong, and what is the fix?
Show answer
The Dockerfile uses the shell form of ENTRYPOINT, which runs the command as /bin/sh -c /entrypoint.sh and drops the extra arguments, so $1 is empty. Change it to the exec form, ENTRYPOINT ["/entrypoint.sh"], so the runner's args reach the script.
Exercise
A teammate wants to run your container action on a macos-latest job. Can they, and what are two alternatives?
Show answer
No. Docker container actions run only on Linux runners. They can move the step to a job on ubuntu-latest, or you can rewrite the action as a JavaScript action, or as a composite action, which can run on any runner OS.