Skip to content

Commit 65856e9

Browse files
Fix docker build user (#35)
* use fixuid to dynamically map user/group permissions at runtime when the container is started * update readme * set username of docker run script as developer * remove some packages from apt list as those are installed via apt or come in ros * remove unused apt package python3-pytest * Address comments --------- Co-authored-by: JesusSilvaUtrera <jesus.silva@ekumenlabs.com>
1 parent 2971dee commit 65856e9

6 files changed

Lines changed: 50 additions & 31 deletions

File tree

docker/Dockerfile

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,49 @@ RUN apt-get update && apt-get install -y --no-install-recommends python3-pip &&
2020
# Initialize rosdep as root. Then regular users can rosdep update
2121
RUN rosdep init
2222

23-
# Take ownership of the existing non-root user (ID 1000)
24-
ARG USERNAME=ros2
25-
ARG USER_UID=1000
26-
ARG USER_GID=1000
27-
28-
# The Ubuntu 24 base image has a default user 'ubuntu' at UID/GID 1000.
29-
# We modify this user to match the host user's name.
30-
RUN apt-get update && apt-get install -y sudo \
31-
&& groupmod --gid $USER_GID --new-name $USERNAME ubuntu \
32-
&& usermod --uid $USER_UID --gid $USER_GID --login $USERNAME --home /home/$USERNAME --move-home ubuntu \
33-
&& echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME \
34-
&& chmod 0440 /etc/sudoers.d/$USERNAME
23+
ARG USER=developer
24+
ARG GROUP=roscon
25+
26+
RUN deluser ubuntu
27+
28+
RUN addgroup --gid 1001 $GROUP \
29+
&& adduser --uid 1001 --ingroup $GROUP --home /home/$USER --shell /bin/bash --disabled-password --gecos "" $USER \
30+
&& adduser $USER sudo \
31+
&& echo "$USER ALL=NOPASSWD: ALL" > /etc/sudoers.d/$USER \
32+
&& chmod 0440 /etc/sudoers.d/$USER
33+
34+
COPY docker/fixuid_config.yml /etc/fixuid/config.yml
35+
RUN /bin/bash -c '\
36+
ARCH=`uname -m` && if [ "$ARCH" == "aarch64" ]; then FIXUID_ARCH="arm64"; else FIXUID_ARCH="amd64"; fi \
37+
&& curl -SsL https://github.com/boxboat/fixuid/releases/download/v0.6.0/fixuid-0.6.0-linux-$FIXUID_ARCH.tar.gz | tar -C /usr/local/bin -xzf - \
38+
&& chmod 4755 /usr/local/bin/fixuid \
39+
&& cd /etc/fixuid \
40+
&& sed -i "s/_USER_/$USER/" config.yml \
41+
&& sed -i "s/_GROUP_/$GROUP/" config.yml'
3542

3643
# Worspace and work directory
37-
ENV WS=/home/${USERNAME}/ws
38-
RUN mkdir -p ${WS} && chown -R ${USER_UID}:${USER_GID} ${WS}
44+
ENV WS=/home/${USER}/ws
45+
RUN mkdir -p ${WS} && chown -R ${USER}:${GROUP} ${WS}
3946
WORKDIR ${WS}
4047

4148
# Copy only package manifests first so this layer is cacheable even if source changes
4249
# The .dockerignore file excludes all source code so this layer stays small
4350
# and only rebuilds when manifests change.
44-
COPY --chown=${USERNAME}:${USERNAME} modules/ ${WS}/src/
51+
COPY --chown=${USER}:${GROUP} modules/ ${WS}/src/
4552

4653
# Set the new user
47-
USER $USERNAME
54+
USER $USER:$GROUP
4855

4956
# Install dependencies via rosdep
5057
SHELL ["/bin/bash", "-c"]
5158
RUN source /opt/ros/$ROS_DISTRO/setup.bash && \
5259
rosdep update && \
60+
sudo apt-get update && \
5361
rosdep install --from-paths src --ignore-src -r -y
5462

5563
# Source ROS 2 and colcon autocompletion on container startup
5664
RUN echo "source /opt/ros/$ROS_DISTRO/setup.bash" >> ~/.bashrc \
5765
&& echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
5866

59-
ENTRYPOINT ["/ros_entrypoint.sh"]
67+
ENTRYPOINT ["fixuid", "-q", "/ros_entrypoint.sh"]
6068
CMD ["bash"]

docker/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ It includes development tools, linters, Google Test/Mock, and all the necessary
66

77
## 🚀 Building the Image and Running the Container
88

9+
You can either build the **image locally** or pull it directly from **DockerHub**.
10+
11+
### Pull from DockerHub (recommended)
12+
13+
The prebuilt image is available at:
14+
15+
```bash
16+
docker pull ekumenlabs/ros2-testing-workshop-roscon-es-25:jazzy
17+
```
18+
19+
This is the easiest and fastest option.
20+
21+
### Build the Image Locally
22+
923
To build the image, use the [build.sh](./build.sh) script provided in this repository:
1024

1125
```bash
@@ -25,6 +39,8 @@ It's possible to get information about the available flags for both scripts usin
2539
./docker/run.sh --help
2640
```
2741

42+
## 📦 Container Usage
43+
2844
Once the container is started, the entrypoint is located at the root of the ROS 2 workspace (~/ws).
2945

3046
After shutdown, the user will be asked whether to save the changes made inside the container or not to the image (performing a `docker commit` internally).
@@ -37,5 +53,3 @@ docker exec -it ros2-testing-workshop-roscon-es-25-container bash
3753

3854
> [!NOTE]
3955
> The name of the container might differ if a different name is provided in the script, so be careful about that.
40-
41-
<!-- TODO: Add Troubleshoot section? Common problems? -->

docker/apt_packages.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ python3-colcon-common-extensions
66
python3-rosdep
77
python3-vcstool
88

9-
# === Linters and formatters ===
10-
clang-format
11-
python3-pytest
12-
uncrustify
13-
14-
# === Google Test (for C++ unit tests) ===
15-
libgtest-dev
16-
179
# === Quality of life tools ===
1810
curl
1911
wget
2012
nano
13+
vim
14+
sudo

docker/build.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ fi
6262
echo "Building Docker image: ${IMAGE_NAME}:${TAG}"
6363
docker build \
6464
--platform "${PLATFORM}" \
65-
--build-arg USERNAME="$(whoami)" \
66-
--build-arg USER_UID="$(id -u)" \
67-
--build-arg USER_GID="$(id -g)" \
6865
-f "${DOCKERFILE_PATH}" \
6966
-t "${IMAGE_NAME}:${TAG}" \
7067
"${CONTEXT_FOLDER_PATH}"

docker/fixuid_config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
user: _USER_
2+
group: _GROUP_
3+
paths:
4+
- /
5+
- /home/_USER_/.ccache

docker/run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -e
33

44
# Configuration
5-
USERNAME=$(whoami)
5+
USERNAME=developer
66
IMAGE_NAME="ekumenlabs/ros2-testing-workshop-roscon-es-25"
77
ROS_DISTRO="jazzy"
88
TAG="${ROS_DISTRO}"
@@ -113,6 +113,7 @@ xhost +
113113

114114
# Run the container
115115
docker run -it \
116+
--user=$(id -u):$(id -g) \
116117
--net=host \
117118
${NVIDIA_FLAGS} \
118119
--name "${CONTAINER_NAME}" \

0 commit comments

Comments
 (0)