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