原創(chuàng) 宮關 運維DevOps 2023-06-29 10:00 發(fā)表于廣東 在使用docker的時候,經常需要借助一些基礎鏡像來進一步二次定制,比如centos鏡像、ubunt鏡像,那么如果要自己定義一個基礎鏡像應該如何做呢,有兩種方法,這兩種方法都是參考網絡上以及自己實踐后確實可用的方法,本例子以麒麟操作系統(tǒng)為例子,推薦方法二 1、創(chuàng)建任意一個目錄并進入此目錄(不可在/root下創(chuàng)建),命令如下:
mkdir -p /opt/kylin && cd /opt/kylini 3、copy系統(tǒng)相關文件到usr路徑下,命令如下:
cp -a /usr/lib /usr/lib64 /usr/bin /usr/share usr/ 4、按照當前系統(tǒng)來創(chuàng)建軟鏈,命令如下:
ln -s usr/lib libln -s usr/lib64 lib64ln -s usr/bin bin 6、刪除一些非必要腳本,否則執(zhí)行chroot .會提示Error: /proc must be mounted,如下:
rm -rf etc/profile.d/flatpak.sh etc/profile.d/gawk.csh etc/profile.d/gawk.sh etc/profile.d/lang.csh etc/profile.d/lang.sh etc/profile.d/system-info.sh 8、執(zhí)行chroot . 命令,測試鏡像目錄是否有問題,已進入容器,如圖:
cd ..tar -C kylin/ -zc . -f kylin-docker.tar.gz 或者tar -zcvf kylin-docker.tar.gz -C kylin/ . 10、編寫Dockerfile文件,定制鏡像,如下:
FROM scratch ADD kylin-docker.tar.gz / LABEL \ org.label-schema.schema-version="1.0" \ org.label-schema.name="Kylin Base Image" \ org.label-schema.vendor="Kylin" \ org.label-schema.license="GPLv2" \ org.label-schema.build-date="20230629" \ org.opencontainers.image.title="Kylin v10 Image" \ org.opencontainers.image.vendor="Kylin" \ org.opencontainers.image.licenses="GPL-2.0-only" \ org.opencontainers.image.created="2022-06-29 00:00:00+00:00" CMD ["/bin/bash"]
docker build -t kylin-v10 . 方法二,直接通過腳本(來源GitHub并二次修改),推薦此種方法制作#!/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).
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 referred 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=/root $(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/{kylin,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"
注意:第129行需要修改為自己系統(tǒng)的release,可通過cat /etc/kylin-release命令查看./mkimage-yum.sh -y /etc/yum.conf kylinv10 從上圖可以看出,通過腳本方式做成的鏡像體積更小一些,如果想體積更小一些,可通過刪除系統(tǒng)內不用的軟件包實現(xiàn),具體可根據(jù)需求自行實現(xiàn)
|