-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_functions.sh
82 lines (75 loc) · 1.62 KB
/
docker_functions.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
#### Docker utilities ####
function dls
{
# Lists active and stopped containers
color_echo "$LBLUE" "Active docker containers:"
docker container ls
echo -e ""
color_echo "$LYELLOW" "Stopped docker containers:"
docker ps --filter "status=exited"
}
function dsh
{
if [ "$#" -ne 1 ]; then
echo -e "Usage: dsh <container>"
else
# Starts a session to a container
if ! drunning $1; then
docker restart $1
fi
docker exec -it $1 bash
fi
}
function drunning
{
if [ "$( docker container inspect -f '{{.State.Status}}' $1 )" == "running" ]; then
true && return
else
false
fi
}
function dils
{
# List images
color_echo "$LGRAY" "List of docker images:"
docker images
}
function dkill
{
# stop a container
if [ "$#" -ne 1 ]; then
echo -e "Usage: dkill <container>"
else
if drunning $1; then
if confirm "Stop container $1? "; then
docker stop $1
fi
else
echo -e "Container $1 is not running."
fi
fi
}
alias dstop="dkill"
function drm
{
# remove a container
if [ "$#" -ne 1 ]; then
echo -e "Usage: drm <container>"
else
if confirm "Remove container $1? "; then
docker rm $1
fi
fi
}
function dcommit
{
if [ "$#" -ne 2 ]; then
echo -e "Usage: dcommit <container> <image_name>"
else
if ! drunning $1; then
docker commit $1 $2
else
echo -e "Unable to commit. The container $1 is still running. Stop it first."
fi
fi
}