-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·109 lines (89 loc) · 2.37 KB
/
build.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
#!/usr/bin/env bash
## Copyright (c) 2022, DURUYAO. All rights reserved.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## date: 2022-01-20
## author: duruao@gmail.com
## desc: build a new dock-duck image with one more development kit layer than the base image
set -euo pipefail
function error_ln() {
printf "\033[1;32;31m%s\n\033[m" "$1"
}
function warning_ln() {
printf "\033[1;33m%s\n\033[m" "$1"
}
function info_ln() {
printf "\033[0;32;32m%s\n\033[m" "$1"
}
function show_usage() {
cat <<EOF
Usage: $0 [OPTIONS] IMAGE
Build a new dock-duck image with one more development kit layer than the base image
Options:
-h, --help Display this help message
--name STRING Assign a name to the new image (default: "IMAGE:TAG-dk")
See more about DockDuck at https://github.com/duruyao/DockDuck
EOF
}
image=""
name=""
if [ -z "$(command -v docker)" ]; then
error_ln "Error: 'docker' required but not found" >&2
show_usage >&2
exit 1
fi
## parse arguments
while (($#)); do
case "$1" in
-h | --help)
show_usage
exit 0
;;
--name)
name="$2"
shift 2
;;
--* | -*)
error_ln "Error: Unknown flag: '$1'" >&2
show_usage >&2
exit 1
;;
*)
repo="${1//\:*/}"
tag="${1//*\:/}"
if ! echo "$1" | grep -q ":"; then
tag="latest"
fi
if [ -z "$(docker images -q "${repo}:${tag}")" ]; then
error_ln "Error: No such docker image: '${repo}:${tag}'" >&2
show_usage >&2
exit 1
fi
image="$1"
if [ -z "${name}" ]; then
name="${repo}:${tag}-dk"
fi
shift 1
;;
esac
done
if [ -z "${image}" ]; then
error_ln "Error: Missing IMAGE" >&2
show_usage >&2
exit 1
fi
## change Dockerfile
sed -i "/FROM .*/d" "${PWD}"/Dockerfile
sed -i "1i FROM ${image}" "${PWD}"/Dockerfile
set -x
docker build -t "${name}" -f "${PWD}"/Dockerfile "${PWD}"