Skip to content

Commit 846d0f1

Browse files
author
jonathan.cacace
committed
user agnostic docker script added
1 parent cf43ec8 commit 846d0f1

9 files changed

Lines changed: 102 additions & 157 deletions

File tree

Docker/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
UID=1000
2+
GID=1000
3+
NAME=user

Docker/.vscode/c_cpp_properties.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

Docker/.vscode/settings.json

Lines changed: 0 additions & 103 deletions
This file was deleted.

Docker/Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install libzmq3-dev
1616
RUN DEBIAN_FRONTEND=noninteractive apt-get install libncurses5-dev libncursesw5-dev -y
1717
#Groot dependencies
1818
RUN apt-get update && apt-get install qtbase5-dev libqt5svg5-dev libzmq3-dev libdw-dev -y
19+
RUN apt-get update && apt-get install -y gosu
1920

2021
COPY entrypoint.sh /entrypoint.sh
2122
COPY deps.repos /deps.repos
2223
# Make sure it is executable
2324
RUN chmod +x /entrypoint.sh
2425

25-
USER ubuntu
26-
RUN mkdir -p /home/ubuntu/eut_bt_ros2_ws/src
27-
WORKDIR /home/ubuntu/eut_bt_ros2_ws/src
26+
27+
#USER ubuntu
28+
#RUN mkdir -p /home/ubuntu/eut_bt_ros2_ws/src
29+
#WORKDIR /home/ubuntu/eut_bt_ros2_ws/src
2830

2931

3032
ENTRYPOINT ["/entrypoint.sh"]

Docker/docker-compose.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@ services:
55
container_name: "eut_bt_ros2"
66
network_mode: host
77
#runtime: nvidia
8-
user: ubuntu
8+
#user: ubuntu
9+
user: root
910
image: eut_bt_ros2:jazzy
1011
privileged: true
1112
volumes:
12-
- /tmp/.X11-unix:/tmp/.X11-unix:rw
13-
- ~/.ssh:/home/ubuntu/.ssh:ro
13+
- /tmp/.X11-unix:/tmp/.X11-unix
14+
- ~/.ssh:/home/user/.ssh
15+
- ~/.ssh:/home/user/.ssh
1416
- ~/.xauth_docker/share/:/tmp/xauth/
15-
- ./.vscode:/home/ubuntu/eut_bt_ros2_ws/.vscode
16-
- ../behaviortree_ros2:/home/ubuntu/eut_bt_ros2_ws/src/behaviortree_ros2
17-
- ../btcpp_ros2_interfaces:/home/ubuntu/eut_bt_ros2_ws/src/btcpp_ros2_interfaces
18-
- ../bt_examples:/home/ubuntu/eut_bt_ros2_ws/src/bt_examples
17+
- ./.vscode:/home/user/eut_bt_ros2_ws/.vscode
18+
- ../behaviortree_ros2:/home/user/eut_bt_ros2_ws/src/behaviortree_ros2
19+
- ../btcpp_ros2_interfaces:/home/user/eut_bt_ros2_ws/src/btcpp_ros2_interfaces
20+
- ../bt_examples:/home/user/eut_bt_ros2_ws/src/bt_examples
1921
tty: true
2022
stdin_open: true
2123
environment:
2224
# - NVIDIA_VISIBLE_DEVICES=all
2325
- DISPLAY=$DISPLAY
2426
- QT_X11_NO_MITSHM=1
2527
- XAUTHORITY=/tmp/xauth/.docker.xauth
28+
- UID=${UID}
29+
- GID=${GID}
30+
- UNAME=${NAME}
2631
devices:
2732
- /dev/dri:/dev/dri
2833
- /dev/snd:/dev/snd

Docker/entrypoint.sh

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,95 @@
11
#!/bin/bash
22
ROS_DISTRO=jazzy
33

4-
54
set -e
6-
mkdir -p deps
75

8-
cd deps
6+
# Set default UID and GID if not provided
7+
USER_ID=${UID:-1000}
8+
GROUP_ID=${GID:-1000}
9+
USERNAME=${UNAME:-ubuntu}
10+
GROUPNAME=${UNAME:-ubuntu}
11+
user_removed=false
12+
13+
14+
# Check if the user already exists
15+
EXISTING_USERNAME=$(getent passwd "$USER_ID" | cut -d: -f1)
16+
17+
# If the user exists and the username is different, remove the existing user (we need to free the UID)
18+
if [ -n "$EXISTING_USERNAME" ] && [ "$EXISTING_USERNAME" != "$USERNAME" ]; then
19+
echo "Removing user $EXISTING_USERNAME"
20+
deluser --remove-home $EXISTING_USERNAME
21+
user_removed=true
22+
fi
23+
24+
# The development folder has shared in the docker-compose file as volume
25+
# It is not a user folder, so we need to add the initial files like .bashrc and .profile
26+
if [ "$user_removed" = true ]; then
27+
cp -r /etc/skel/. /home/$USERNAME/ 2>/dev/null
28+
fi
929

