I'm currently experimenting on Docker specifically on the aspect where a user inside given container affects the Docker host itself. Ultimately, I want the user to be able to run containers on its very own host from inside its container. It kind of sounds a little absurd, but I'm hoping it works.
At the moment, I'm looking for a way for that given user to be part of the Docker host's docker group. Is there a way to do that?
From the perspective of the Docker host, any users inside the container are treated exactly the same as a user outside the container with the same UID (not the same name!), regardless of whether the UID is actually in use on the host. Unfortunately, it appears that only users with a username can belong to groups, so you can't just add the UID to the group. Instead, you need to add the host user with the same UID to the group (or create a user with that UID if one doesn't exist).
I wrote a bash script to generate dockerfiles on the fly with the current user added on the container and the docker group. Here is the meat of it:
# get the current user's info
user=$USER
uid=`id -u $user`
gid=`id -g $user`
# get gid for group docker
RESULT=$(id -g docker 2>&1)
if [ 1 -ne $? ] ; then
docker_gid=$RESULT
fi
echo FROM ubuntu:14.04
echo MAINTAINER Nobody "no-reply@nothere.com"
echo USER root
echo "RUN apt-get update && apt-get -y install docker-engine"
echo "RUN /bin/bash -c 'getent passwd ${user} || \
adduser --system --gid ${gid} ${docker_group} --uid ${uid} --shell /bin/bash ${user} || \
usermod -l ${user} \$(getent passwd ${uid} | cut -d: -f1)'"
if [ -n "$docker_gid" ] ; then
echo "RUN /bin/bash -c 'getent group docker && groupmod --gid ${docker_gid} docker \
|| groupadd --gid ${docker_gid} docker'"
echo "RUN /bin/bash -c 'groups ${user} | grep docker || usermod --groups ${docker_gid} ${user}'"
fi
HTH!