From b5e81fd4d9b18a77d80ee87ac8d70e47cdec6ae2 Mon Sep 17 00:00:00 2001 From: Griswald Brooks Date: Fri, 8 Sep 2023 21:41:17 +0000 Subject: [PATCH] Separate UID and GID and fix ccache --- Dockerfile | 54 ++++++++++++++++++++++++++++++++++++------------- README.md | 2 +- compose.dev.yml | 6 +++--- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index eb5cc36..8e02f98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,31 +12,22 @@ ARG DEBIAN_FRONTEND=noninteractive RUN apt-get update \ && apt-get install -q -y --no-install-recommends \ apt-utils \ - ccache \ clang \ - clang-format \ - clang-tidy \ cmake \ git \ lld \ llvm \ - python3-colcon-common-extensions \ python3-colcon-mixin \ - python3-pip \ python3-colcon-common-extensions \ python3-colcon-lcov-result \ python3-colcon-coveragepy-result \ python3-colcon-mixin \ + python3-pip \ python3-rosdep \ python3-vcstool \ - vim \ wget \ - ssh-client \ && rm -rf /var/lib/apt/lists/* -# install some pip packages needed for testing -RUN python3 -m pip install -U \ - pre-commit # copy source to install repo dependencies WORKDIR /ws @@ -51,12 +42,47 @@ RUN rosdep update && apt-get update \ FROM upstream AS development -ARG UIDGID +ARG UID +ARG GID ARG USER # fail build if args are missing -RUN if [ -z "$UIDGID" ]; then echo '\nERROR: UIDGID not set. Run \n\n \texport UIDGID=$(id -u):$(id -g) \n\n on host before building Dockerfile.\n'; exit 1; fi +# hadolint ignore=SC2028 +RUN if [ -z "$UID" ]; then echo '\nERROR: UID not set. Run \n\n \texport UID=$(id -u) \n\n on host before building Dockerfile.\n'; exit 1; fi +# hadolint ignore=SC2028 +RUN if [ -z "$GID" ]; then echo '\nERROR: GID not set. Run \n\n \texport GID=$(id -g) \n\n on host before building Dockerfile.\n'; exit 1; fi +# hadolint ignore=SC2028 RUN if [ -z "$USER" ]; then echo '\nERROR: USER not set. Run \n\n \texport USER=$(whoami) \n\n on host before building Dockerfile.\n'; exit 1; fi -# chown working directory to user -RUN mkdir -p /home/${USER}/ws && chown -R ${UIDGID} /home/${USER} +# install developer tools +RUN --mount=type=cache,target=/var/cache/apt,id=apt \ + apt-get update && apt-get upgrade -y \ + && apt-get install -q -y --no-install-recommends \ + ccache \ + clang-format \ + clang-tidy \ + git \ + openssh-client \ + vim \ + wget \ + && rm -rf /var/lib/apt/lists/* + +RUN python3 -m pip install --no-cache-dir \ + pre-commit==3.0.4 + +# Setup user home directory +# --no-log-init helps with excessively long UIDs +RUN groupadd --gid $GID $USER \ + && useradd --no-log-init --uid $GID --gid $UID -m $USER --groups sudo \ + && echo $USER ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USER \ + && chmod 0440 /etc/sudoers.d/$USER \ + && echo "source /opt/ros/${ROS_DISTRO}/setup.bash" >> /home/${USER}/.profile \ + && touch /home/${USER}/.bashrc \ + && chown -R ${GID}:${UID} /home/${USER} + +USER $USER +ENV SHELL /bin/bash +ENTRYPOINT [] + +# Setup mixin +WORKDIR /home/${USER}/ws diff --git a/README.md b/README.md index 05f695a..3c40656 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Build a new development image ``` source .env mkdir -p ~/.${REPO}/ccache -export UIDGID=$(id -u):$(id -g); docker compose -f compose.dev.yml build +export UID=$(id -u) export GID=$(id -g); docker compose -f compose.dev.yml build ``` Start an interactive development container ``` diff --git a/compose.dev.yml b/compose.dev.yml index 036f1a3..1688935 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -4,7 +4,8 @@ services: development: build: args: - UIDGID: ${UIDGID} + UID: ${UID} + GID: ${GID} USER: ${USER} REPO: ${REPO} context: . @@ -13,6 +14,7 @@ services: colcon mixin add default https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml && colcon mixin update && export PATH="/usr/lib/ccache:$PATH" && + source /opt/ros/humble/setup.bash && bash" container_name: ${USER}-${REPO}-dev environment: @@ -26,14 +28,12 @@ services: image: ${REPO}-dev:latest network_mode: host privileged: true - user: ${UIDGID} volumes: - ~/.ssh:${HOME}/.ssh:ro - ~/.gitconfig:${HOME}/.gitconfig:ro - ${PWD}:${HOME}/ws/src/${REPO} - ~/.${REPO}/ccache:${HOME}/.ccache - /tmp/.X11-unix:/tmp/.X11-unix:ro - - ${XDG_RUNTIME_DIR}:${XDG_RUNTIME_DIR}:ro - /etc/group:/etc/group:ro - /etc/passwd:/etc/passwd:ro - /etc/shadow:/etc/shadow:ro