Skip to content

Commit

Permalink
Minor Dockerfile cleanup (#11)
Browse files Browse the repository at this point in the history
* Autoupdate pre-commit

* Minor cleanup in Dockerfile

* Updates to functionality.

* Minor update to Dockerfile
  • Loading branch information
amarburg authored Nov 26, 2024
1 parent bc5e753 commit 35fc552
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
# file type, which requires identity >= 2.5.35
# (current CI image uses 2.5.33)
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v16.0.4
rev: v19.1.3
hooks:
- id: clang-format
types_or: [c++, c]
Expand Down
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
FROM osrf/ros:noetic-desktop-full

# For consistency with later (jazzy, rolling) ROS images which are based on
# "noble", use "ubuntu"
ARG USER_NAME=ubuntu
ARG USER_UID=1000
ARG USER_GID=${USER_UID}

# Adding typical tools we need or like to have
RUN apt-get update \
&& apt-get install -q -y --no-install-recommends \
Expand All @@ -22,8 +16,17 @@ ENV ROS_DISTRO=""

COPY --chmod=0755 ./entrypoint.sh /ros_entrypoint.sh

RUN groupadd --gid ${USER_GID} ${USER_NAME} && \
useradd --uid ${USER_UID} --gid ${USER_GID} \
# Later (jazzy, rolling) ROS images are based on "noble"
# "noble" images contain a built-in user "ubuntu" as uid 1000
# so we use that name as well
#
# Note the sudoers file calls out "all members of group sudo"
# so it works even if ubuntu's UID changes
ARG USER_NAME=ubuntu
ARG USER_UID=1000
ARG USER_GID=${USER_UID}
RUN groupadd --gid ${USER_GID} ${USER_NAME} && \
useradd --gid ${USER_GID} --uid ${USER_UID} \
-G sudo ${USER_NAME} \
&& echo %sudo ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/nopasswd \
&& chmod 0440 /etc/sudoers.d/nopasswd
Expand Down
5 changes: 3 additions & 2 deletions install_rosvenv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

DEPLOY_BRANCH=main
DEPLOY_REPO=https://github.com/apl-ocean-engineering/rosvenv
ROSVENV_ROOT="${HOME}/.rosvenv"

ROSVENV_ROOT="~/.rosvenv"

# If rosvenv isn't already installed and sourced
if [ "$( type -t createROSWS )" != "function" ]; then
git clone --depth 1 -b $DEPLOY_BRANCH $DEPLOY_REPO $ROSVENV_ROOT

printf "\n# ROSVENV\nsource ${ROSVENV_ROOT}/rosvenv.bash\nsource ${ROSVENV_ROOT}/rosvenv_docker.bash\n" >> "${HOME}/.bashrc"
printf "\n# ROSVENV; As the username inside Docker may not match outside, it's critical these use \"~\" not an explicit home dir\nsource ${ROSVENV_ROOT}/rosvenv.bash\nsource ${ROSVENV_ROOT}/rosvenv_docker.bash\n" >> "${HOME}/.bashrc"
echo "export ROSVENV_ROOT=$ROSVENV_ROOT" >> "${HOME}/.bashrc"
fi

Expand Down
7 changes: 5 additions & 2 deletions rosvenv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ createROSWS() {
fi
else
if ! $(rosvenv_docker_image_exists $ROSVENV_DEFAULT_DOCKER_IMAGE); then
echo "ROSVENV base image does not seem to exist. Building it..."
echo "ROSVENV base image does not seem to exist. Pulling it..."
if ! rosvenv_docker_build_container; then
echo "Something went wrong while trying to build base container. Please see output above."
echo "Something went wrong while trying to pull base container. Please see output above."
return -1
fi
else
Expand All @@ -146,6 +146,8 @@ createROSWS() {

container_name="$(_rosvenv_ws_path_to_name $ws_dir)"

echo "Launching docker ($docker_image) for workspace $ws_dir"

rosvenv_docker_login_wrapper $docker_image $container_name $ws_dir "createROSWS" $original_args
fi
fi
Expand Down Expand Up @@ -239,6 +241,7 @@ activateROS() {

if ! _rosvenv_precheck || ([ -z $ROSVENV_IN_DOCKER ] && $(_rosvenv_ws_has_docker $ws_dir)); then
docker_image="$(_rosvenv_get_ws_image_name $ws_dir)"

echo "Signing into docker ($docker_image) for workspace $ws_dir"

if ! rosvenv_docker_autobuild $docker_image $ws_dir; then
Expand Down
16 changes: 12 additions & 4 deletions rosvenv_docker.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ROSVENV DOCKER TOOLS
# ========================================

ROSVENV_DEFAULT_DOCKER_IMAGE="ghcr.io/apl-ocean-engineering/rosvenv:latest"
ROSVENV_DEFAULT_DOCKER_IMAGE="ghcr.io/apl-ocean-engineering/rosvenv:main"

rosvenv_has_docker() {
# Checks if docker is installed on the system.
Expand Down Expand Up @@ -133,7 +133,7 @@ rosvenv_docker_start_ws_container() {

gpu_options=""
if ! rosvenv_has_nvctk; then
echo "Found no installation of nvidia-container-toolkit."
echo "Found no installation of nvidia-container-toolkit, no GPU support in docker..."
else
echo "Found installation of nvidia-container-toolkit. Exposing your GPUs to the container..."
gpu_options="--gpus all --env NVIDIA_VISIBLE_DEVICES=all --env NVIDIA_DRIVER_CAPABILITIES=all"
Expand All @@ -143,15 +143,21 @@ rosvenv_docker_start_ws_container() {
container_name=$2
shift 2

echo "Running docker!"
docker run --name $container_name \
--group-add=sudo \
--network=host \
--hostname $container_name \
--add-host=$container_name=127.0.0.1 \
--env DISPLAY=$DISPLAY \
-u `id -u`:`id -g` \
$gpu_options \
$@ \
--env QT_X11_NO_MITSHM=1 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-d -v $HOME:$HOME --user $USER $image_name \
-v $HOME:/home/ubuntu \
-d \
$image_name \
bash -c "while true; do sleep 0.5s; done"
}

Expand Down Expand Up @@ -187,7 +193,9 @@ rosvenv_docker_login_wrapper() {
shift 3
bash_instruction="printf \"source ~/.bashrc\nunset ROS_DISTRO\nexport ROSVENV_IN_DOCKER=1\n$*\n\" > /tmp/COMMAND; bash --init-file /tmp/COMMAND"

docker exec -w $PWD -it $container_name bash -c "$bash_instruction"
pwd_in_docker=`echo $PWD | sed s/$USER/ubuntu/`

docker exec -w $pwd_in_docker -it $container_name bash -c "$bash_instruction"
}

rosvenv_docker_autobuild() {
Expand Down

0 comments on commit 35fc552

Please sign in to comment.