zoukankan      html  css  js  c++  java
  • 如何从头构建一个只有bash的镜像

    说明:本次生成的镜像是centos7版本的

    从官网下载对应的脚本:

    https://github.com/moby/moby/blob/master/contrib/mkimage-yum.sh

    上面这个脚本就是centos官网用于创建docker镜像的

    1、脚本具体的信息如下:

    #!/usr/bin/env bash
    #
    # Create a base CentOS Docker image.
    #
    # This script is useful on systems with yum installed (e.g., building
    # a CentOS image on CentOS).  See contrib/mkimage-rinse.sh for a way
    # to build CentOS images on other systems.
    
    set -e
    
    usage() {
        cat << EOOPTS
    $(basename $0) [OPTIONS] <name>
    OPTIONS:
      -p "<packages>"  The list of packages to install in the container.
                       The default is blank. Can use multiple times.
      -g "<groups>"    The groups of packages to install in the container.
                       The default is "Core". Can use multiple times.
      -y <yumconf>     The path to the yum config to install packages from. The
                       default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
      -t <tag>         Specify Tag information.
                       default is reffered at /etc/{redhat,system}-release
    EOOPTS
        exit 1
    }
    
    # option defaults
    yum_config=/etc/yum.conf
    if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
        yum_config=/etc/dnf/dnf.conf
        alias yum=dnf
    fi
    # for names with spaces, use double quotes (") as install_groups=('Core' '"Compute Node"')
    install_groups=()
    install_packages=()
    version=
    while getopts ":y:p:g:t:h" opt; do
        case $opt in
            y)
                yum_config=$OPTARG
                ;;
            h)
                usage
                ;;
            p)
                install_packages+=("$OPTARG")
                ;;
            g)
                install_groups+=("$OPTARG")
                ;;
            t)
                version="$OPTARG"
                ;;
            ?)
                echo "Invalid option: -$OPTARG"
                usage
                ;;
        esac
    done
    shift $((OPTIND - 1))
    name=$1
    
    if [[ -z $name ]]; then
        usage
    fi
    
    # default to Core group if not specified otherwise
    if [ ${#install_groups[*]} -eq 0 ]; then
        install_groups=('Core')
    fi
    
    target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
    
    set -x
    
    mkdir -m 755 "$target"/dev
    mknod -m 600 "$target"/dev/console c 5 1
    mknod -m 600 "$target"/dev/initctl p
    mknod -m 666 "$target"/dev/full c 1 7
    mknod -m 666 "$target"/dev/null c 1 3
    mknod -m 666 "$target"/dev/ptmx c 5 2
    mknod -m 666 "$target"/dev/random c 1 8
    mknod -m 666 "$target"/dev/tty c 5 0
    mknod -m 666 "$target"/dev/tty0 c 4 0
    mknod -m 666 "$target"/dev/urandom c 1 9
    mknod -m 666 "$target"/dev/zero c 1 5
    
    # amazon linux yum will fail without vars set
    if [ -d /etc/yum/vars ]; then
        mkdir -p -m 755 "$target"/etc/yum
        cp -a /etc/yum/vars "$target"/etc/yum/
    fi
    
    if [[ -n "$install_groups" ]]; then
        yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs 
            --setopt=group_package_types=mandatory -y groupinstall "${install_groups[@]}"
    fi
    
    if [[ -n "$install_packages" ]]; then
        yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs 
            --setopt=group_package_types=mandatory -y install "${install_packages[@]}"
    fi
    
    yum -c "$yum_config" --installroot="$target" -y clean all
    
    cat > "$target"/etc/sysconfig/network << EOF
    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    EOF
    
    # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
    #  locales
    rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
    #  docs and man pages
    rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
    #  cracklib
    rm -rf "$target"/usr/share/cracklib
    #  i18n
    rm -rf "$target"/usr/share/i18n
    #  yum cache
    rm -rf "$target"/var/cache/yum
    mkdir -p --mode=0755 "$target"/var/cache/yum
    #  sln
    rm -rf "$target"/sbin/sln
    #  ldconfig
    rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
    mkdir -p --mode=0755 "$target"/var/cache/ldconfig
    
    if [ -z "$version" ]; then
        for file in "$target"/etc/{redhat,system}-release; do
            if [ -r "$file" ]; then
                version="$(sed 's/^[^0-9]*([0-9.]+).*$/1/' "$file")"
                break
            fi
        done
    fi
    
    if [ -z "$version" ]; then
        echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
        version=$name
    fi
    
    tar --numeric-owner -c -C "$target" . | docker import - $name:$version
    
    docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
    
    rm -rf "$target"

    该脚本的具体用法如下:

    bash 脚本名字 -p "包名" -g "组名"  镜像名字

    2、修改官方的脚本

    修改以后脚本如下:

    #!/usr/bin/env bash
    #
    # Create a base CentOS Docker image.
    #
    # This script is useful on systems with yum installed (e.g., building
    # a CentOS image on CentOS).  See contrib/mkimage-rinse.sh for a way
    # to build CentOS images on other systems.
    
    set -e
    
    usage() {
        cat << EOOPTS
    $(basename $0) [OPTIONS] <name>
    OPTIONS:
      -p "<packages>"  The list of packages to install in the container.
                       The default is blank. Can use multiple times.
      -g "<groups>"    The groups of packages to install in the container.
                       The default is "Core". Can use multiple times.
      -y <yumconf>     The path to the yum config to install packages from. The
                       default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
      -t <tag>         Specify Tag information.
                       default is reffered at /etc/{redhat,system}-release
    EOOPTS
        exit 1
    }
    
    # option defaults
    yum_config=/etc/yum.conf
    if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
        yum_config=/etc/dnf/dnf.conf
        alias yum=dnf
    fi
    # for names with spaces, use double quotes (") as install_groups=('Core' '"Compute Node"')
    install_groups=()
    install_packages=()
    version=
    while getopts ":y:p:g:t:h" opt; do
        case $opt in
            y)
                yum_config=$OPTARG
                ;;
            h)
                usage
                ;;
            p)
                install_packages+=("$OPTARG")
                ;;
            g)
                install_groups+=("$OPTARG")
                ;;
            t)
                version="$OPTARG"
                ;;
            ?)
                echo "Invalid option: -$OPTARG"
                usage
                ;;
        esac
    done
    shift $((OPTIND - 1))
    name=$1
    
    if [[ -z $name ]]; then
        usage
    fi
    
    # default to Core group if not specified otherwise
    #if [ ${#install_groups[*]} -eq 0 ]; then
    #    install_groups=('Core')
    #fi
    
    target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
    
    set -x
    
    #mkdir -m 755 "$target"/dev
    #mknod -m 600 "$target"/dev/console c 5 1
    #mknod -m 600 "$target"/dev/initctl p
    #mknod -m 666 "$target"/dev/full c 1 7
    #mknod -m 666 "$target"/dev/null c 1 3
    #mknod -m 666 "$target"/dev/ptmx c 5 2
    #mknod -m 666 "$target"/dev/random c 1 8
    #mknod -m 666 "$target"/dev/tty c 5 0
    #mknod -m 666 "$target"/dev/tty0 c 4 0
    #mknod -m 666 "$target"/dev/urandom c 1 9
    #mknod -m 666 "$target"/dev/zero c 1 5
    
    # amazon linux yum will fail without vars set
    #if [ -d /etc/yum/vars ]; then
    #    mkdir -p -m 755 "$target"/etc/yum
    #    cp -a /etc/yum/vars "$target"/etc/yum/
    #fi
    
    #if [[ -n "$install_groups" ]]; then
    #    yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs 
    #        --setopt=group_package_types=mandatory -y groupinstall "${install_groups[@]}"
    #fi
    
    if [[ -n "$install_packages" ]]; then
        yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs 
            --setopt=group_package_types=mandatory -y install "${install_packages[@]}"
    fi
    
    yum -c "$yum_config" --installroot="$target" -y clean all
    
    #cat > "$target"/etc/sysconfig/network << EOF
    #NETWORKING=yes
    #HOSTNAME=localhost.localdomain
    #EOF
    
    # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
    #  locales
    rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
    #  docs and man pages
    rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
    #  cracklib
    rm -rf "$target"/usr/share/cracklib
    #  i18n
    rm -rf "$target"/usr/share/i18n
    #  yum cache
    rm -rf "$target"/var/cache/yum
    #mkdir -p --mode=0755 "$target"/var/cache/yum
    #  sln
    rm -rf "$target"/sbin/sln
    #  ldconfig
    rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
    mkdir -p --mode=0755 "$target"/var/cache/ldconfig
    
    if [ -z "$version" ]; then
        for file in "$target"/etc/{redhat,system}-release; do
            if [ -r "$file" ]; then
                version="$(sed 's/^[^0-9]*([0-9.]+).*$/1/' "$file")"
                break
            fi
        done
    fi
    
    if [ -z "$version" ]; then
        echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
        version=$name
    fi
    
    #tar --numeric-owner -c -C "$target"      . | docker import - $name:$version
    tar -cvf centos7_bash_new.tar   "$target"
    #docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
    
    rm -rf "$target"

    该脚本删除了官方脚本内部默认安装的Core组

    脚本执行:

    bash 脚本名字  -p "bash" centos7  // centos7这个没有实际的用处了,,这里只是为了脚本不报错,没来的既修改脚本

    说明 :上述的命令会只执行安装bash,然后在当前的目录下生成centos7_bash_new.tar包,

    使用刚才生成的包创建docker镜像

    # cat create_centos_new.sh |docker import - centos7_bash:7.7.1908
    2、1查看生成的镜像

  • 相关阅读:
    【转】myeclipse中连接mysql数据库
    struts2入门示例(hello world)
    【转】MyEclipse第一个Servlet程序
    学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
    typeof关键字
    SHLVL--shell终端深度
    stack
    queue
    getopt--parse command line options
    怎样实时判断socket连接状态?
  • 原文地址:https://www.cnblogs.com/yjt1993/p/12803765.html
Copyright © 2011-2022 走看看