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