Skip to content

Instantly share code, notes, and snippets.

@nitrobin
Last active April 22, 2019 18:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nitrobin/4d16fbe347c150a422ad to your computer and use it in GitHub Desktop.
Save nitrobin/4d16fbe347c150a422ad to your computer and use it in GitHub Desktop.
# http://wiki.ros.org/docker/Tutorials/GUI
# mount /etc/group and /etc/passwd read only
# set user from $USER
docker run -ti --rm -v $(pwd):/tmp/hx -w /tmp/hx -v /etc/group:/etc/group:ro -v /etc/passwd:/etc/passwd:ro —user=$USER debian:jessie
@Graham42
Copy link

Graham42 commented Sep 6, 2017

With newer versions of docker volumes don't get mounted until after the user is assumed. However, if you use -u=$UID instead of --user=$USER it will work.

If you want to match the group id too you can do -u=$UID:$(id -g $USER)

@gzm55
Copy link

gzm55 commented Nov 16, 2017

we can use --grouo-add

#!/bin/sh

set -e
test ":$DEBUG" != :true || set -x

# set image
set -- debian:jessie "$@"

# use current user and its groups at host
for v in /etc/group /etc/passwd; do
  [ ! -r "$v" ] || set -- -v $v:$v:ro "$@"
done
set -- --user "`id -u`:`id -g`" "$@"
for g in `id -G`; do
  set -- --group-add "$g" "$@"
done
set -- -v "$HOME":"$HOME" "$@"

exec docker run --rm -it "$@"

@zioalex
Copy link

zioalex commented May 14, 2018

You can even avoid to mount the passwd and group files. The user will be not recognized in the container but outside everything will be fine and however you can run whatever you want.

@anandbhaskaran
Copy link

-u=$UID:$(id -g $USER)

I am getting I have no name! as the username inside docker :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment