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