10-
vcs import . < /deps.repos
11-
vcs pull .
30+
# Change the ownership of the home directory to the specified user and group
31+
chown $USER_ID:$GROUP_ID /home/$USERNAME/
1232

33+
### Create group if it doesn't exist
34+
if ! getent group $GROUP_ID >/dev/null; then
35+
groupadd -g $GROUP_ID $USERNAME
36+
fi
37+
38+
# Now, we can create the user with the specified UID and GID
39+
if ! id -u $USER_ID >/dev/null 2>&1; then
40+
echo "Creating user $USERNAME with UID $USER_ID and GID $GROUP_ID"
41+
useradd -m -u $USER_ID -g $GROUP_ID -s /bin/bash $USERNAME
42+
fi
43+
44+
# If not present, request to load the ROS environment in the .bashrc file
1345
ROS_SETUP="source /opt/ros/${ROS_DISTRO}/setup.bash"
14-
if ! grep -Fxq "$ROS_SETUP" ~/.bashrc; then
46+
if ! grep -Fxq "$ROS_SETUP" /home/$USERNAME/.bashrc; then
1547
echo "$ROS_SETUP" >> ~/.bashrc
1648
fi
17-
#
18-
OVERLAY_SETUP="source /home/ubuntu/eut_bt_ros2_ws/install/setup.bash"
19-
if ! grep -Fxq "$OVERLAY_SETUP" ~/.bashrc; then
20-
echo "$OVERLAY_SETUP" >> ~/.bashrc
49+
50+
# If not present, request to load the workspace environment in the .bashrc file
51+
OVERLAY_SETUP="source /home/${USERNAME}/eut_bt_ros2_ws/install/setup.bash"
52+
if ! grep -Fxq "$OVERLAY_SETUP" /home/$USERNAME/.bashrc; then
53+
echo "$OVERLAY_SETUP" >> /home/$USERNAME/.bashrc
2154
fi
22-
#
55+
56+
# If not present, request to load the colcon_argcomplete environment in the .bashrc file
2357
AUTOCOMPLETE_SETUP="source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash"
24-
if ! grep -Fxq "$AUTOCOMPLETE_SETUP" ~/.bashrc; then
25-
echo "$AUTOCOMPLETE_SETUP" >> ~/.bashrc
58+
if ! grep -Fxq "$AUTOCOMPLETE_SETUP" /home/$USERNAME/.bashrc; then
59+
echo "$AUTOCOMPLETE_SETUP" >> /home/$USERNAME/.bashrc
2660
fi
2761

28-
WORKING_DIR="cd /home/ubuntu/eut_bt_ros2_ws"
29-
if ! grep -Fxq "$WORKING_DIR" ~/.bashrc; then
30-
echo "$WORKING_DIR" >> ~/.bashrc
31-
fi
3262

33-
cd ../../
63+
#WORKING_DIR="cd /home/${USERNAME}/eut_bt_ros2_ws"
64+
#if ! grep -Fxq "$WORKING_DIR" /home/$USERNAME/.bashrc; then
65+
# echo "$WORKING_DIR" >> /home/$USERNAME/.bashrc
66+
#fi
67+
68+
69+
# Set the ownership of the home directory and recurisvely the volumes set in the docker-compose file
70+
chown $USERNAME:$GROUPNAME /home/$USERNAME/* -R
71+
72+
73+
# Switch to the new user and run the main command
74+
exec gosu "$USERNAME" bash -c "
75+
echo 'Running as $USERNAME...';
76+
77+
# Load the environment variables
78+
source /home/$USERNAME/.bashrc;
79+
80+
# Open the workspace and join the dependencies folder after created
81+
cd /home/$USERNAME/eut_bt_ros2_ws/src/;
82+
mkdir deps;
83+
cd deps;
3484
35-
source /opt/ros/${ROS_DISTRO}/setup.bash
36-
colcon build --symlink-install
37-
source ~/.bashrc
85+
# Download the dependencies from the repository
86+
vcs import . < /deps.repos;
87+
vcs pull .;
3888
39-
echo "SYSTEM READY"
40-
exec "$@"
89+
# Compile the workspace
90+
cd ../../;
91+
source /opt/ros/${ROS_DISTRO}/setup.bash;
92+
colcon build --symlink-install;
93+
94+
exec \"$@\";
95+
"

scripts/.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

scripts/exec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
docker exec -it eut_bt_ros2 bash
1+
docker exec -it --user user eut_bt_ros2 bash

scripts/install.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
EXEC_DIR="$(pwd)"
66
cd $SCRIPT_DIR
77

8+
echo "UID=$(id -u)" > ../Docker/.env
9+
echo "GID=$(id -g)" >> ../Docker/.env
10+
echo "NAME=user" >> ../Docker/.env
811

912
docker_dir="../Docker/"
1013
docker_depend_dir="../Docker/depend"

0 commit comments

Comments
 (0)