-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.sh
executable file
·96 lines (80 loc) · 2.09 KB
/
connect.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
#!/bin/bash
CONTAINER_NAME=""
USERNAME=root
HELP_MESSAGE="
Usage: aica-docker connect <container> [-u <username>]
Connect an interactive terminal shell to a running container.
The container name is required and can be provided either as
a positional argument or explicitly with the -n|--name option.
Options:
-n, --name <container> Specify the container name.
(required)
-u, --user <username> Specify the login username.
(default: ${USERNAME})
-h, --help Show this help message.
Any additional arguments passed to this script are forwarded to
the 'docker container exec' command.
"
FWD_ARGS=()
while [ "$#" -gt 0 ]; do
case "$1" in
-n | --name)
CONTAINER_NAME=$2
shift 2
;;
-u | --user)
USERNAME=$2
shift 2
;;
-h | --help)
echo "${HELP_MESSAGE}"
exit 0
;;
*)
if [ -z "${CONTAINER_NAME}" ]; then
CONTAINER_NAME=$1
else
FWD_ARGS+=("$1")
fi
shift 1
;;
esac
done
if [ -z "$CONTAINER_NAME" ]; then
CONTAINERS="$(docker ps --format "{{.Names}}")"
if [ -z "$CONTAINERS" ]; then
echo "No running containers found!"
exit 1
else
echo "Enter the ID of the container to which to connect:"
fi
# shellcheck disable=SC2206
CONTAINERS=($CONTAINERS)
INDEX=0
for CONTAINER in "${CONTAINERS[@]}"; do
:
INDEX=$((INDEX + 1))
echo "$INDEX: $CONTAINER"
done
read -r INPUT
if [[ -n ${INPUT//[0-9]/} || "$INPUT" -le 0 || "$INPUT" -gt "$INDEX" ]]; then
echo "Invalid input!"
exit 1
fi
CONTAINER_NAME=${CONTAINERS[$((INPUT - 1))]}
echo "Using container $CONTAINER_NAME"
fi
EXEC_FLAGS=()
EXEC_FLAGS+=(-u "${USERNAME}")
if [[ "$OSTYPE" == "darwin"* ]]; then
EXEC_FLAGS+=(-e DISPLAY=host.docker.internal:0)
else
xhost +
EXEC_FLAGS+=(-e DISPLAY="${DISPLAY}")
EXEC_FLAGS+=(-e XAUTHORITY="${XAUTH}")
fi
if [ ${#FWD_ARGS[@]} -gt 0 ]; then
echo "Forwarding additional arguments to docker container exec command:"
echo "${FWD_ARGS[@]}"
fi
docker container exec -it "${EXEC_FLAGS[@]}" "${FWD_ARGS[@]}" "${CONTAINER_NAME}" /bin/bash