2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
14 mkimg="$(basename "$0")"
17 echo >&2 "usage: $mkimg [-d dir] [-t tag] script [script-args]"
18 echo >&2 " ie: $mkimg -t someuser/debian debootstrap --variant=minbase jessie"
19 echo >&2 " $mkimg -t someuser/ubuntu debootstrap --include=ubuntu-minimal trusty"
20 echo >&2 " $mkimg -t someuser/busybox busybox-static"
21 echo >&2 " $mkimg -t someuser/centos:5 rinse --distribution centos-5"
25 scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
27 optTemp=$(getopt --options '+d:t:h' --longoptions 'dir:,tag:,help' --name "$mkimg" -- "$@")
28 eval set -- "$optTemp"
35 -d|--dir) dir="$2" ; shift 2 ;;
36 -t|--tag) tag="$2" ; shift 2 ;;
43 [ "$script" ] || usage
46 if [ ! -x "$scriptDir/$script" ]; then
47 echo >&2 "error: $script does not exist or is not executable"
48 echo >&2 " see $scriptDir for possible scripts"
52 # don't mistake common scripts like .febootstrap-minimize as image-creators
53 if [[ "$script" == .* ]]; then
54 echo >&2 "error: $script is a script helper, not a script"
55 echo >&2 " see $scriptDir for possible scripts"
60 if [ -z "$dir" ]; then
61 dir="$(mktemp -d ${TMPDIR:-/tmp}/docker-mkimage.XXXXXXXXXX)"
65 rootfsDir="$dir/rootfs"
66 ( set -x; mkdir -p "$rootfsDir" )
68 # pass all remaining arguments to $script
69 "$scriptDir/$script" "$rootfsDir" "$@"
71 # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them
72 rm -rf "$rootfsDir/dev" "$rootfsDir/proc"
73 mkdir -p "$rootfsDir/dev" "$rootfsDir/proc"
75 # make sure /etc/resolv.conf has something useful in it
76 mkdir -p "$rootfsDir/etc"
77 cat > "$rootfsDir/etc/resolv.conf" <<'EOF'
82 tarFile="$dir/rootfs.tar.xz"
87 tar --numeric-owner -caf "$tarFile" -C "$rootfsDir" --transform='s,^./,,' .
90 echo >&2 "+ cat > '$dir/Dockerfile'"
91 cat > "$dir/Dockerfile" <<'EOF'
96 # if our generated image has a decent shell, let's set a default command
97 for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do
98 if [ -x "$rootfsDir/$shell" ]; then
99 ( set -x; echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile" )
104 umount $rootfsDir/run/shm
105 ( set -x; rm -rf "$rootfsDir" )
108 ( set -x; docker build -t "$tag" "$dir" )
109 elif [ "$delDir" ]; then
110 # 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_
111 ( set -x; docker build "$dir" )
114 if [ "$delDir" ]; then
115 ( set -x; rm -rf "$dir" )