-
Notifications
You must be signed in to change notification settings - Fork 106
/
strip-docker-image-export
executable file
·178 lines (162 loc) · 4.05 KB
/
strip-docker-image-export
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
#!/bin/bash
# NAME
# strip-docker-image-export - exports the bare essentials from a Docker image
#
# SYNOPSIS
# strip-docker-image-export [-d export-dir ] [-p package | -f file] [-v]
#
#
# OPTIONS
# -d export-directory to copy content to, defaults to /export.
# -p package package to include from image, multiple -p allowed.
# -f file file to include from image, multiple -f allowed.
# -v verbose
#
# DESCRIPTION
# this script copies all the files from an installed package and copies them
# to an export directory. Additional files can be added. When an executable
# is copied, all dynamic libraries required by the executed are included too.
#
# EXAMPLE
# The following example strips the nginx installation from the default NGiNX docker image,
# and allows the files in ./export to be added to a scratch image.
#
# docker run -v $PWD/export/:/export \
# -v $PWD/bin:/mybin nginx \
# /mybin/strip-image.sh \
# -p nginx \
# -f /etc/passwd \
# -f /etc/group \
# -f '/lib/*/libnss*' \
# -f /bin/ls \
# -f /bin/cat \
# -f /bin/sh \
# -f /bin/mkdir \
# -f /bin/ps \
# -f /var/run \
# -f /var/log/nginx \
# -d /export
# CAVEATS
# requires an image that has a bash, readlink and ldd installed.
#
# AUTHOR
# Mark van Holsteijn
#
# COPYRIGHT
#
# Copyright 2015 Xebia Nederland B.V.
#
# 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.
#
export EXPORT_DIR=/export
function usage() {
echo "usage: $(basename $0) [-v] [-d export-dir ] [-p package | -f file]" >&2
echo " $@" >&2
}
function parse_commandline() {
while getopts "vp:f:d:" OPT; do
case "$OPT" in
v)
VERBOSE=v
;;
p)
PACKAGES="$PACKAGES $OPTARG"
;;
f)
FILES="$FILES $OPTARG"
;;
d)
EXPORT_DIR="$OPTARG"
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$PACKAGES" -a -z "$FILES" ] ; then
usage "Missing -p or -f options"
exit 1
fi
if [ ! -d $EXPORT_DIR ] ; then
usage "$EXPORT_DIR is not a directory."
exit 1
fi
}
function print_file() {
if [ "$1" = "/usr/bin/ldd" ]; then
exit
fi
if [ -e "$1" ] ; then
echo "$1"
else
test -n "$VERBOSE" && echo "INFO: ignoring not existent file '$1'" >&2
fi
if [ -s "$1" ] ; then
TARGET=$(readlink "$1")
if [ -n "$TARGET" ] ; then
if expr "$TARGET" : '^/' >/dev/null 2>&1 ; then
list_dependencies "$TARGET"
else
list_dependencies $(dirname "$1")/"$TARGET"
fi
fi
fi
}
function list_dependencies() {
for FILE in $@ ; do
if [ -e "$FILE" ] ; then
print_file "$FILE"
if /usr/bin/ldd "$FILE" >/dev/null 2>&1 ; then
/usr/bin/ldd "$FILE" | \
awk '/statically/{next;} /=>/ { print $3; next; } { print $1 }' | \
while read LINE ; do
test -n "$VERBOSE" && echo "INFO: including $LINE" >&2
print_file "$LINE"
done
fi
else
test -n "$VERBOSE" && echo "INFO: ignoring not existent file $FILE" >&2
fi
done
}
function list_packages() {
if test -e /usr/bin/dpkg; then
DEPS=$(/usr/bin/dpkg -L $1)
elif test -e /usr/bin/rpm; then
DEPS=$(/usr/bin/rpm -ql $1)
elif test -e /sbin/apk; then
DEPS=$(/sbin/apk info -L $1 | grep -Ev '^$|contains:' | sed 's/^/\//g')
else
echo 'WARN: Unknown OS, aborted list_packages()'
exit
fi
while read FILE ; do
if [ ! -d "$FILE" ] ; then
list_dependencies "$FILE"
fi
done <<< "$DEPS"
}
function list_all_packages() {
for i in "$@" ; do
list_packages "$i"
done
}
parse_commandline "$@"
tar czf - $(
(
list_all_packages $PACKAGES
list_dependencies $FILES
) | sort -u
) | ( cd $EXPORT_DIR ; tar -xzh${VERBOSE}f - )