-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcon
executable file
·31 lines (26 loc) · 892 Bytes
/
con
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
#!/bin/bash
set -e
if [[ "$(whoami)" != "root" ]] && ! [ "$(groups | grep -F 'docker')" ]; then
>&2 echo "User must be either root or in group docker"
exit 1
fi
container_name="$1"
if ! [[ "$container_name" ]]; then
>&2 echo "Requires container name as first argument"
exit 1
fi
available_containers="$(docker container ls -a --format='table {{.Names}}' | tail -n +2)"
if ! echo "$available_containers" | grep -qxF "$container_name"; then
>&2 echo "'$container_name' is not an available container"
exit 1
fi
running_containers="$(docker container ls --format='table {{.Names}}' | tail -n +2)"
if ! echo "$running_containers" | grep -qxF "$container_name"; then
docker start "$container_name" > /dev/null
fi
shift
if [[ "$#" == 0 ]]; then
exec docker exec -it --env DISPLAY "$container_name" /bin/bash -i
else
exec docker exec -i --env DISPLAY "$container_name" "$@"
fi