Skip to content

A Github Action which fixes ownership issues with files and directories after docker action usage.

License

Notifications You must be signed in to change notification settings

xanantis/docker-file-ownership-fix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Docker File Ownership Fix

This action fixes ownership issues with files and directories after docker action usage. Use as last resort only!

This action does next:

  • Moves /usr/bin/docker to /usr/bin/docker___
  • Moves "wrapper" to /usr/bin/docker
  • Wrapper prepends docker options with --user "$DOCKER_USER_OPTION" when used with commands:
    • create
    • run
    • exec
  • Wrapper doesn't change options with any other commands
  • Skips if docker is already wrapped.
  • Wrapper will expand environment variables: $USER, $UID, $GID. It substitutes them with $(id -u) or/and $(id -g) when those variables are empty.

WARNING! For a self-hosted runner, this change is permanent. But, you can revert them, simply by moving /usr/bin/docker___ back to /usr/bin/docker.

Example usage

Use this action at the beginning of every job that uses docker actions.

name: CI

on: [push]

env:
  DOCKER_USER_OPTION: '$UID:$GID'
  #DOCKER_USER_OPTION: '$USER:$GID'
  #DOCKER_USER_OPTION: '$USER'
  #DOCKER_USER_OPTION: '$UID'
  #DOCKER_USER_OPTION: '0:0'
  #DOCKER_USER_OPTION: '1000:1000'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: xanantis/docker-file-ownership-fix@v1
      - uses: ghost-actions/docker-action@v1
      - uses: ghost-actions/docker-action-root@v1
        env:
          DOCKER_USER_OPTION: '0:0'
    # ..........

To fix issues with containers

Set option "--user " with the right values in jobs.<job_id>.container.options

Read more at docs.github.com

To fix issues with services

Set option "--user " with the right values in jobs.<job_id>.services.options. Similar to the fix above.

Read more at docs.github.com

To fix all those above for self-hosted runner

Create a file "docker" with this content:

#!/bin/bash


command="$1"
args=( "$@" )


if [ "$command" = "create" ] || [ "$command" = "exec" ] || [ "$command" = "run" ]
then
  if [ ! -z "$DOCKER_USER_OPTION" ]; then
    docker_user=${DOCKER_USER_OPTION%:*}
    docker_group=${DOCKER_USER_OPTION#*:}

    if [ "$docker_user" = "$docker_group" ]; then
      docker_group=""
    fi

    if [ "$docker_user" = "\$USER" ]; then
      docker_user=`pick_one "$USER" $(id -u)`
    elif [ "$docker_user" = "\$UID" ]; then
      docker_user=`pick_one "$UID" $(id -u)`
    fi

    if [ "$docker_group" = "\$GID" ]; then
      docker_group=`pick_one "$GID" $(id -g)`
    fi

    DOCKER_USER_OPTION="$docker_user"

    if [ ! -z "$docker_group" ]; then
      DOCKER_USER_OPTION+=":$docker_group"
    fi

    args=( "${args[@]:0:1}" "--user" "$DOCKER_USER_OPTION" "${args[@]:1}" )

  fi

fi

script_path=$(dirname $(realpath -s $0))
script_path=${script_path%/}
new_path=
export IFS=":"
for dir in $PATH; do
    if [ "${dir%/}" != "$script_path" ]
    then
	new_path="$new_path:$dir"
    fi
done

new_path="${new_path:1}"


"$(PATH="$new_path" which docker)" "${args[@]}"
  • Add docker to /home/ACTIONS_RUNNER_USER/bin

  • Add this line to /home/ACTIONS_RUNNER_USER/.bashrc export PATH=/home/ACTIONS_RUNNER_USER/bin:$PATH

  • Prepend Path in file ACTIONS_RUNNER/.path with /home/ACTIONS_RUNNER_USER/bin:

  • Restart actions runner. Make sure that it uses the correct $PATH environment variable

  • Add $DOCKER_USER_OPTION to your workflow, job or step. or event container and service

  • Optionally, Add DOCKER_USER_OPTION="$UID" to the runner .env file to set value by default

About

A Github Action which fixes ownership issues with files and directories after docker action usage.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages