Skip to content

Instantly share code, notes, and snippets.

@qizhihere
Created June 28, 2016 14:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qizhihere/c8db099c673e2f4718418ca05c9b2767 to your computer and use it in GitHub Desktop.
Save qizhihere/c8db099c673e2f4718418ca05c9b2767 to your computer and use it in GitHub Desktop.
Run a process in a docker container with specific uid and gid. Take dropbox for example.
#!/usr/bin/env sh
has_gid () {
cut -d: -f1,4 /etc/passwd | grep -q "^${1}:${2}" || return 1
}
# example: ensure_user btsync 1000 1000
ensure_user () {
local user=$1
local uid=$2
local gid=$3
local group_prefix=${4:-_}
{
deluser $user
delgroup $user
delgroup $group_prefix$user
} 2>/dev/null
adduser -h / -s /bin/sh -D -u $uid $user
has_gid $user $gid || {
addgroup -g $gid $group_prefix$user
sed -i 's/^\('$user'\(:[^:]\{1,\}\)\{2\}\):[0-9]\{1,\}/\1:'$gid/ /etc/passwd
}
}
ensure_user dropbox $UID $GID
chown -R $UID:$GID /.dropbox-dist /Dropbox
exec su dropbox -c "exec $*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment