Skip to content

Commit

Permalink
Improvements for better portability
Browse files Browse the repository at this point in the history
  • Loading branch information
luigifreda committed Sep 2, 2024
1 parent 612bf58 commit b4bf3ff
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions Dockerfile_noetic_3dmr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ RUN touch ~/.gitconfig && touch ~/.ssh/known_hosts
RUN git config --global user.name "${user}"
RUN git config --global user.email "${user}@${user}.com"
RUN git config --global http.proxy ""
RUN sudo git config --system --add safe.directory '*' # to avoid "detected dubious ownership" error

# Install 3dmr
WORKDIR /tmp
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile_pyslam
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ libudev-dev
# pyslam stuff
RUN apt-get install -y rsync python3-sdl2 python3-tk \
libprotobuf-dev libeigen3-dev libopencv-dev libsuitesparse-dev libglew-dev
RUN apt-get install -y libhdf5-dev # needed when building h5py wheel from src is required (arm64)

################################

Expand Down Expand Up @@ -103,6 +104,7 @@ RUN touch ~/.gitconfig && touch ~/.ssh/known_hosts
RUN git config --global user.name "${user}"
RUN git config --global user.email "${user}@${user}.com"
RUN git config --global http.proxy ""
RUN sudo git config --system --add safe.directory '*' # to avoid "detected dubious ownership" error

# Install pyslam
WORKDIR ${home}
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile_pyslam_cuda
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ libudev-dev
# pyslam stuff
RUN apt-get install -y rsync python3-sdl2 python3-tk \
libprotobuf-dev libeigen3-dev libopencv-dev libsuitesparse-dev libglew-dev
RUN apt-get install -y libhdf5-dev # needed when building h5py wheel from src is required (arm64)

################################

Expand Down Expand Up @@ -104,6 +105,7 @@ RUN touch ~/.gitconfig && touch ~/.ssh/known_hosts
RUN git config --global user.name "${user}"
RUN git config --global user.email "${user}@${user}.com"
RUN git config --global http.proxy ""
RUN sudo git config --system --add safe.directory '*' # to avoid "detected dubious ownership" error

# Install pyslam
WORKDIR ${home}
Expand Down
7 changes: 7 additions & 0 deletions bash_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ function extract_version(){
}

function get_current_nvidia_driver_version(){
# check if nvidia-smi is installed
if ! command -v nvidia-smi &> /dev/null
then
echo ""
return
fi

NVIDIA_DRIVER_STRING=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader)
NVIDIA_DRIVER_VERSION=$(extract_version "$NVIDIA_DRIVER_STRING")
echo "$NVIDIA_DRIVER_VERSION"
Expand Down
15 changes: 14 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ source config.sh
echo building container $CONTAINER_NAME with $DOCKER_FILE
sleep 1

TIMEZONE=""
case "$OSTYPE" in
darwin*)
TIMEZONE=$(sudo systemsetup -gettimezone | sed 's/^Time Zone: //')
;;
linux*)
TIMEZONE=$(cat /etc/timezone)
;;
*)
echo "";
;;
esac

# Build the docker image (update the nvidia driver version if needed)
docker build -f "$DOCKER_FILE" --rm\
--build-arg user=$USER\
Expand All @@ -27,5 +40,5 @@ docker build -f "$DOCKER_FILE" --rm\
--build-arg shell=$SHELL\
--build-arg nvidia_driver_version="$NVIDIA_DRIVER_VERSION"\
--build-arg container_name=$CONTAINER_NAME\
--build-arg timezone=$(cat /etc/timezone)\
--build-arg timezone=$TIMEZONE\
-t $CONTAINER_NAME .
4 changes: 3 additions & 1 deletion config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ echo USE_LOCAL_HOME_FOLDER: $USE_LOCAL_HOME_FOLDER
# $ nvidia-smi --query-gpu=driver_version --format=csv,noheader
# The following variable is automatically set and must correctly reflect the currently used nvidia driver
export NVIDIA_DRIVER_VERSION=$(get_current_nvidia_driver_version)
echo Using NVIDIA DRIVER: $NVIDIA_DRIVER_VERSION
if [[ "${NVIDIA_DRIVER_VERSION}" != "" ]]; then
echo Using NVIDIA DRIVER: $NVIDIA_DRIVER_VERSION
fi

