From b58a99e1921c4bf00849a146e5f37ccc468f9778 Mon Sep 17 00:00:00 2001 From: texhnolyze Date: Thu, 16 May 2024 19:09:18 +0200 Subject: [PATCH] feat(devcontainer): use non root user `bitbots` to prevent issues when interacting with the repository both from within the container and outside the container, due to permissions not being correct --- .devcontainer/Dockerfile | 23 +++++++++++++++++------ .devcontainer/devcontainer.json | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index f45a47088..5aee3c9db 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,5 +1,9 @@ FROM ros:iron +ARG user=bitbots +ARG uid=1000 +ARG gid=1000 + # Basic Utilities ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update -y \ @@ -78,18 +82,25 @@ RUN python3 -m pip install \ # Set zsh as default shell SHELL ["/bin/zsh", "-c"] -# Create home directory and colcon workspace -RUN mkdir -p "/root/colcon_ws" +# Create user bitbots with home directory and add to sudo group +RUN useradd -m -U -u "$uid" -G sudo -s /bin/zsh $user \ + && groupmod -g "$gid" $user \ + && echo "$user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +USER $user + +# Create colcon workspace +RUN mkdir -p /home/$user/colcon_ws/src # Install oh-my-zsh for pretty terminal RUN sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" && \ - git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions + git clone https://github.com/zsh-users/zsh-autosuggestions "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions" -# Add zshrc -COPY zshrc "/root/.zshrc" +# Add zshrc to bitbots home directory +COPY --chown=$user:$user zshrc /home/$user/.zshrc # This is required for sharing Xauthority ENV QT_X11_NO_MITSHM=1 # Switch to the workspace directory -WORKDIR "/root/colcon_ws" +WORKDIR /home/$user/colcon_ws diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d7c87f452..fe8c55aa6 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -20,8 +20,8 @@ } }, - "workspaceMount": "type=bind,source=${localWorkspaceFolder},target=/root/colcon_ws/src/bitbots_main", - "workspaceFolder": "/root/colcon_ws/src/bitbots_main", + "workspaceMount": "type=bind,source=${localWorkspaceFolder},target=/home/bitbots/colcon_ws/src/bitbots_main", + "workspaceFolder": "/home/bitbots/colcon_ws/src/bitbots_main", "mounts": [ "type=bind,source=${localEnv:HOME},target=/srv/host_home,consistency=cached"