Skip to content

Commit f0907e2

Browse files
committed
Use non-root user and group in default for Docker images.
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
1 parent 7c41d9e commit f0907e2

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ https://github.com/fujitatomoya/ros2ai/assets/43395114/78a0799b-40e3-4dc8-99cb-4
3939
see available images for [tomoyafujita/ros2ai@dockerhub](https://hub.docker.com/repository/docker/tomoyafujita/ros2ai/tags?page=1&ordering=last_updated)
4040

4141
```bash
42-
docker run -it --rm --net=host -e OPENAI_API_KEY=$OPENAI_API_KEY tomoyafujita/ros2ai:humble
42+
docker run -it --rm --net=host -e OPENAI_API_KEY=$OPENAI_API_KEY -e OPENAI_ENDPOINT=$OPENAI_ENDPOINT -e OPENAI_MODEL_NAME=$OPENAI_MODEL_NAME tomoyafujita/ros2ai:humble
4343
```
4444

4545
> [!NOTE]

docker/Dockerfile

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# Build:
2-
# docker build --rm -f ./docker/Dockerfile --build-arg="ROS_DISTRO=rolling" --build-arg="COLCON_WS=/root/colcon_ws" -t <user_name>/ros2ai:rolling .
2+
# docker build --rm -f ./docker/Dockerfile --build-arg="ROS_DISTRO=rolling" --build-arg="COLCON_WS=/home/ros2ai/colcon_ws" -t <user_name>/ros2ai:rolling .
33
#
44
# Usage:
55
# docker pull <user_name>/ros2ai:rolling
6+
#
7+
# Note: The container runs as non-root user 'ros2ai' (UID=1000, GID=1000) by default for security.
8+
# You can override the user at runtime with: docker run --user $(id -u):$(id -g) ...
69

710
# An ARG declared before a FROM is outside of a build stage,
8-
# so it cant be used in any instruction after a FROM.
11+
# so it can't be used in any instruction after a FROM.
912
# To use the default value of an ARG declared before the first FROM
1013
# use an ARG instruction without a value inside of a build stage:
1114
ARG ROS_DISTRO=rolling
12-
ARG COLCON_WS=/root/colcon_ws
15+
ARG COLCON_WS=/home/ros2ai/colcon_ws
16+
ARG USERNAME=ros2ai
17+
ARG USER_UID=1234
18+
ARG USER_GID=1234
1319

1420
FROM ros:${ROS_DISTRO}
1521

@@ -19,11 +25,24 @@ LABEL description="ros2ai ${ROS_DISTRO} docker image"
1925

2026
ARG ROS_DISTRO
2127
ARG COLCON_WS
28+
ARG USERNAME
29+
ARG USER_UID
30+
ARG USER_GID
2231

2332
SHELL ["/bin/bash","-c"]
2433

34+
# Create non-root user with sudo privileges
35+
RUN groupadd --gid ${USER_GID} ${USERNAME} \
36+
&& useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME} \
37+
&& apt-get update \
38+
&& apt-get install -y sudo \
39+
&& echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USERNAME} \
40+
&& chmod 0440 /etc/sudoers.d/${USERNAME} \
41+
&& rm -rf /var/lib/apt/lists/*
42+
2543
RUN mkdir -p ${COLCON_WS}/src
2644
COPY . ${COLCON_WS}/src/ros2ai/
45+
RUN chown -R ${USER_UID}:${USER_GID} ${COLCON_WS}
2746

2847
# All apt-get commands start with an update, then install
2948
# and finally, a cache cleanup to keep the image size small.
@@ -47,12 +66,17 @@ RUN if [ "$ROS_DISTRO" = "humble" ]; then \
4766
# Build and source colcon workspace
4867
RUN cd $COLCON_WS \
4968
&& source /opt/ros/$ROS_DISTRO/setup.bash \
50-
&& colcon build --symlink-install --packages-select ros2ai
69+
&& colcon build --symlink-install --packages-select ros2ai \
70+
&& chown -R ${USER_UID}:${USER_GID} $COLCON_WS
5171

5272
# Add source environment in .bashrc
53-
RUN echo -n -e "\n" >> /root/.bashrc
54-
RUN echo "### ros2ai workspace setting" >> /root/.bashrc
55-
RUN echo "cd $COLCON_WS && source ./install/setup.bash" >> /root/.bashrc
73+
RUN echo -n -e "\n" >> /home/${USERNAME}/.bashrc
74+
RUN echo "### ros2ai workspace setting" >> /home/${USERNAME}/.bashrc
75+
RUN echo "cd $COLCON_WS && source ./install/setup.bash" >> /home/${USERNAME}/.bashrc
76+
77+
# Switch to non-root user
78+
USER ${USERNAME}
79+
WORKDIR /home/${USERNAME}
5680

5781
# Overwrite as environmental variable so that entrypoint can rely on those
5882
# OPENAI_API_KEY should not be included here, that is required for the runtime

scripts/docker_release.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
################
1010

1111
DOCKERHUB_USERNAME="${DOCKERHUB_USERNAME:-tomoyafujita}"
12-
COLCON_WS="${COLCON_WS:-/root/colcon_ws}"
12+
COLCON_WS="${COLCON_WS:-/home/ros2ai/colcon_ws}"
1313

1414
ros_distros=(
1515
"humble"
@@ -88,7 +88,7 @@ function verify_images() {
8888
-e OPENAI_ENDPOINT=$OPENAI_ENDPOINT \
8989
-e OPENAI_MODEL_NAME=$OPENAI_MODEL_NAME \
9090
$DOCKERHUB_USERNAME/ros2ai:$distro \
91-
bash -c "/ros_entrypoint.sh && source /root/.bashrc && cd $COLCON_WS && source ./install/setup.bash && $COLCON_WS/src/ros2ai/scripts/verification.sh"
91+
bash -c "/ros_entrypoint.sh && source /home/ros2ai/.bashrc && cd $COLCON_WS && source ./install/setup.bash && $COLCON_WS/src/ros2ai/scripts/verification.sh"
9292
done
9393
echo "----- all images successfully verified!!! -----"
9494
}

0 commit comments

Comments
 (0)