2014-05-01 17:38:44 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
|
|
|
mkimg="$(basename "$0")"
|
|
|
|
|
|
|
|
usage() {
|
2015-07-16 04:44:58 -04:00
|
|
|
echo >&2 "usage: $mkimg [-d dir] [-t tag] [--compression algo| --no-compression] script [script-args]"
|
2014-05-01 17:38:44 -04:00
|
|
|
echo >&2 " ie: $mkimg -t someuser/debian debootstrap --variant=minbase jessie"
|
2014-10-01 17:09:54 -04:00
|
|
|
echo >&2 " $mkimg -t someuser/ubuntu debootstrap --include=ubuntu-minimal --components=main,universe trusty"
|
2014-05-01 17:38:44 -04:00
|
|
|
echo >&2 " $mkimg -t someuser/busybox busybox-static"
|
|
|
|
echo >&2 " $mkimg -t someuser/centos:5 rinse --distribution centos-5"
|
2014-05-31 06:58:58 -04:00
|
|
|
echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4"
|
|
|
|
echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4 --mirror=http://somemirror/"
|
2014-05-01 17:38:44 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")/mkimage"
|
|
|
|
|
2016-06-07 03:45:21 -04:00
|
|
|
os=
|
|
|
|
os=$(uname -o)
|
|
|
|
|
2015-06-19 17:46:54 -04:00
|
|
|
optTemp=$(getopt --options '+d:t:c:hC' --longoptions 'dir:,tag:,compression:,no-compression,help' --name "$mkimg" -- "$@")
|
2014-05-01 17:38:44 -04:00
|
|
|
eval set -- "$optTemp"
|
|
|
|
unset optTemp
|
|
|
|
|
|
|
|
dir=
|
|
|
|
tag=
|
2015-06-19 17:46:54 -04:00
|
|
|
compression="auto"
|
2014-05-01 17:38:44 -04:00
|
|
|
while true; do
|
|
|
|
case "$1" in
|
2020-03-02 22:27:49 -05:00
|
|
|
-d | --dir)
|
|
|
|
dir="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
-t | --tag)
|
|
|
|
tag="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--compression)
|
|
|
|
compression="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--no-compression)
|
|
|
|
compression="none"
|
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
-h | --help) usage ;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
2014-05-01 17:38:44 -04:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
script="$1"
|
|
|
|
[ "$script" ] || usage
|
|
|
|
shift
|
|
|
|
|
2020-03-02 22:27:49 -05:00
|
|
|
if [ "$compression" == 'auto' ] || [ -z "$compression" ]; then
|
|
|
|
compression='xz'
|
2015-06-19 17:46:54 -04:00
|
|
|
fi
|
|
|
|
|
2015-07-13 16:55:14 -04:00
|
|
|
[ "$compression" == 'none' ] && compression=''
|
2015-06-19 17:46:54 -04:00
|
|
|
|
2014-05-01 17:38:44 -04:00
|
|
|
if [ ! -x "$scriptDir/$script" ]; then
|
|
|
|
echo >&2 "error: $script does not exist or is not executable"
|
|
|
|
echo >&2 " see $scriptDir for possible scripts"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# don't mistake common scripts like .febootstrap-minimize as image-creators
|
|
|
|
if [[ "$script" == .* ]]; then
|
|
|
|
echo >&2 "error: $script is a script helper, not a script"
|
|
|
|
echo >&2 " see $scriptDir for possible scripts"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
delDir=
|
|
|
|
if [ -z "$dir" ]; then
|
2014-06-27 08:43:12 -04:00
|
|
|
dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)"
|
2014-05-01 17:38:44 -04:00
|
|
|
delDir=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
rootfsDir="$dir/rootfs"
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
mkdir -p "$rootfsDir"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
|
|
|
|
# pass all remaining arguments to $script
|
|
|
|
"$scriptDir/$script" "$rootfsDir" "$@"
|
|
|
|
|
|
|
|
# Docker mounts tmpfs at /dev and procfs at /proc so we can remove them
|
|
|
|
rm -rf "$rootfsDir/dev" "$rootfsDir/proc"
|
|
|
|
mkdir -p "$rootfsDir/dev" "$rootfsDir/proc"
|
|
|
|
|
|
|
|
# make sure /etc/resolv.conf has something useful in it
|
|
|
|
mkdir -p "$rootfsDir/etc"
|
2020-03-02 22:27:49 -05:00
|
|
|
cat > "$rootfsDir/etc/resolv.conf" << 'EOF'
|
2014-05-01 17:38:44 -04:00
|
|
|
nameserver 8.8.8.8
|
|
|
|
nameserver 8.8.4.4
|
|
|
|
EOF
|
|
|
|
|
2015-07-13 16:55:14 -04:00
|
|
|
tarFile="$dir/rootfs.tar${compression:+.$compression}"
|
2014-05-01 17:38:44 -04:00
|
|
|
touch "$tarFile"
|
|
|
|
|
|
|
|
(
|
|
|
|
set -x
|
2015-06-19 17:46:54 -04:00
|
|
|
tar --numeric-owner --create --auto-compress --file "$tarFile" --directory "$rootfsDir" --transform='s,^./,,' .
|
2014-05-01 17:38:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
echo >&2 "+ cat > '$dir/Dockerfile'"
|
2020-03-02 22:27:49 -05:00
|
|
|
cat > "$dir/Dockerfile" << EOF
|
2014-05-01 17:38:44 -04:00
|
|
|
FROM scratch
|
2015-07-13 16:55:14 -04:00
|
|
|
ADD $(basename "$tarFile") /
|
2014-05-01 17:38:44 -04:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# if our generated image has a decent shell, let's set a default command
|
|
|
|
for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do
|
|
|
|
if [ -x "$rootfsDir/$shell" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
rm -rf "$rootfsDir"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
|
|
|
|
if [ "$tag" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
docker build -t "$tag" "$dir"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
elif [ "$delDir" ]; then
|
|
|
|
# if we didn't specify a tag and we're going to delete our dir, let's just build an untagged image so that we did _something_
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
docker build "$dir"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$delDir" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
rm -rf "$dir"
|
|
|
|
)
|
2014-05-01 17:38:44 -04:00
|
|
|
fi
|