Subscribe

Self-hosted GitHub runners sharing one user: ECR logins that blame the wrong IAM role

Three self-hosted runners on one box shared one ~/.docker/config.json, so parallel ECR logins overwrote each other and pushes failed naming another job's IAM role. Why widening IAM is wrong, and the per-runner DOCKER_CONFIG fix.

Self-hosted GitHub runners sharing one user: ECR logins that blame the wrong IAM role

A push to our api-stage ECR repository failed with an access denied error. The odd part was the role in the message. It wasn't the API deploy role at all, it was the role that belongs to a completely different repository's workflow. The API job had never assumed that role.

It looks exactly like a scoping bug in IAM. It wasn't IAM. It was three GitHub Actions runners on one machine sharing one Docker config file, and it only started when we went from one runner to three.

The setup

Our CI runs on self-hosted runners on an Ubuntu box with 12 threads and 38 GB of RAM, which is far cheaper than paying for hosted minutes on Docker builds. For a long time there was one runner. By mid August there were three of them: ~/actions-runner, ~/actions-runner-2 and ~/actions-runner-3, all version 2.336.0, all registered to the same organisation.

All three ran as the same OS user. That felt harmless. They have separate work folder, separate _diag logs, separate services. What could they share?

$HOME, it turns out. And with it, ~/.docker/config.json.

What the error looked like

Each workflow assumes its own IAM role through GitHub OIDC, then logs Docker in to ECR with aws-actions/amazon-ecr-login. The roles are scoped tightly: the API role can push to the API repositories only, the worker role to the worker repositories only. The failure was shaped like this (names changed):

denied: User: arn:aws:sts::111122223333:assumed-role/gha-worker/GitHubActions is not
authorized to perform: ecr:InitiateLayerUpload on resource:
arn:aws:ecr:ap-south-1:111122223333:repository/api-stage because no
identity-based policy allows the ecr:InitiateLayerUpload action

Read literally, it says the worker role tried to push to the API repository. And the worker role is correctly not allowed to. So the IAM was working perfectly. The question was why an API job was pushing as the worker role.

The fast and wrong fix is to give gha-worker push rights on api-stage too, or to merge both roles into one. The error would go away. You'd also lose the whole point of per-repo roles, and the real bug would still be there, waiting to push an image into a repository it shouldn't touch. Please don't widen IAM for this one.

Why the token gets swapped

docker login writes the credential into config.json under a key, and that key is the registry host:

{
  "auths": {
    "111122223333.dkr.ecr.ap-south-1.amazonaws.com": {
      "auth": "QVdTOmV5SndZWGxzYjJGa0lqb2l..."
    }
  }
}

In ECR every repository in one account and region lives under the same host. So there is exactly one slot for that registry, whatever repository you're pushing to. Now imagine two jobs on two runners:

  1. The API job assumes gha-api, logs in, and writes its token into the slot.
  2. A few seconds later the worker workflow assumes gha-worker, logs in, and overwrites the slot.
  3. The API job starts docker push. Docker reads the slot and sends the worker role's token.

ECR sees the worker role trying to push to api-stage and correctly says no. With one runner, logins never overlapped, so this never showed up. With three, it showed up whenever two builds were in the same minute, which on a busy day happens just often enough to look random.

There is a second, sneakier version. amazon-ecr-login runs docker logout in its post step by default, so a job finishing can remove the slot while another job is still pushing. That gives you no basic auth credentials instead, and you go hunting in a different direction.

The fix: one Docker config per runner

Docker reads its config directory from DOCKER_CONFIG if it's set. Each runner directory has a .env file, and the runner puts those variables into every job it runs. So each runner got its own line:

# ~/actions-runner-2/.env
LANG=C.UTF-8
DOCKER_CONFIG=/home/ci/actions-runner-2/.docker

Same for the other two, each pointing at its own directory. The .env file is read when the runner starts, so restart the service after editing it. Editing the file alone does nothing to a running runner, quite easy to forget.

cd ~/actions-runner-2
sudo ./svc.sh stop
sudo ./svc.sh start
sudo ./svc.sh status

After that each runner has its own auths slot, and two parallel logins can't collide any more.

Doing it in the workflow instead

If you can't touch the runner machines, or you want the fix to travel with the workflow, set it per job. runner.temp is a fresh directory for each job, so it can't be shared:

jobs:
  push-image:
    runs-on: [self-hosted, linux]
    steps:
      - uses: actions/checkout@v7

      - name: Give this job its own Docker config
        run: |
          mkdir -p "$RUNNER_TEMP/docker"
          echo "DOCKER_CONFIG=$RUNNER_TEMP/docker" >> "$GITHUB_ENV"

      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::111122223333:role/gha-api
          aws-region: ap-south-1

      - id: ecr
        uses: aws-actions/amazon-ecr-login@v2

      - name: Build and push
        env:
          IMAGE: ${{ steps.ecr.outputs.registry }}/api:${{ github.sha }}
        run: |
          docker build -t "$IMAGE" .
          docker push "$IMAGE"

Why a step writing to $GITHUB_ENV and not a job-level env: block? Because the runner context isn't available in jobs.<id>.env, so ${{ runner.temp }} there fails the workflow. The step is a bit ugly, but it works. It has to come before the ECR login step. The full workflow with permissions: id-token: write is in Building and pushing Docker images to ECR and GHCR from GitHub Actions.

Actually the cleanest fix of all is one OS user per runner. Then nothing in $HOME is shared: Docker config, AWS config, git credentials, caches. I'd do that for a new box. For a box that already works, the .env line was a five minute change.

The other thing $HOME was sharing

The same shared $HOME had one more trap in it, and this one is scarier. The runner user was also the user I sometimes worked as on that machine. It had ~/.aws/credentials with a personal profile pointing at a different AWS account.

A job that forgets to configure credentials should fail. On this box it wouldn't have: the AWS CLI and SDKs fall back to ~/.aws/credentials, so the job would quietly authenticate to the wrong account and carry on. So each runner's .env also points the AWS config somewhere empty of secrets:

AWS_SHARED_CREDENTIALS_FILE=/home/ci/actions-runner-2/.aws/credentials
AWS_CONFIG_FILE=/home/ci/actions-runner-2/.aws/config

Those files don't exist (or are empty), so a job with no OIDC step fails loudly with "Unable to locate credentials". That's the behaviour you want. More on locking workflows down in Securing GitHub Actions workflows.

Run each runner as a service

./run.sh in a terminal works until the box reboots. The runner ships with a helper that installs a systemd unit:

cd ~/actions-runner-3
sudo ./svc.sh install ci
sudo ./svc.sh start
systemctl list-units 'actions.runner.*' --no-pager

The argument to install is the user the service runs as. The unit is named actions.runner.<org>.<runner name>.service and is enabled at boot. All three of ours run like this.

One more thing a shared box teaches you: disk. With one runner the Docker build cache had reached 334 GB before I noticed. Now a cron entry runs docker builder prune -af --filter until=168h every Sunday at 04:17, which drops anything not used for a week. Three runners fill the disk three times faster, so put that in place before the second runner goes in, not after.

If your deploy step comes next, Deploying to ECS from GitHub Actions picks up from the pushed image.