forked from whatwedo/docker-base-images
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-builder.sh
More file actions
executable file
·195 lines (170 loc) · 6.07 KB
/
docker-builder.sh
File metadata and controls
executable file
·195 lines (170 loc) · 6.07 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
#########################################################################
# #
# docker-builder.sh is a script for managing complex docker images #
# It provivides an easy mechanism for creating and building docker #
# images #
# #
# Author: Felix Imobersteg #
# Email: felix@whatwedo.ch #
# Website: http://whatwedo.ch #
# #
#########################################################################
########################################################
# CHECK PREREQUISITES #
########################################################
# Exit on error
set -e
# Check software
dockerTest=$(which docker)
m4Test=$(which m4)
[[ -z "$dockerTest" && "$1" -ne "lint-file" && "$1" -ne "lint-files" ]] && { echo "docker doesn't appear to be installed - this is required for script to run"; exit 1; }
[[ -z "$m4Test" && "$1" -ne "lint-file" && "$1" -ne "lint-files" ]] && { echo "m4 doesn't appear to be installed - this is required for script to run"; exit 1; }
# Set directory
cd "$(dirname "$0")"
########################################################
# MAIN #
########################################################
# build all dockerfiles
build-files() {
rm -rf "dist"
for file in images/*.m4; do
name="${file##*/}"
name="${name%.*}"
build-file $name
done
}
# build the given dockerfile
build-file() {
rm -rf "dist/$1"
mkdir -p "dist/$1"
cp -R files "dist/$1"
echo "
##################################################
# #
# DO NOT EDIT THIS FILE MANUALLY #
# AUTOMATICALLY CREATED WITH docker-builder.sh #
# #
##################################################
" > "dist/$1/Dockerfile"
lastline=""
templine=""
while read -r line; do
if [[ $line == RUN* ]] && [[ $templine == RUN* ]] ;
then
echo "$lastline && \\" >> "dist/$1/Dockerfile"
templine=$line
lastline=$(echo "$line" | sed 's/^RUN //g' )
elif [[ $line == LASTRUN* ]] && [[ $templine == RUN* ]] ;
then
line=$(echo "$line" | sed 's/^LASTRUN\ /RUN\ /g' )
echo "$lastline" >> "dist/$1/Dockerfile"
templine=$line
lastline=$line
else
line=$(echo "$line" | sed 's/^LASTRUN\ /RUN\ /g' )
echo "$lastline" >> "dist/$1/Dockerfile"
templine=$line
lastline=$line
fi
done <<< "$(m4 -I "modules/*.m4" "images/$1.m4" | grep -v '^\#' | grep -v '^\s*$')"
echo "$lastline" >> "dist/$1/Dockerfile"
echo "LABEL ch.whatwedo.image.base=\"whatwedo/$1\"" >> "dist/$1/Dockerfile"
cp "images/$1.md" "dist/$1/README.md"
}
# build the given image
build-image() {
cd "dist/$1"
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" docker build --no-cache --rm -t "$1:latest" .
cd ../..
if [[ $1 == "base" ]]; then
docker-squash $1 -t $1
else
docker-squash $1 -t $1 -f whatwedo/base:latest
fi
}
# build the given image with cache
build-cached-image() {
cd "dist/$1"
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" docker build --rm -t "$1:latest" .
cd ../..
}
# Publish the given image
publish-image() {
VERSION="$(git describe --tags `git rev-list --tags --max-count=1`)"
docker tag $1 whatwedo/$1:$VERSION
docker push whatwedo/$1:$VERSION
docker tag $1 whatwedo/$1:latest
docker push whatwedo/$1:latest
}
# download latest base images
update-base-images() {
docker pull ubuntu:14.04
docker pull whatwedo/base:latest
}
# lint all dockerfiles
lint-files() {
echo "start"
for file in dist/*; do
name="${file##*/}"
echo ""
echo ""
echo "##########################################"
echo "# Linting image $name"
echo "##########################################"
echo ""
lint-file $name
done
echo ""
echo ""
}
# Lint file with hadolint
lint-file() {
cd "dist/$1"
if [[ -f /root/.local/bin/hadolint ]]; then
/root/.local/bin/hadolint ./Dockerfile
else
docker run --rm -i -v `pwd`:/data lukasmartinelli/hadolint hadolint /data/Dockerfile || true
# allow failure to not interrupt the ci task
fi
cd ../..
}
if [ "$1" = "build-files" ]; then
build-files
elif [ "$1" = "build-file" ]; then
[ -z "$2" ] && { echo "Image name not specified"; exit 1; }
build-file $2
elif [ "$1" = "build-image" ]; then
[ -z "$2" ] && { echo "Image name not specified"; exit 1; }
update-base-images
build-file $2
build-image $2
elif [ "$1" = "build-cached-image" ]; then
[ -z "$2" ] && { echo "Image name not specified"; exit 1; }
update-base-images
build-file $2
build-cached-image $2
elif [ "$1" = "publish-image" ]; then
[ -z "$2" ] && { echo "Image name not specified"; exit 1; }
publish-image $2
elif [ "$1" = "lint-file" ]; then
[ -z "$2" ] && { echo "Image name not specified"; exit 1; }
lint-file $2
elif [ "$1" = "lint-files" ]; then
lint-files
else
echo "
docker-builder.sh is a script for managing complex docker images
It provivides an easy mechanism for creating and building docker
images
USAGE:
./docker-builder.sh build-files - This will build all dockerfiles
./docker-builder.sh build-file [name] - This will build the given dockerfile
./docker-builder.sh build-images - This will build all images
./docker-builder.sh build-image [name] - This will build the given image
./docker-builder.sh build-cached-image [name] - This will build the given image with cache
./docker-builder.sh publish-image [name] - This will publish the given existing image
./docker-builder.sh lint-file [name] - This will linting a given Dockerfiles with hadolint
./docker-builder.sh lint-files [name] - This will lint all Dockerfiles
"
fi