-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive.sh
executable file
·149 lines (127 loc) · 3.74 KB
/
interactive.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
IMAGE_NAME=""
CONTAINER_NAME=""
USERNAME=""
GPUS=""
ROS_DOMAIN_ID=14
GENERATE_HOST_NAME=true
COMMAND=/bin/bash
HELP_MESSAGE="
Usage: aica-docker interactive <image> [-n <name>] [-u <user>]
Run a docker container as an interactive shell.
Options:
-i, --image <name> Specify the name of the docker image.
(required)
-n, --name <name> Specify the name of generated container.
By default, this is deduced from
the image name, replacing all
'/' and ':' with '-' and appending
'-runtime'. For example, the image
aica-technology/ros2-ws:humble would yield
aica-technology-ros2-ws-humble-runtime
-c, --command <cmd> Pass a command to execute in the container.
(optional, default: ${COMMAND})
-u, --user <user> Specify the name of the login user.
(optional)
--no-hostname Suppress the automatic generation of a
hostname for the container. By default,
the container hostname is set to be
the same as the container name.
--gpus <gpu_options> Add GPU access for applications that
require hardware acceleration (e.g. Gazebo)
For the list of gpu_options parameters see:
>>> https://docs.docker.com/config/containers/resource_constraints
--ros-domain-id <id> Set the ROS_DOMAIN_ID environment variable
to avoid conflicts when doing network discovery.
-h, --help Show this help message.
Any additional arguments passed to this script are forwarded to
the 'docker run' command.
"
RUN_FLAGS=()
FWD_ARGS=()
while [ "$#" -gt 0 ]; do
case "$1" in
-i | --image)
IMAGE_NAME=$2
shift 2
;;
-n | --name)
CONTAINER_NAME=$2
shift 2
;;
-c | --command)
COMMAND=$2
shift 2
;;
--no-hostname)
GENERATE_HOST_NAME=false
shift 1
;;
-u | --user)
USERNAME=$2
shift 2
;;
--gpus)
GPUS=$2
shift 2
;;
--ros-domain-id)
ROS_DOMAIN_ID=$2
shift 2
;;
-h | --help)
echo "${HELP_MESSAGE}"
exit 0
;;
*)
if [ -z "${IMAGE_NAME}" ]; then
IMAGE_NAME=$1
else
FWD_ARGS+=("$1")
fi
shift 1
;;
esac
done
if [ -z "${IMAGE_NAME}" ]; then
echo "No image name provided!"
echo "${HELP_MESSAGE}"
exit 1
fi
if [ -z "${CONTAINER_NAME}" ]; then
CONTAINER_NAME="${IMAGE_NAME//[\/.]/-}"
CONTAINER_NAME="${CONTAINER_NAME/:/-}-runtime"
fi
if [ -n "${USERNAME}" ]; then
RUN_FLAGS+=(-u "${USERNAME}")
fi
if [ $GENERATE_HOST_NAME == true ]; then
RUN_FLAGS+=(--hostname "${CONTAINER_NAME}")
fi
if [ -n "${GPUS}" ]; then
RUN_FLAGS+=(--gpus "${GPUS}")
RUN_FLAGS+=(--env DISPLAY="${DISPLAY}")
RUN_FLAGS+=(--env NVIDIA_VISIBLE_DEVICES="${NVIDIA_VISIBLE_DEVICES:-all}")
RUN_FLAGS+=(--env NVIDIA_DRIVER_CAPABILITIES="${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics")
fi
if [ -n "${ROS_DOMAIN_ID}" ]; then
RUN_FLAGS+=(--env ROS_DOMAIN_ID="${ROS_DOMAIN_ID}")
fi
if [[ "${OSTYPE}" == "darwin"* ]]; then
RUN_FLAGS+=(-e DISPLAY=host.docker.internal:0)
else
xhost +
RUN_FLAGS+=(-e DISPLAY="${DISPLAY}")
RUN_FLAGS+=(-e XAUTHORITY="${XAUTHORITY}")
RUN_FLAGS+=(-v /tmp/.X11-unix:/tmp/.X11-unix:rw)
RUN_FLAGS+=(--device=/dev/dri:/dev/dri)
fi
if [ ${#FWD_ARGS[@]} -gt 0 ]; then
echo "Forwarding additional arguments to docker run command:"
echo "${FWD_ARGS[@]}"
fi
docker run -it --rm \
"${RUN_FLAGS[@]}" \
--name "${CONTAINER_NAME}" \
"${FWD_ARGS[@]}" \
"${IMAGE_NAME}" "${COMMAND}"