Self-hosted Runner can't mount workspace correctly

I dockerize github runner as: https://github.com/swaglive/docker-github-actions-runner

But it’s not mounted when I tried to lists workspace in an action. The only difference is runs-on:

ubuntu-show-env-alpine:
    runs-on: ubuntu-18.04
    steps:
      - name: checkout
        uses: actions/checkout@v1
      - uses: docker://alpine
        with:
          args: env
      - uses: docker://alpine
        name: alpine workspace
        with:
          args: ls -lat

  show-env-alpine:
    runs-on: self-hosted
    steps:
      - name: checkout
        uses: actions/checkout@v1
      - uses: docker://alpine
        with:
          args: env
      - uses: docker://alpine
        name: alpine workspace
        with:
          args: ls -lat

It looked normal in github-hosted runner:

/usr/bin/docker run --name alpine_b1c9fa --label 671ee6 --workdir /github/workspace --rm -e INPUT_ARGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/action-demo/action-demo":"/github/workspace" alpine ls -lat
total 32
drwxr-xr-x 5 root root 4096 Dec 20 09:30 ..
drwxr-xr-x 6 1001 115 4096 Dec 20 09:30 .
drwxr-xr-x 8 1001 115 4096 Dec 20 09:30 .git
drwxr-xr-x 3 1001 115 4096 Dec 20 09:30 .github
-rw-r--r-- 1 1001 115 12 Dec 20 09:30 .gitignore
-rw-r--r-- 1 1001 115 230 Dec 20 09:30 README.md
drwxr-xr-x 3 1001 115 4096 Dec 20 09:30 docker
drwxr-xr-x 4 1001 115 4096 Dec 20 09:30 echo-box

But in my self-hosted runner shows:

/usr/bin/docker run --name alpine_ad7bfc --label b1cbc5 --workdir /github/workspace --rm -e INPUT_ARGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/_work/_temp/_github_home":"/github/home" -v "/home/runner/_work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/_work/action-demo/action-demo":"/github/workspace" alpine ls -lat
total 4
drwxr-xr-x 5 root root 4096 Dec 20 09:30 ..
drwxr-xr-x 2 root root 40 Dec 20 09:30 .

more logs in: https://github.com/swaglive/action-demo/runs/357868106

I also tried to use a dockerfile in repo for action:

uses: ./action/demo

It’s can build image correctly but it also can’t mount workspace correctly.

It also looked correct in container’s workspace.

runner@2fdf56bf699d:~$ ls -lat /home/runner/_work/action-demo/action-demo
total 32
drwxr-xr-x 6 runner runner 4096 Dec 20 09:30 .
drwxr-xr-x 8 runner runner 4096 Dec 20 09:30 .git
drwxr-xr-x 3 runner runner 4096 Dec 20 09:30 .github
-rw-r--r-- 1 runner runner 12 Dec 20 09:30 .gitignore
-rw-r--r-- 1 runner runner 230 Dec 20 09:30 README.md
drwxr-xr-x 3 runner runner 4096 Dec 20 09:30 docker
drwxr-xr-x 4 runner runner 4096 Dec 20 09:30 echo-box
drwxr-xr-x 3 runner runner 4096 Dec 20 09:30 ..

According to your self-hosted runner logs, it tried to map /home/runner/_work/action-demo/action-demo to /github/workspace

/usr/bin/docker run --name alpine_ad7bfc --label b1cbc5 --workdir /github/workspace --rm -e INPUT_ARGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/_work/_temp/_github_home":"/github/home" -v "/home/runner/_work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/_work/action-demo/action-demo":"/github/workspace" alpine ls -lat

In default, /home/runner/_work/repo_name/repo_name is the workspace on hosted runner. When you set up self-hosted runner, What’s the self-hosted runner working folder?

Can you enable step debug logging and run your self-hosted job again, please share your debug logs here.

> What’s the self-hosted runner working folder?

It’s /home/runner

I wrote working folder with: https://github.com/swaglive/docker-github-actions-runner/blob/master/Dockerfile

WORKDIR /home/runner

...
USER runner

and I validated it with this command:

docker run -it --rm \
    --entrypoint /bin/bash \
    --name runner swaglive/github-runner

step debug log: https://github.com/swaglive/action-demo/runs/361798031

