Skip to content

Instantly share code, notes, and snippets.

@luochen1990
Last active March 8, 2022 16:26
Show Gist options
  • Save luochen1990/45dd0f40fb3fb8b68b077988def74d28 to your computer and use it in GitHub Desktop.
Save luochen1990/45dd0f40fb3fb8b68b077988def74d28 to your computer and use it in GitHub Desktop.
Docker devbox for NixOS users who want a FHS linux environment
#!/usr/bin/env bash
#Ref: https://vsupalov.com/docker-shared-permissions/
# https://stackoverflow.com/questions/27701930/how-to-add-users-to-docker-container
# https://askubuntu.com/questions/420784/what-do-the-disabled-login-and-gecos-options-of-adduser-command-stand
if [ $# == 0 ]; then
echo "usage: devbox {session-name}"
else
nix-shell -p xorg.xhost --run 'xhost +local:docker' > /dev/null # for GUI apps
session_name=$1
home=/home/$(id -un)
sh="export PATH=\$PATH:$home/.nix-profile/bin/:/run/current-system/sw/bin/; zsh"
docker run \
--mount type=bind,source=/nix/,target=/nix/,readonly \
--mount type=bind,source=/run/current-system/sw/,target=/run/current-system/sw/,readonly \
--mount type=bind,source=$home/ws/,target=$home/ws \
--mount type=bind,source=$home/.config/,target=$home/.config,readonly \
--mount type=bind,source=$home/.profile,target=$home/.profile,readonly \
--mount type=bind,source=$home/.zshrc,target=$home/.zshrc,readonly \
--mount type=bind,source=$home/.antigen/,target=$home/.antigen/,readonly \
--mount type=bind,source=$home/.vimrc,target=$home/.vimrc,readonly \
--mount type=bind,source=$home/.gitconfig,target=$home/.gitconfig,readonly \
--mount type=bind,source=$home/.ssh/,target=$home/.ssh/,readonly \
--env "DISPLAY" \
--env "NIX_PATH" \
--mount type=bind,source=$home/.Xauthority,target=$home/.Xauthority,readonly \
--mount type=bind,source=/tmp/.X11-unix,target=/tmp/.X11-unix,readonly \
--user $(id -u):$(id -g) \
-v /etc/passwd:/etc/passwd \
--name $session_name --hostname devbox --net host --privileged \
-it devbox -c "$sh" > /dev/null \
|| ( docker start $session_name > /dev/null && docker exec -it $session_name sh -c "$sh" )
fi
#!/usr/bin/env -S bash -c 'docker build -t devbox --build-arg USER_ID=$(id -u) --build-arg USER_NAME=$(id -un) --build-arg GROUP_ID=$(id -g) --build-arg GROUP_NAME=$(id -gn) $(realpath $(dirname $0))'
FROM ubuntu:20.04
ARG USER_ID
ARG USER_NAME
ARG GROUP_ID
ARG GROUP_NAME
RUN addgroup --gid $GROUP_ID $GROUP_NAME; exit 0
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID $USER_NAME
#COPY ./sources.list /etc/apt/sources.list
RUN apt-get update
USER $USER_NAME
WORKDIR /home/$USER_NAME
ENTRYPOINT [ "/bin/bash" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment