LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

如何制作操作系統鏡像(docker)

admin
2025年6月29日 15:3 本文熱度 548

使用docker的時候,經常需要借助一些基礎鏡像來進一步二次定制,比如centos鏡像、ubunt鏡像,那么如果要自己定義一個基礎鏡像應該如何做呢,有兩種方法,這兩種方法都是參考網絡上以及自己實踐后確實可用的方法,本例子以麒麟操作系統為例子,推薦方法二

方法一:逐步操作
1、創建任意一個目錄并進入此目錄(不可在/root下創建),命令如下:
mkdir -p /opt/kylin && cd /opt/kylini
2、創建基礎目錄,命令如下:
mkdir usr dev
3、copy系統相關文件到usr路徑下,命令如下:
cp -a /usr/lib /usr/lib64 /usr/bin /usr/share usr/
4、按照當前系統來創建軟鏈,命令如下:
ln -s usr/lib libln -s usr/lib64 lib64ln -s usr/bin bin
5、復制配置文件以及root目錄,如下:
cp -a /etc /root ./
6、刪除一些非必要腳本,否則執行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
7、全部操作完成后文件目錄結構如圖所示:
8、執行chroot . 命令,測試鏡像目錄是否有問題,已進入容器,如圖:
9、將整個目錄打包,命令如下:
cd ..tar -C kylin/ -zc . -f kylin-docker.tar.gz 或者tar -zcvf kylin-docker.tar.gz -C kylin/ .
10、編寫Dockerfile文件,定制鏡像,如下:
FROM scratchADD 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"]
11、構建鏡像,命令如下:
docker build -t kylin-v10 .
12、構建完成后,查看鏡像,如圖:
從上圖可以看出,通過此方法制作的鏡像偏大
方法二,直接通過腳本(來源GitHub并二次修改),推薦此種方法制作
1、bash文件內容如下:
#!/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}-releaseEOOPTS  exit 1}
# option defaultsyum_config=/etc/yum.confif [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then  yum_config=/etc/dnf/dnf.conf  alias yum=dnffi# 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      ;;  esacdoneshift $((OPTIND - 1))name=$1
if [[ -z $name ]]; then  usagefi
# default to Core group if not specified otherwiseif [ ${#install_groups[*]} -eq 0 ]; then  install_groups=('Core')fi
target=$(mktemp -d --tmpdir=/root $(basename $0).XXXXXX)
set -x
mkdir -m 755 "$target"/devmknod -m 600 "$target"/dev/console c 5 1mknod -m 600 "$target"/dev/initctl pmknod -m 666 "$target"/dev/full c 1 7mknod -m 666 "$target"/dev/null c 1 3mknod -m 666 "$target"/dev/ptmx c 5 2mknod -m 666 "$target"/dev/random c 1 8mknod -m 666 "$target"/dev/tty c 5 0mknod -m 666 "$target"/dev/tty0 c 4 0mknod -m 666 "$target"/dev/urandom c 1 9mknod -m 666 "$target"/dev/zero c 1 5
# amazon linux yum will fail without vars setif [ -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 << EOFNETWORKING=yesHOSTNAME=localhost.localdomainEOF
# effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".#  localesrm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}#  docs and man pagesrm -rf "$target"/usr/share/{man,doc,info,gnome/help}#  cracklibrm -rf "$target"/usr/share/cracklib#  i18nrm -rf "$target"/usr/share/i18n#  yum cacherm -rf "$target"/var/cache/yummkdir -p --mode=0755 "$target"/var/cache/yum#  slnrm -rf "$target"/sbin/sln#  ldconfigrm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfigmkdir -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  donefi
if [ -z "$version" ]; then  echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"  version=$namefi
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行需要修改為自己系統的release,可通過cat /etc/kylin-release命令查看
2、執行bash文件,命令如下:
./mkimage-yum.sh -y /etc/yum.conf kylinv10
3、執行完成后,查看鏡像大小,如圖:
從上圖可以看出,通過腳本方式做成的鏡像體積更小一些,如果想體積更小一些,可通過刪除系統內不用的軟件包實現,具體可根據需求自行實現
至此,制作基礎鏡像完成!


閱讀原文:原文鏈接


該文章在 2025/7/1 23:22:07 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
婷婷视频在线播放免费观看 | 日日摸夜夜摸狠狠摸中文字幕 | 日本中文字幕第一页 | 亚洲美女在线一区 | 天天免费看国产一区二二区 | 一区二区三区四区激情另类 |