# Set default container name if it wasn't set up
if [[ -z "${CONTAINER_NAME}" ]]; then
Expand Down
43 changes: 38 additions & 5 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ if [ ! -f $XAUTH ]; then
chmod a+r $XAUTH
fi

DISPLAY_OPTIONS=""
if [[ "$OSTYPE" == "darwin"* ]]; then
# allow access from localhost (from https://medium.com/@mreichelt/how-to-show-x11-windows-within-docker-on-mac-50759f4b65cb)
echo "Starting XQuartz and allowing access to it..."
# NOTE: from https://gist.github.com/cschiewek/246a244ba23da8b9f0e7b11a68bf3285?permalink_comment_id=4198715
# 1. Install XQuartz: https://www.xquartz.org/
# brew install --cask xquartz
# 2. Launch XQuartz. Under the XQuartz menu, select Preferences
# 3. Go to the security tab and ensure "Allow connections from network clients" is checked.
open -a XQuartz
xhost + $(hostname)
touch $XAUTH

#DISPLAY_OPTIONS="$(hostname):0"
DISPLAY_OPTIONS=host.docker.internal:0

# NOTE: you may need to run this inside docker:
echo You may need to run the following command inside docker:
echo " export LIBGL_ALWAYS_INDIRECT=1"
fi
if [[ "$OSTYPE" == "linux"* ]]; then
DISPLAY_OPTIONS=$DISPLAY
fi

# N.B.:
# -v is used to map folders and files from your_os::src to docker_container::dst (!if you do not map them, yo do not see them!)

Expand All @@ -45,7 +69,7 @@ if [ "$(docker ps -a | grep ${CONTAINER_NAME})" ]; then
docker exec -it \
-e SHELL \
-e USER \
-e DISPLAY \
-e DISPLAY="$DISPLAY_OPTIONS" \
--privileged \
$1 $SHELL
else
Expand Down Expand Up @@ -76,19 +100,28 @@ else
group_id=$(id -g)
UID_OPTIONS="-u $user_id:$group_id"

NVIDIA_OPTIONS=""
if [[ "$OSTYPE" == "linux"* ]]; then
NVIDIA_OPTIONS="--gpus all -e NVIDIA_DRIVER_CAPABILITIES=all -e NVIDIA_VISIBLE_DEVICES=all --runtime=nvidia"
fi

MOUNT_OPTIONS=""
if [[ "$OSTYPE" == "linux"* ]]; then
MOUNT_OPTIONS="-v /media:/media -v /mnt:/mnt -v /dev:/dev" # not working under macOS
fi

docker run -it --rm \
--net=host \
--privileged $UID_OPTIONS \
-e SHELL \
-e USER \
-e DISPLAY \
-e DISPLAY="$DISPLAY_OPTIONS" \
-e DOCKER=1 \
-e XAUTHORITY=${XAUTH} \
-v $XSOCK:$XSOCK:rw \
-v $XAUTH:$XAUTH:rw \
$HOME_OPTIONS \
-v /media:/media -v /mnt:/mnt -v /dev:/dev -v $HOME/.ssh:$HOME/.ssh \
--gpus all -e NVIDIA_DRIVER_CAPABILITIES=all -e NVIDIA_VISIBLE_DEVICES=all --runtime=nvidia \
$HOME_OPTIONS $MOUNT_OPTIONS $NVIDIA_OPTIONS \
-v $HOME/.ssh:$HOME/.ssh \
--name $CONTAINER_NAME \
$1 $SHELL
fi

0 comments on commit b4bf3ff

Please sign in to comment.