Skip to content

Commit

Permalink
Update internal command for docker and setup. (StoglRobotics#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
destogl authored Dec 6, 2022
1 parent 8dc4ed2 commit 2d3721e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/faq/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ On Docker

How to forward a xsession
"""""""""""""""""""""""""""""
If you want to forward a xsession from docker (e.g. rviz2), you have to install xhost. When executing the commands ``start_container`` the docker user is added to the X Server access list.
If you want to forward a xsession from docker (e.g. rviz2), you have to install xhost.
The forwarding is done automatically by adding docker user to the X Server access list when docker is created.


How to use nvidia driver in docker
Expand Down
21 changes: 18 additions & 3 deletions scripts/_RosTeamWs_Docker_Defines.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rtw_switch_to_docker () {
fi

source "$RosTeamWS_FRAMEWORK_SCRIPTS_PATH"/docker/_RosTeamWs_Docker_Defines.bash
start_and_connect_user_to_docker "$RosTeamWS_DOCKER_TAG"
RTW_Docker_start_and_connect_user_to_docker "$RosTeamWS_DOCKER_TAG"
}

rtw_switch_to_docker_root () {
Expand All @@ -34,14 +34,29 @@ rtw_switch_to_docker_root () {
fi

source "$RosTeamWS_FRAMEWORK_SCRIPTS_PATH"/docker/_RosTeamWs_Docker_Defines.bash
start_and_connect_root_to_docker "$RosTeamWS_DOCKER_TAG"
RTW_Docker_start_and_connect_root_to_docker "$RosTeamWS_DOCKER_TAG"
}

rtw_docker_stop () {
if [ -z "$RosTeamWS_WS_DOCKER_SUPPORT" ] || [ "$RosTeamWS_WS_DOCKER_SUPPORT" == false ]; then
print_and_exit "It seems your current workspace does not support docker. If it should, did you activate it by executing _\"<ws_alias>\"?"
fi

source "$RosTeamWS_FRAMEWORK_SCRIPTS_PATH"/docker/_RosTeamWs_Docker_Defines.bash
RTW_Docker_stop_docker_container "$RosTeamWS_DOCKER_TAG"
}

rtw_stop_docker () {
notify_user "Command 'rtw_stop_docker' is deprecated in favor of 'rtw_docker_stop'. Please use this in the future."

rtw_docker_stop
}

rtw_docker_clean_container_and_image () {
if [ -z "$RosTeamWS_WS_DOCKER_SUPPORT" ] || [ "$RosTeamWS_WS_DOCKER_SUPPORT" == false ]; then
print_and_exit "It seems your current workspace does not support docker. If it should, did you activate it by executing _\"<ws_alias>\"?"
fi

source "$RosTeamWS_FRAMEWORK_SCRIPTS_PATH"/docker/_RosTeamWs_Docker_Defines.bash
stop_docker_container "$RosTeamWS_DOCKER_TAG"
RTW_Docker_clean_container_and_image "$RosTeamWS_DOCKER_TAG"
}
52 changes: 34 additions & 18 deletions scripts/docker/_RosTeamWs_Docker_Defines.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare -r -A ros_distro_to_rtw_branch=( ["noetic"]="master" ["foxy"]="foxy" ["g

# $1 = name (tag) of the created docker image
# $2 = Dockerfile which is used for creating the image
build_docker_image () {
RTW_Docker_build_docker_image () {
if [ -z "$1" ]; then
echo "No docker image tag specified. Can not create image."
return 1
Expand Down Expand Up @@ -53,7 +53,7 @@ build_docker_image () {
cd "$prev_pwd" || { print_and_exit "Build of docker container succeeded but changing back previous working directory failed."; }
}

create_docker_container() {
RTW_Docker_create_docker_container () {
if [ -z "$1" ]; then
print_and_exit "No docker image (tag) specified. Can not instantiate image."
fi
Expand Down Expand Up @@ -107,77 +107,93 @@ create_docker_container() {
-it "$docker_image_tag" /bin/bash
}

connect_user () {
RTW_Docker_connect_user () {
if [ -z "$1" ]; then
print_and_exit "No container name given. Can not connect to instance."
fi
local container_instance_name
container_instance_name=$1 # assigen separate https://github.com/koalaman/shellcheck/wiki/SC2155

echo "Connecting to container instance: $container_instance_name as user."
echo -e "${RTW_COLOR_NOTIFY_USER}Connecting to container instance as user:${TERMINAL_COLOR_NC} $container_instance_name"
docker exec -u $USER -it "$container_instance_name" /bin/bash
}

connect_root_user () {
RTW_Docker_connect_root_user () {
if [ -z "$1" ]; then
print_and_exit "No container name given. Can not connect to instance."
fi
local container_instance_name
container_instance_name=$1 # assigen separate https://github.com/koalaman/shellcheck/wiki/SC2155

echo "Connecting to container instance: $container_instance_name as root-user."
echo -e "${RTW_COLOR_NOTIFY_USER}Connecting to container instance as root-user:${TERMINAL_COLOR_NC} $container_instance_name"
docker exec -u root -it "$container_instance_name" /bin/bash
}

start_container () {
RTW_Docker_start_container () {
if [ -z "$1" ]; then
print_and_exit "No container name given. Can not start dockerinstance."
fi
local container_instance_name
container_instance_name=$1 # assigen separate https://github.com/koalaman/shellcheck/wiki/SC2155

echo "Starting container instance: $container_instance_name "
echo -e "${RTW_COLOR_NOTIFY_USER}Starting container instance:${TERMINAL_COLOR_NC} $container_instance_name "
xhost +local:docker
docker start "$container_instance_name"
}

stop_container () {
RTW_Docker_stop_container () {
if [ -z "$1" ]; then
print_and_exit "No container name given. Can not stop dockerinstance."
fi
local container_instance_name
container_instance_name=$1 # assigen separate https://github.com/koalaman/shellcheck/wiki/SC2155

echo "Stopping container instance: $container_instance_name "
echo -e "${RTW_COLOR_NOTIFY_USER}Stopping container instance:${TERMINAL_COLOR_NC} $container_instance_name"
xhost -local:docker
docker stop "$container_instance_name"
}

start_and_connect_user_to_docker () {
RTW_Docker_start_and_connect_user_to_docker () {
if [ -z "$1" ]; then
print_and_exit "The given docker tag is empty, something went wrong. Does your current workspace contain a RosTeamWS_DOCKER_TAG in .ros_team_ws_rc ?"
fi
local container_instance_name="$1-instance"

start_container "$container_instance_name"
connect_user "$container_instance_name"
RTW_Docker_start_container "$container_instance_name"
RTW_Docker_connect_user "$container_instance_name"
}

start_and_connect_root_to_docker () {
RTW_Docker_start_and_connect_root_to_docker () {
if [ -z "$1" ]; then
print_and_exit "The given docker tag is empty, something went wrong. Does your current workspace contain a RosTeamWS_DOCKER_TAG in .ros_team_ws_rc ?"
fi
local container_instance_name="$1-instance"

start_container "$container_instance_name"
connect_root_user "$container_instance_name"
RTW_Docker_start_container "$container_instance_name"
RTW_Docker_connect_root_user "$container_instance_name"
}

stop_docker_container () {
RTW_Docker_stop_docker_container () {
if [ -z "$1" ]; then
print_and_exit "The given docker tag is empty, something went wrong. Does your current workspace contain a RosTeamWS_DOCKER_TAG in .ros_team_ws_rc ?"
fi
local container_instance_name="$1-instance"

stop_container "$container_instance_name"
RTW_Docker_stop_container "$container_instance_name"
}

RTW_Docker_clean_container_and_image () {
if [ -z "$1" ]; then
print_and_exit "The given docker tag is empty, something went wrong. Does your current workspace contain a RosTeamWS_DOCKER_TAG in .ros_team_ws_rc ?"
fi
local image_tag="$1"
local container_instance_name="$image_tag-instance"

RTW_Docker_stop_container "$container_instance_name"

echo -e "${RTW_COLOR_NOTIFY_USER}Removing container instance:${TERMINAL_COLOR_NC} $container_instance_name"
docker container rm "$container_instance_name"

echo -e "${RTW_COLOR_NOTIFY_USER}Removing image:${TERMINAL_COLOR_NC} $image_tag"
docker image rm "$image_tag"
}
4 changes: 2 additions & 2 deletions scripts/setup-ros-workspace.bash
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,13 @@ create_workspace_docker () {
sed -i "s|DUMMY_WS_FOLDER|${docker_ws_path}|g" "$ws_docker_folder/recreate_docker.sh"

# now we are all set for building the container
build_docker_image "$docker_image_tag" "$ws_docker_folder/Dockerfile" || { print_and_exit "Build of docker container failed."; }
RTW_Docker_build_docker_image "$docker_image_tag" "$ws_docker_folder/Dockerfile" || { print_and_exit "Build of docker container failed."; }

echo ""
echo "######################################################################################################################"
echo -e "${RTW_COLOR_NOTIFY_USER}Finished creating new workspace with docker support: Going to switch to docker. Next time simply run '$alias_name'${TERMINAL_COLOR_NC}"
echo "######################################################################################################################"
sleep 2 # give user time to read above message before switching to docker container

create_docker_container "$docker_image_tag" "${docker_ws_path}" "$chosen_ros_distro" "$docker_host_name"
RTW_Docker_create_docker_container "$docker_image_tag" "${docker_ws_path}" "$chosen_ros_distro" "$docker_host_name"
}

0 comments on commit 2d3721e

Please sign in to comment.