github.com/reds/docker@v1.11.2-rc1/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    -p "<packages>"  The list of packages to install in the container.
    14                     The default is blank.
    15    -g "<groups>"    The groups of packages to install in the container.
    16                     The default is "Core".
    17    -y <yumconf>     The path to the yum config to install packages from. The
    18                     default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
    19  EOOPTS
    20      exit 1
    21  }
    22  
    23  # option defaults
    24  yum_config=/etc/yum.conf
    25  if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
    26  	yum_config=/etc/dnf/dnf.conf
    27  	alias yum=dnf
    28  fi
    29  install_groups="Core"
    30  while getopts ":y:p:g:h" opt; do
    31      case $opt in
    32          y)
    33              yum_config=$OPTARG
    34              ;;
    35          h)
    36              usage
    37              ;;
    38          p)
    39              install_packages="$OPTARG"
    40              ;;
    41          g)
    42              install_groups="$OPTARG"
    43              ;;
    44          \?)
    45              echo "Invalid option: -$OPTARG"
    46              usage
    47              ;;
    48      esac
    49  done
    50  shift $((OPTIND - 1))
    51  name=$1
    52  
    53  if [[ -z $name ]]; then
    54      usage
    55  fi
    56  
    57  target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
    58  
    59  set -x
    60  
    61  mkdir -m 755 "$target"/dev
    62  mknod -m 600 "$target"/dev/console c 5 1
    63  mknod -m 600 "$target"/dev/initctl p
    64  mknod -m 666 "$target"/dev/full c 1 7
    65  mknod -m 666 "$target"/dev/null c 1 3
    66  mknod -m 666 "$target"/dev/ptmx c 5 2
    67  mknod -m 666 "$target"/dev/random c 1 8
    68  mknod -m 666 "$target"/dev/tty c 5 0
    69  mknod -m 666 "$target"/dev/tty0 c 4 0
    70  mknod -m 666 "$target"/dev/urandom c 1 9
    71  mknod -m 666 "$target"/dev/zero c 1 5
    72  
    73  # amazon linux yum will fail without vars set
    74  if [ -d /etc/yum/vars ]; then
    75  	mkdir -p -m 755 "$target"/etc/yum
    76  	cp -a /etc/yum/vars "$target"/etc/yum/
    77  fi
    78  
    79  if [[ -n "$install_groups" ]];
    80  then
    81      yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
    82          --setopt=group_package_types=mandatory -y groupinstall $install_groups
    83  fi
    84  
    85  if [[ -n "$install_packages" ]];
    86  then
    87      yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
    88          --setopt=group_package_types=mandatory -y install $install_packages
    89  fi
    90  
    91  yum -c "$yum_config" --installroot="$target" -y clean all
    92  
    93  cat > "$target"/etc/sysconfig/network <<EOF
    94  NETWORKING=yes
    95  HOSTNAME=localhost.localdomain
    96  EOF
    97  
    98  # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
    99  #  locales
   100  rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
   101  #  docs and man pages
   102  rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
   103  #  cracklib
   104  rm -rf "$target"/usr/share/cracklib
   105  #  i18n
   106  rm -rf "$target"/usr/share/i18n
   107  #  yum cache
   108  rm -rf "$target"/var/cache/yum
   109  mkdir -p --mode=0755 "$target"/var/cache/yum
   110  #  sln
   111  rm -rf "$target"/sbin/sln
   112  #  ldconfig
   113  rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
   114  mkdir -p --mode=0755 "$target"/var/cache/ldconfig
   115  
   116  version=
   117  for file in "$target"/etc/{redhat,system}-release
   118  do
   119      if [ -r "$file" ]; then
   120          version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
   121          break
   122      fi
   123  done
   124  
   125  if [ -z "$version" ]; then
   126      echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
   127      version=$name
   128  fi
   129  
   130  tar --numeric-owner -c -C "$target" . | docker import - $name:$version
   131  
   132  docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
   133  
   134  rm -rf "$target"