improvement: Add user in container (not image)

This way the image isn't tethered to a particular user
This commit is contained in:
John Colvin committed 2023-03-26 13:43:48 -04:00
1 parent f3a7217661
commit e30e3164cb
3 files changed
+144 -28

No files matched your search

-19
View File
@@ -87,22 +87,3 @@ RUN \
ln -s /usr/bin/ccache /usr/local/bin/g++-12
RUN locale-gen en_US.UTF-8
ARG docker_user
ARG docker_password
RUN \
useradd -m $docker_user \
&& echo "${docker_user}:${docker_password}" | chpasswd \
&& adduser $docker_user sudo
RUN USER=$docker_user && \
GROUP=$docker_user && \
curl -SsL https://github.com/boxboat/fixuid/releases/download/v0.5.1/fixuid-0.5.1-linux-amd64.tar.gz | tar -C /usr/local/bin -xzf - && \
chown root:root /usr/local/bin/fixuid && \
chmod 4755 /usr/local/bin/fixuid && \
mkdir -p /etc/fixuid && \
printf "user: $USER\ngroup: $GROUP\n" > /etc/fixuid/config.yml
USER $docker_user:$docker_user
ENTRYPOINT ["fixuid"]
+64
View File
@@ -0,0 +1,64 @@
#!/usr/bin/env bash
function print_help_and_exit() {
cat <<EOF
Open a shell inside the docker container
Usage: docker_shell.sh [OPTIONS] [CONTAINER]
Options:
-u,--user The user to login with in the container (defaults to the current user)
-c,--container The container name (may also be specified as positional arg)
-h,--help Print this help message and exit
EOF
exit "$1"
}
declare docker_user=""
declare cont_name=""
while [[ $# -gt 0 ]]; do
case "$1" in
-u)
;&
--user)
if [[ -z "$2" ]]; then
echo "User (-u,--user) must be non-empty"
print_help_and_exit 1
fi
docker_user="$2"
shift 2
;;
-h)
;&
--help)
print_help_and_exit 0
;;
-c)
;&
--container)
shift 1
;&
*)
if [[ ! -z "$cont_name" ]]; then
echo "Unexpected argument '$1'"
print_help_and_exit 1
fi
cont_name="$1"
shift 1
;;
esac
done
if [[ -z "$cont_name" ]]; then
echo "Please specify the container ([-c,--container])"
print_help_and_exit 1
fi
if [[ -z "$docker_user" ]]; then
docker_user="$USER"
fi
docker exec -u "$docker_user" -it "$cont_name" bash
+80 -9
View File
@@ -3,11 +3,17 @@
function print_help_and_exit() {
cat <<EOF
Start a docker dev container
Usage: start_docker.sh [OPTIONS]
Usage: start_docker.sh [OPTIONS] image container
Positionals:
image The image to instantiate
container The name for the container
Options:
-u,--user The user (name) to add to the image (defaults to the current user)
-p,--pass The password for the <--user> in the image (defaults to <--user>)
-i,--image The image to instantiate
-c,--container The name to give the container
-c,--container The name for the container
-m,--mount The name for the folder (on the host) that is mounted to container's 'home' dir
-h,--help Print this help message and exit
@@ -16,17 +22,43 @@ EOF
}
declare docker_user=""
declare docker_pass=""
declare image_name=""
declare cont_name=""
declare mount_name=""
while [[ $# -gt 0 ]]; do
case "$1" in
-u)
;&
--user)
if [[ -z "$2" ]]; then
echo "User (-u,--user) must be non-empty"
print_help_and_exit 1
fi
docker_user="$2"
shift 2
;;
-p)
;&
--pass)
if [[ -z "$2" ]]; then
echo "Password (-p,--pass) must be non-empty"
print_help_and_exit 1
fi
docker_pass="$2"
shift 2
;;
-i)
;&
--image)
if [[ -z "$2" ]]; then
if [[ -n "$image_name" ]]; then
echo "Unexpected argument '$1'"
print_help_and_exit 1
elif [[ -z "$2" ]]; then
echo "Image (-i,--image) must be non-empty"
print_help_and_exit 1
fi
image_name="$2"
shift 2
@@ -34,8 +66,12 @@ while [[ $# -gt 0 ]]; do
-c)
;&
--container)
if [[ -z "$2" ]]; then
if [[ -n "$cont_name" ]]; then
echo "Unexpected argument '$1'"
print_help_and_exit 1
elif [[ -z "$2" ]]; then
echo "Container (-c,--container) must be non-empty"
print_help_and_exit 1
fi
cont_name="$2"
shift 2
@@ -55,8 +91,18 @@ while [[ $# -gt 0 ]]; do
print_help_and_exit 0
;;
*)
echo "Unexpected argument '$1'"
print_help_and_exit 1
if [[ -z "$1" ]]; then
echo "Invalid positional argument '$1'"
print_help_and_exit 1
elif [[ -z "$image_name" ]]; then
image_name="$1"
elif [[ -z "$cont_name" ]]; then
cont_name="$1"
else
echo "Unexpected argument '$1'"
print_help_and_exit 1
fi
shift 1
;;
esac
done
@@ -80,6 +126,10 @@ if [[ -z "$mount_name" && -z "$cont_name" ]]; then
print_help_and_exit 1
fi
if [[ -z "$docker_user" ]]; then
docker_user="$USER"
fi
declare home_src=$HOME/.spark-dev/mounts/$mount_name
declare home_tgt=$HOME
declare repo_src=$(git rev-parse --show-superproject-working-tree)
@@ -94,14 +144,15 @@ fi
mkdir -p $home_src
echo "Starting container
echo "Starting container '$cont_name'
image_name : $image_name
cont_name : $cont_name
home_src : $home_src
home_tgt : $home_tgt
repo_src : $repo_src
repo_tgt : $repo_tgt
working dir : $working_dir"
working dir : $working_dir
"
docker run \
--net host \
@@ -112,7 +163,6 @@ docker run \
--name "$cont_name" \
-v "$home_src:$home_tgt" \
-v "$repo_src:$repo_tgt" \
-u $(id -u $USER):$(id -g $USER) \
-w "$working_dir" \
--shm-size=4024m \
-t \
@@ -121,3 +171,24 @@ docker run \
-e DISPLAY="$DISPLAY" \
"$image_name" \
bash
# Add user w/ same uid and gid as the current user outside of docker, so the
# user inside the docker has file permissions in the mounted dir.
declare docker_group="$docker_user"
declare docker_user_uid="$(id -u $USER)"
declare docker_user_gid="$(id -g $USER)"
echo "Adding user in container '$cont_name'
name : $docker_user
uid : $docker_user_uid
gid : $docker_user_gid"
if [[ -z "$docker_pass" ]]; then
docker_pass="$docker_user"
echo "Giving user '$docker_user' a secure password: $docker_pass"
fi
docker exec -u root -it "$cont_name" groupadd -g $docker_user_gid "$docker_group"
docker exec -u root -it "$cont_name" useradd -u $docker_user_uid -g $docker_user_gid "$docker_user"
docker exec -u root -it "$cont_name" usermod --password "$(openssl passwd -1 $docker_pass)" "$docker_user"
docker exec -u root -it "$cont_name" usermod -aG sudo "$docker_user"