forked from DrSnowbird/intellij-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·163 lines (142 loc) · 5.73 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
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
162
#!/bin/bash
set -e
# Reference:
# - https://docs.docker.com/engine/userguide/containers/dockerimages/
# - https://github.com/dockerfile/java/blob/master/oracle-java8/Dockerfile
if [ $# -lt 1 ]; then
echo "-------------------------------------------------------------------------------------------"
echo "Usage: "
echo " ${0} [<Dockerfile> <imageTag> [<some more optional arguments...>] ] "
echo "e.g."
echo " ./build.sh ./centos/Dockerfile.centos.xfce.vnc openkbs/centos-xfce-vnc --no-cache --build-arg OS_TYPE=centos'"
echo " ./build.sh ./Dockerfile.ubuntu.xfce.vnc openkbs/ubuntu-xfce-vnc --no-cache --build-arg OS_TYPE=centos'"
echo "-------------------------------------------------------------------------------------------"
fi
MY_DIR=$(dirname "$(readlink -f "$0")")
DOCKERFILE=${1:-./Dockerfile}
DOCKERFILE=$(realpath $DOCKERFILE)
imageTag=${2}
if [ $# -gt 2 ]; then
shift 2
options="$*"
else
options=""
fi
###################################################
#### ---- Change this only if want to use your own
###################################################
ORGANIZATION=stefaneicher
###################################################
#### ---- Detect Docker Run Env files ----
###################################################
function detectDockerBuildEnvFile() {
curr_dir=`pwd`
if [ -s "${DOCKER_ENV_FILE}" ]; then
echo "--- INFO: Docker Build Environment file '${DOCKER_ENV_FILE}' FOUND!"
else
echo "*** WARNING: Docker Build Environment file '${DOCKER_ENV_FILE}' NOT found!"
echo "*** WARNING: Searching for .env or docker.env as alternative!"
echo "*** --->"
if [ -s "./docker-build.env" ]; then
echo "--- INFO: ./docker-build.env FOUND to use as Docker Run Environment file!"
DOCKER_ENV_FILE="./docker-build.env"
else
if [ -s "./.env" ]; then
echo "--- INFO: ./.env FOUND to use as Docker Run Environment file!"
DOCKER_ENV_FILE="./.env"
else
echo "--- INFO: ./.env Docker Environment file (.env) NOT found!"
if [ -s "./docker.env" ]; then
echo "--- INFO: ./docker.env FOUND to use as Docker Run Environment file!"
DOCKER_ENV_FILE="./docker.env"
else
echo "*** WARNING: Docker Environment file (.env) or (docker.env) NOT found!"
fi
fi
fi
fi
}
detectDockerBuildEnvFile
###################################################
#### ---- Container package information ----
###################################################
DOCKER_IMAGE_REPO=`echo $(basename $PWD)|tr '[:upper:]' '[:lower:]'|tr "/: " "_" `
imageTag=${imageTag:-"${ORGANIZATION}/${DOCKER_IMAGE_REPO}"}
###################################################
#### ---- Generate build-arg arguments ----
###################################################
BUILD_ARGS=""
BUILD_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`"
VCS_REF="`git rev-parse --short HEAD`"
VCS_URL="https://github.com/`echo $(basename $PWD)`"
BUILD_ARGS="--build-arg BUILD_DATE=${BUILD_DATE} --build-arg VCS_REF=${VCS_REF}"
## -- ignore entries start with "#" symbol --
function generateBuildArgs() {
for r in `cat ${DOCKER_ENV_FILE} | grep -v '^#'`; do
echo "entry=> $r"
key=`echo $r | tr -d ' ' | cut -d'=' -f1`
value=`echo $r | tr -d ' ' | cut -d'=' -f2`
BUILD_ARGS="${BUILD_ARGS} --build-arg $key=$value"
done
}
generateBuildArgs
echo "BUILD_ARGS=${BUILD_ARGS}"
###################################################
#### ---- Setup Docker Build Proxy ----
###################################################
# export NO_PROXY="localhost,127.0.0.1,.openkbs.org"
# export HTTP_PROXY="http://gatekeeper-w.openkbs.org:80"
# when using "wget", add "--no-check-certificate" to avoid https certificate checking failures
#
echo "... Setup Docker Build Proxy: ..."
PROXY_PARAM=
function generateProxyArgs() {
if [ "${HTTP_PROXY}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg HTTP_PROXY=${HTTP_PROXY}"
fi
if [ "${HTTPS_PROXY}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
fi
if [ "${NO_PROXY}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg NO_PROXY=\"${NO_PROXY}\""
fi
if [ "${http_proxy}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg HTTP_PROXY=${http_proxy}"
fi
if [ "${https_proxy}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg HTTPS_PROXY=${https_proxy}"
fi
if [ "${no_proxy}" != "" ]; then
PROXY_PARAM="${PROXY_PARAM} --build-arg NO_PROXY=\"${no_proxy}\""
fi
BUILD_ARGS="${BUILD_ARGS} ${PROXY_PARAM}"
}
generateProxyArgs
echo "BUILD_ARGS=> ${BUILD_ARGS}"
###################################################
#### ---- Build Container ----
###################################################
set -x
echo "========> imageTag: ${imageTag}"
docker build --rm -t ${imageTag} \
${BUILD_ARGS} \
${options} \
-f ${DOCKERFILE} .
set +x
echo "----> Shell into the Container in interactive mode: "
echo " docker exec -it --name <some-name> /bin/bash"
echo "e.g."
echo " docker run --name "my-$(basename $imageTag)" /bin/bash "
echo "----> Run: "
echo " docker run --name <some-name> -it ${imageTag} /bin/bash"
echo "e.g."
echo " docker run --name "my-$(basename $imageTag)" ${imageTag} "
echo "----> Run in interactive mode: "
echo " docker run -it --name <some-name> ${imageTag} /bin/bash"
echo "e.g."
echo " docker run -it --name "my-$(basename $imageTag)" -it ${imageTag} "
echo "----> Build Docker Images again: "
echo "To build again: (there is a dot at the end of the command!)"
echo " docker build -t ${imageTag} . "
echo
docker images |grep "$imageTag"