diff --git a/docs/faq/index.rst b/docs/faq/index.rst index 47765bc0..c7414335 100644 --- a/docs/faq/index.rst +++ b/docs/faq/index.rst @@ -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 diff --git a/scripts/_RosTeamWs_Docker_Defines.bash b/scripts/_RosTeamWs_Docker_Defines.bash index 7fe25259..c863e138 100644 --- a/scripts/_RosTeamWs_Docker_Defines.bash +++ b/scripts/_RosTeamWs_Docker_Defines.bash @@ -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 () { @@ -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 _\"\"?" + 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 _\"\"?" 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" } diff --git a/scripts/docker/_RosTeamWs_Docker_Defines.bash b/scripts/docker/_RosTeamWs_Docker_Defines.bash index 1cf2d947..c29225dc 100755 --- a/scripts/docker/_RosTeamWs_Docker_Defines.bash +++ b/scripts/docker/_RosTeamWs_Docker_Defines.bash @@ -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 @@ -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 @@ -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" } diff --git a/scripts/setup-ros-workspace.bash b/scripts/setup-ros-workspace.bash index b543a479..72089303 100755 --- a/scripts/setup-ros-workspace.bash +++ b/scripts/setup-ros-workspace.bash @@ -409,7 +409,7 @@ 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 "######################################################################################################################" @@ -417,5 +417,5 @@ create_workspace_docker () { 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" }