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