-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-shell
More file actions
executable file
·75 lines (68 loc) · 2.05 KB
/
docker-shell
File metadata and controls
executable file
·75 lines (68 loc) · 2.05 KB
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
#!/bin/sh
set -e
shells="bash fish zsh tcsh csh ksh sh"
command="$1"
case "$command" in
--help|-h|-?)
echo "Gives you a shell in a running Docker container".
echo ""
echo "Usage:"
echo " `basename $0` [CONTAINER] [SHELL]"
echo " `basename $0` --help|-h|-?"
echo " `basename $0` --version|-v"
echo ""
echo "Arguments:"
echo " CONTAINER Name of the container"
echo " SHELL Force a specific shell, otherwise pick 'best' shell"
echo " [$shells]"
echo " -h, -?, --help Print this help text"
echo " -v, --version Print version and exit"
echo ""
echo "Note:"
echo " If the container name is omitted then the newest container will be used."
echo " Container name will match any part of name with wildcards."
echo " E.g 'web' and 'ebserv' will both match 'webserver'."
echo " If container name matches more than one container the latest one will be used."
echo ""
echo "Author:"
echo " Oskar Andersson <[email protected]>"
exit 0
;;
--version|-v)
echo "`basename $0` v1.0.0"
exit 0
;;
*)
esac
if [ -z "$1" ]; then
# Latest running container
container=`docker ps --latest --filter "status=running" --format "{{.Names}}"`
else
# Latest container based on name filtering
container=`docker ps --latest --filter "name=$command" --filter "status=running" --format "{{.Names}}"`
fi
if [ -z "$container" ]; then
echo "Error: No container found."
exit 1
fi
if [ -n "$2" ]; then
shells="$2"
fi
# Detected shell
set +e
for shell in $shells
do
check=`docker exec -it $container which $shell`
if [ $? -eq 0 ]; then
chosen_shell=$shell
break
fi
done
if [ -z "$chosen_shell" ]; then
echo "Error: No shell found."
exit 2
fi
echo "Trying to execute $chosen_shell on $container ..."
docker exec -it $container $chosen_shell
echo "Shell exited with exit code $?. Welcome back!"
exit 0