github.com/SamWhited/moby@v1.13.1/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] [--compression algo| --no-compression] 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 echo >&2 " $mkimg -t someuser/solaris solaris" 15 exit 1 16 } 17 18 scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")/mkimage" 19 20 os= 21 os=$(uname -o) 22 23 # set up path to gnu tools if solaris 24 [[ $os == "Solaris" ]] && export PATH=/usr/gnu/bin:$PATH 25 # TODO check for gnu-tar, gnu-getopt 26 27 # TODO requires root/sudo due to some pkg operations. sigh. 28 [[ $os == "Solaris" && $EUID != "0" ]] && echo >&2 "image create on Solaris requires superuser privilege" 29 30 optTemp=$(getopt --options '+d:t:c:hC' --longoptions 'dir:,tag:,compression:,no-compression,help' --name "$mkimg" -- "$@") 31 eval set -- "$optTemp" 32 unset optTemp 33 34 dir= 35 tag= 36 compression="auto" 37 while true; do 38 case "$1" in 39 -d|--dir) dir="$2" ; shift 2 ;; 40 -t|--tag) tag="$2" ; shift 2 ;; 41 --compression) compression="$2" ; shift 2 ;; 42 --no-compression) compression="none" ; shift 1 ;; 43 -h|--help) usage ;; 44 --) shift ; break ;; 45 esac 46 done 47 48 script="$1" 49 [ "$script" ] || usage 50 shift 51 52 if [ "$compression" == 'auto' ] || [ -z "$compression" ] 53 then 54 compression='xz' 55 fi 56 57 [ "$compression" == 'none' ] && compression='' 58 59 if [ ! -x "$scriptDir/$script" ]; then 60 echo >&2 "error: $script does not exist or is not executable" 61 echo >&2 " see $scriptDir for possible scripts" 62 exit 1 63 fi 64 65 # don't mistake common scripts like .febootstrap-minimize as image-creators 66 if [[ "$script" == .* ]]; then 67 echo >&2 "error: $script is a script helper, not a script" 68 echo >&2 " see $scriptDir for possible scripts" 69 exit 1 70 fi 71 72 delDir= 73 if [ -z "$dir" ]; then 74 dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)" 75 delDir=1 76 fi 77 78 rootfsDir="$dir/rootfs" 79 ( set -x; mkdir -p "$rootfsDir" ) 80 81 # pass all remaining arguments to $script 82 "$scriptDir/$script" "$rootfsDir" "$@" 83 84 # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them 85 rm -rf "$rootfsDir/dev" "$rootfsDir/proc" 86 mkdir -p "$rootfsDir/dev" "$rootfsDir/proc" 87 88 # make sure /etc/resolv.conf has something useful in it 89 mkdir -p "$rootfsDir/etc" 90 cat > "$rootfsDir/etc/resolv.conf" <<'EOF' 91 nameserver 8.8.8.8 92 nameserver 8.8.4.4 93 EOF 94 95 tarFile="$dir/rootfs.tar${compression:+.$compression}" 96 touch "$tarFile" 97 98 ( 99 set -x 100 tar --numeric-owner --create --auto-compress --file "$tarFile" --directory "$rootfsDir" --transform='s,^./,,' . 101 ) 102 103 echo >&2 "+ cat > '$dir/Dockerfile'" 104 cat > "$dir/Dockerfile" <<EOF 105 FROM scratch 106 ADD $(basename "$tarFile") / 107 EOF 108 109 # if our generated image has a decent shell, let's set a default command 110 for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do 111 if [ -x "$rootfsDir/$shell" ]; then 112 ( set -x; echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile" ) 113 break 114 fi 115 done 116 117 ( set -x; rm -rf "$rootfsDir" ) 118 119 if [ "$tag" ]; then 120 ( set -x; docker build -t "$tag" "$dir" ) 121 elif [ "$delDir" ]; then 122 # 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_ 123 ( set -x; docker build "$dir" ) 124 fi 125 126 if [ "$delDir" ]; then 127 ( set -x; rm -rf "$dir" ) 128 fi