github.com/feiyang21687/docker@v1.5.0/contrib/mkimage-yum.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Create a base CentOS Docker image.
     4  #
     5  # This script is useful on systems with yum installed (e.g., building
     6  # a CentOS image on CentOS).  See contrib/mkimage-rinse.sh for a way
     7  # to build CentOS images on other systems.
     8  
     9  usage() {
    10      cat <<EOOPTS
    11  $(basename $0) [OPTIONS] <name>
    12  OPTIONS:
    13    -y <yumconf>  The path to the yum config to install packages from. The
    14                  default is /etc/yum.conf.
    15  EOOPTS
    16      exit 1
    17  }
    18  
    19  # option defaults
    20  yum_config=/etc/yum.conf
    21  while getopts ":y:h" opt; do
    22      case $opt in
    23          y)
    24              yum_config=$OPTARG
    25              ;;
    26          h)
    27              usage
    28              ;;
    29          \?)
    30              echo "Invalid option: -$OPTARG"
    31              usage
    32              ;;
    33      esac
    34  done
    35  shift $((OPTIND - 1))
    36  name=$1
    37  
    38  if [[ -z $name ]]; then
    39      usage
    40  fi
    41  
    42  #--------------------
    43  
    44  target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
    45  
    46  set -x
    47  
    48  mkdir -m 755 "$target"/dev
    49  mknod -m 600 "$target"/dev/console c 5 1
    50  mknod -m 600 "$target"/dev/initctl p
    51  mknod -m 666 "$target"/dev/full c 1 7
    52  mknod -m 666 "$target"/dev/null c 1 3
    53  mknod -m 666 "$target"/dev/ptmx c 5 2
    54  mknod -m 666 "$target"/dev/random c 1 8
    55  mknod -m 666 "$target"/dev/tty c 5 0
    56  mknod -m 666 "$target"/dev/tty0 c 4 0
    57  mknod -m 666 "$target"/dev/urandom c 1 9
    58  mknod -m 666 "$target"/dev/zero c 1 5
    59  
    60  yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
    61      --setopt=group_package_types=mandatory -y groupinstall Core
    62  yum -c "$yum_config" --installroot="$target" -y clean all
    63  
    64  cat > "$target"/etc/sysconfig/network <<EOF
    65  NETWORKING=yes
    66  HOSTNAME=localhost.localdomain
    67  EOF
    68  
    69  # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb
    70  # --keep-services "$target".  Stolen from mkimage-rinse.sh
    71  #  locales
    72  rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
    73  #  docs
    74  rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
    75  #  cracklib
    76  rm -rf "$target"/usr/share/cracklib
    77  #  i18n
    78  rm -rf "$target"/usr/share/i18n
    79  #  sln
    80  rm -rf "$target"/sbin/sln
    81  #  ldconfig
    82  rm -rf "$target"/etc/ld.so.cache
    83  rm -rf "$target"/var/cache/ldconfig/*
    84  
    85  version=
    86  if [ -r "$target"/etc/redhat-release ]; then
    87      version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$target"/etc/redhat-release)"
    88  fi
    89  
    90  if [ -z "$version" ]; then
    91      echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
    92      version=$name
    93  fi
    94  
    95  tar --numeric-owner -c -C "$target" . | docker import - $name:$version
    96  docker run -i -t $name:$version echo success
    97  
    98  rm -rf "$target"