github.com/jandre/docker@v1.7.0/contrib/mkimage.sh (about) 1 #!/usr/bin/env bash 2 set -e 3 4 mkimg="$(basename "$0")" 5 6 usage() { 7 echo >&2 "usage: $mkimg [-d dir] [-t tag] script [script-args]" 8 echo >&2 " ie: $mkimg -t someuser/debian debootstrap --variant=minbase jessie" 9 echo >&2 " $mkimg -t someuser/ubuntu debootstrap --include=ubuntu-minimal --components=main,universe trusty" 10 echo >&2 " $mkimg -t someuser/busybox busybox-static" 11 echo >&2 " $mkimg -t someuser/centos:5 rinse --distribution centos-5" 12 echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4" 13 echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4 --mirror=http://somemirror/" 14 exit 1 15 } 16 17 scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")/mkimage" 18 19 optTemp=$(getopt --options '+d:t:h' --longoptions 'dir:,tag:,help' --name "$mkimg" -- "$@") 20 eval set -- "$optTemp" 21 unset optTemp 22 23 dir= 24 tag= 25 while true; do 26 case "$1" in 27 -d|--dir) dir="$2" ; shift 2 ;; 28 -t|--tag) tag="$2" ; shift 2 ;; 29 -h|--help) usage ;; 30 --) shift ; break ;; 31 esac 32 done 33 34 script="$1" 35 [ "$script" ] || usage 36 shift 37 38 if [ ! -x "$scriptDir/$script" ]; then 39 echo >&2 "error: $script does not exist or is not executable" 40 echo >&2 " see $scriptDir for possible scripts" 41 exit 1 42 fi 43 44 # don't mistake common scripts like .febootstrap-minimize as image-creators 45 if [[ "$script" == .* ]]; then 46 echo >&2 "error: $script is a script helper, not a script" 47 echo >&2 " see $scriptDir for possible scripts" 48 exit 1 49 fi 50 51 delDir= 52 if [ -z "$dir" ]; then 53 dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)" 54 delDir=1 55 fi 56 57 rootfsDir="$dir/rootfs" 58 ( set -x; mkdir -p "$rootfsDir" ) 59 60 # pass all remaining arguments to $script 61 "$scriptDir/$script" "$rootfsDir" "$@" 62 63 # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them 64 rm -rf "$rootfsDir/dev" "$rootfsDir/proc" 65 mkdir -p "$rootfsDir/dev" "$rootfsDir/proc" 66 67 # make sure /etc/resolv.conf has something useful in it 68 mkdir -p "$rootfsDir/etc" 69 cat > "$rootfsDir/etc/resolv.conf" <<'EOF' 70 nameserver 8.8.8.8 71 nameserver 8.8.4.4 72 EOF 73 74 tarFile="$dir/rootfs.tar.xz" 75 touch "$tarFile" 76 77 ( 78 set -x 79 tar --numeric-owner -caf "$tarFile" -C "$rootfsDir" --transform='s,^./,,' . 80 ) 81 82 echo >&2 "+ cat > '$dir/Dockerfile'" 83 cat > "$dir/Dockerfile" <<'EOF' 84 FROM scratch 85 ADD rootfs.tar.xz / 86 EOF 87 88 # if our generated image has a decent shell, let's set a default command 89 for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do 90 if [ -x "$rootfsDir/$shell" ]; then 91 ( set -x; echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile" ) 92 break 93 fi 94 done 95 96 ( set -x; rm -rf "$rootfsDir" ) 97 98 if [ "$tag" ]; then 99 ( set -x; docker build -t "$tag" "$dir" ) 100 elif [ "$delDir" ]; then 101 # 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_ 102 ( set -x; docker build "$dir" ) 103 fi 104 105 if [ "$delDir" ]; then 106 ( set -x; rm -rf "$dir" ) 107 fi