nonanonno / ros2_d_docker

0 stars 0 forks source link

xdocker #1

Open nonanonno opened 3 years ago

nonanonno commented 3 years ago
#!/bin/bash

CUSER="root"
CHOME="/root"
CNAME="container"
HISTORY="$HOME/.container_history"
TMUX="$HOME/.tmux.conf"
VOLUMES=""
WORK=""

usage_exit() {
  echo -e "Usage: xdocker [options ...] image cmd"
  echo -e "\t-d\tRun container in background"
  echo -e "\t-i\titerative and tty"
  echo -e "\t-h\tprint this message and exit"
  echo -e "\t-l\tbash_history file to attach to the container (default $HISTORY)"
  echo -e "\t-n\tcontainer name (default $CNAME)"
  echo -e "\t-r\tadd '--rm' option"
  echo -e "\t-t\ttmux.conf name to attach to the container (default $TMUX)"
  echo -e "\t-u\tusername in the container (default $CUSER)"
  echo -e "\t-v\tvolume options (same as docker command)"
  echo -e "\t-w\tworking directory in the docker"

  exit 1
}

OPT=`getopt -o hl:n:t:u:v:w: -- "$@"`
if [ $? != 0 ] ; then
  exit 1
fi
eval set -- "$OPT"

while true
do
  case $1 in
    -h )
      usage_exit
      ;;
    -l )
      HISTORY=$2
      shift 2
      ;;
    -n )
      CNAME=$2
      shift 2
      ;;
    -t )
      TMUX=$2
      shift 2
      ;;
    -u ) 
      CUSER=$2
      CHOME="/home/$CUSER"
      shift 2
      ;;
    -v )
      VOLUMES="$VOLUMES --volume=$2"
      shift 2
      ;;
    -w )
      WORK="--workdir $2"
      shift 2
      ;;
    --)
      shift
      break
      ;;
  esac
done

if [ -z $2 ] ; then 
  usage_exit
fi

IMAGE=$1
CMD=$2

test -f $HISTORY || touch $HISTORY
test -f $TMUX || touch $TMUX

VOLUMES="$VOLUMES --volume=$(readlink -f $HISTORY):$CHOME/.bash_history"
VOLUMES="$VOLUMES --volume=$(readlink -f $TMUX):$CHOME/.tmux.conf"

XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -

VOLUMES="$VOLUMES --volume=$XSOCK:$XSOCK:rw --volume=$XAUTH:$XAUTH:rw"

docker run --gpus all --privileged --rm -it \
           $VOLUMES \
           --shm-size=1gb \
           --env=XAUTHORITY=$XAUTH \
           --env=DISPLAY=$DISPLAY \
           --env=TERM=xterm-256color \
           --env=QT_X11_NO_MITSHM=1 \
           --net=host \
           -u $CUSER \
           $WORK \
           --name $CNAME \
           $IMAGE \
           $CMD