##[debug]Evaluating condition for step: 'Run alpine'
##[debug]Parsing expression: <success()>
##[debug]Evaluating: success()
##[debug]Evaluating success:
##[debug]=> true
##[debug]Result: true
##[debug]Starting: Run alpine
##[debug]Loading inputs
##[debug]Loading env
Run docker://alpine
  with:
    args: sh -c "pwd && env"
  env:
    ACTIONS_STEP_DEBUG: true
/usr/bin/docker run --name alpine_c7aa97 --label b1cbc5 --workdir /github/workspace --rm -e ACTIONS_STEP_DEBUG -e INPUT_ARGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/_work/_temp/_github_home":"/github/home" -v "/home/runner/_work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/_work/action-demo/action-demo":"/github/workspace" alpine sh -c "pwd && env"
/github/workspace
ACTIONS_RUNTIME_TOKEN=***
HOSTNAME=940e7c0f6835
SHLVL=1
HOME=/github/home
RUNNER_TEMP=/home/runner/_work/_temp
GITHUB_EVENT_PATH=/github/workflow/event.json
GITHUB_HEAD_REF=
INPUT_ARGS=sh -c "pwd && env"
RUNNER_OS=Linux
  alpine workspace2s
##[debug]Finishing: alpine workspace
##[debug]Evaluating condition for step: 'alpine workspace'
##[debug]Parsing expression: <success()>
##[debug]Evaluating: success()
##[debug]Evaluating success:
##[debug]=> true
##[debug]Result: true
##[debug]Starting: alpine workspace
##[debug]Loading inputs
##[debug]Loading env
Run docker://alpine
  with:
    args: ls -lat
  env:
    ACTIONS_STEP_DEBUG: true
/usr/bin/docker run --name alpine_8549f7 --label b1cbc5 --workdir /github/workspace --rm -e ACTIONS_STEP_DEBUG -e INPUT_ARGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/_work/_temp/_github_home":"/github/home" -v "/home/runner/_work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/_work/action-demo/action-demo":"/github/workspace" alpine ls -lat
total 4
drwxr-xr-x 5 root root 4096 Dec 24 02:38 ..
drwxr-xr-x 2 root root 40 Dec 20 09:30 .
##[debug]Finishing: alpine workspace

In your scenario, you are trying to set up a self-hosted runner in docker container using this https://github.com/swaglive/docker-github-actions-runner/blob/master/Dockerfile.

The problem is that when execute docker run command in container, the volumes could not work well.   
Can you remote to the machine which run your container? And try to execute docker run commands in your container? Will the volume work?

Or is it possible to install self-hosted runner in your local machine? I tested in my side, I install a self-hosted runner on my local Ubuntu machine, and install docker on that machine. The workspace is correct:

> Can you remote to the machine which run your container? And try to execute docker run commands in your container? Will the volume work?

Wow. Volume can’t work.

I execute this commands in my container:

runner@21d4e18072f4:~$ docker run -it --rm -v ~/_work:/_work alpine sh
/ # ls -lat _work/action-demo/action-demo/
total 0
drwxr-xr-x 2 root root 40 Dec 20 09:30 .
drwxr-xr-x 3 root root 60 Dec 20 09:30 ..

I feel it like docker-in-docker issue: https://serverfault.com/questions/819369/mounting-a-volume-with-docker-in-docker

Thank you for your confirmation. I would recommend you to install self-hosted runner in machine instead of inside docker container.

> Thank you for your confirmation. I would recommend you to install self-hosted runner in machine instead of inside docker container.

I see your point, but I need to deploy runner inside kubernetes. It’s also easier to scale out and more portable. Thanks for your suggestion.

@rammusxu what did you end up doing? I’m having a similar issue with running self-hosted runners inside a K8s cluster and docker based actions like https://github.com/qazz92/kubectl have empty workspaces so I can’t create deployments I can only patch them with set image

@rammusxu @aqabawe

The problem is permissions. When you run docker in docker you are essentially only running docker on a main host. You then mount /var/run/docker.sock from host to /var/run/docker.sock on the github actions runner docker container. This will allow you to run docker commands in the runner that is running in docker. However, you will still run into issues with the workspace folder. I basically had to, in addition to mapping /var/run/docker.sock, do the following on the host machine:

useradd -m -u 1111 actions
mkdir -p /home/actions/actions-runner/_work
chown -R actions:actions /home/actions

And then in the docker image make sure the runner is running as actions user with guid 1111 and mount the following: /home/actions/actions-runner/_work:/home/actions/actions-runner/_work

Remember to run sudo chown -R actions:actions . because most docker containers run as root and any file changes that are made will be set to root:root and the runner will not be able to do anything.

Hope that helps.