-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-ctl.sh
executable file
·161 lines (146 loc) · 4.12 KB
/
docker-ctl.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
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#
#
# Docker helper script: build, push, run, bash, stop
# Maintainer: David Ryder
#
# Requires:
# jq - https://stedolan.github.io/jq/download/
#
CMD_LIST=${@:-"help"} # build | build-push | run | bash | stop
if [ -d $DOCKER_TAG_NAME ]; then
echo "Docker image tag name envvar not set: DOCKER_TAG_NAME"
echo "export DOCKER_TAG_NAME=..."
exit 1
fi
# Check docker: Linux or Ubuntu snap
DOCKER_CMD=`which docker`
DOCKER_CMD=${DOCKER_CMD:-"/snap/bin/microk8s.docker"}
echo "Using: "$DOCKER_CMD
if [ -d $DOCKER_CMD ]; then
echo "Docker is missing: "$DOCKER_CMD
exit 1
fi
# Check jq
JQ_CMD=`which jq`
if [ -d $JQ_CMD ]; then
echo "jq is missing: "$JQ_CMD
echo "Install jq: https://stedolan.github.io/jq/download/ "
exit 1
fi
_getDockerContainerId() {
IMAGE_NAME=${1:-"Image Name Missing"}
DOCKER_ID=`docker container ps --format '{{json .}}' \
| jq --arg SEARCH_STR "$IMAGE_NAME" 'select(.Image==$SEARCH_STR)' \
| jq -s '[.[] | {ID, Names, Image } ][0]' \
| jq -r .ID`
echo $DOCKER_ID
}
_getDockerImageId() {
REPOSITORY_NAME=${1:-"Repository Name Missing"}
echo "Repository "$REPOSITORY_NAME
DOCKER_ID=`docker images --format '{{json .}}' \
| jq --arg SEARCH_STR "$REPOSITORY_NAME" 'select(.Repository==$SEARCH_STR )' \
| jq -s '[.[] | {Repository, ID} ][0]' \
| jq -r .ID`
echo $DOCKER_ID
}
_dockerPrune() {
$DOCKER_CMD system prune -f
}
_dockerBuild() {
echo "Building image: "$DOCKER_TAG_NAME
$DOCKER_CMD build -t $DOCKER_TAG_NAME .
}
_dockerPush() {
$DOCKER_CMD login -u $DOCKER_REPOSITORY_ACCOUNT -p $DOCKER_REPOSITORY_PWD
$DOCKER_CMD push $DOCKER_REPOSITORY_TAG_NAME
}
_dockerRun() {
# Adds ports -p 8080:8080 -p 15672:15672
# Adds RW volume on host
echo "Docker running $DOCKER_TAG_NAME"
$DOCKER_CMD run --rm --detach \
--hostname $DOCKER_TAG_NAME"_dock" \
--volume /tmp/dock-$DOCKER_TAG_NAME:/$DOCKER_TAG_NAME:rw \
-it \
$DOCKER_TAG_NAME
}
_dockerWaitUntilRunning() {
CID="null"
while [ "$CID" == "null" ];
do
echo "Waiting for container to start: $DOCKER_TAG_NAME $CID"
sleep 1
CID=$(_getDockerContainerId $DOCKER_TAG_NAME)
done
echo "Container Started: $DOCKER_TAG_NAME $CID"
}
_dockerWaitUntilStopped() {
CID="null"
while [ "$CID" != "null" ];
do
echo "Waiting for container to stop: $DOCKER_TAG_NAME $CID"
sleep 1
CID=$(_getDockerContainerId $DOCKER_TAG_NAME)
done
echo "Container Stopped: $DOCKER_TAG_NAME $CID"
}
_dockerBash() {
CID=$(_getDockerContainerId ${DOCKER_TAG_NAME})
echo "Container ID $CID for ${DOCKER_TAG_NAME}"
$DOCKER_CMD exec -it $(_getDockerContainerId ${DOCKER_TAG_NAME}) /bin/bash
}
_dockerStop() {
CONTAINER_ID=`_getDockerContainerId ${DOCKER_TAG_NAME}`
if [ "$CONTAINER_ID" != "" ]; then
echo "Stop ${DOCKER_TAG_NAME} ${CONTAINER_ID}"
docker stop ${CONTAINER_ID} &
sleep 5 # some time for container to stop
else
echo "Container ${DOCKER_TAG_NAME} is not running"
fi
}
_dockerDeleteImage() {
IMAGE_ID=`_getDockerImageId ${DOCKER_TAG_NAME}`
echo ${DOCKER_TAG_NAME} $IMAGE_ID
if [ "$IMAGE_ID" != "" ]; then
echo "Deleting image ${DOCKER_TAG_NAME} ${IMAGE_ID}"
docker rmi ${IMAGE_ID}
else
echo "Image ${DOCKER_TAG_NAME} not found"
fi
}
_runCommand() {
CMD=${1:-"help"}
echo "Running [$CMD]"
if [ $CMD == "build" ]; then
_dockerBuild
elif [ $CMD == "prune" ]; then
_dockerPrune
elif [ $CMD == "build-push" ]; then
DOCKER_REPOSITORY_TAG_NAME=$DOCKER_REPOSITORY_ACCOUNT/$DOCKER_TAG_NAME
echo "Push image to repository $DOCKER_REPOSITORY_ACCOUNT DOCKER_REPOSITORY_TAG_NAME"
_dockerBuild
_dockerPush
elif [ $CMD == "run" ]; then
_dockerRun
_dockerWaitUntilRunning
elif [ $CMD == "bash" ]; then
_dockerBash
elif [ $CMD == "stop" ]; then
_dockerStop
_dockerWaitUntilStopped
elif [ $CMD == "delete" ]; then
_dockerDeleteImage
elif [ $CMD == "sync" ]; then
echo rsync -v -a $SRC_DIR $DST_DIR
else
echo "Commands: build | build-push | run | bash | stop | delete | prune"
exit 1
fi
}
for CMD in $CMD_LIST
do
_runCommand $CMD
done