如何创建CentOS系统的Docker基础镜像?
如何创建CentOS系统的Docker基础镜像?
在使用Docker容器时,我们经常需要基于一些基础镜
像(Base Image)来构建符合自己要求的镜像。通常,我们会使用DockerHub或其他镜像市场提供的基础镜像。虽然
这样很方便,但是我们并不知道第三方提供的基础镜像的实现细节和安全性,若在生产环境贸然使用这些基础镜像,则可能会带来技术隐患。因此,有必要创建自己的基础镜像!本文将详述创建CentOS系统(6.9版本和7.4版本)的Docker基础镜像的两种方法,分别是:通过tar打包已有的CentOS系统,然后导入Docker镜像仓库;通过
mkimage-yum.sh脚本,使用yum下载和安装必要的软件包,然后导入Docker镜像仓库。本文会使用两台虚拟机,分别
安装CentOS 6.9系统和CentOS 7.4系统。一、环境描述1. 虚拟机-1CPU:双核内存:4 GB硬盘:120 GB操作系统:CentOS 6.9 x86_64安装方式:MinimalIP地址:
172.17.243.1692. 虚拟机-2操作系统:CentOS 7.4.1708
x86_64IP地址:172.17.243.168二、通过tar打包构建基础镜像Step-1 卸载不必要的软件包为了缩小文件系统的体积,需要删除一些不必要的软件包。在Shell中执行以下命令:CentOS 6.9系统(虚拟机-1):yum remove -y iwl* ql* xorg* ipw* *firmware* --exclude=kernel-firmwareCentOS
7.4.1708系统(虚拟机-2):yum remove -y iwl* *firmware* --exclude=kernel-firmwareStep-2 清除yum缓存清除yum 缓存,进一步缩小文件系统的体积。在Shell中执行以下命令:yum clean allyum clean allrm -rf /var/cache/yumStep-3 打包文件系统将文件系统打包,排除所有运行时才创建的目录,以及不必要的目录。在Shell中执行以下命令:tar
--numeric-owner --exclude=/proc --exclude=/sys
--exclude=/mnt --exclude=/var/cache
--exclude=/usr/share/{foomatic,backgrounds,perl5,fonts,cu ps,qt4,groff,kde4,icons,pixmaps,emacs,gnome-backgroun d-properties,sounds,gnome,games,desktop-directories}
--exclude=/var/log -zcvf
/mnt/CentOS-6.9-BaseImage.tar.gz /tar --numeric-owner --exclude=/proc --exclude=/sys --exclude=/mnt
--exclude=/var/cache
--exclude=/usr/share/{foomatic,backgrounds,perl5,fonts,cu ps,qt4,groff,kde4,icons,pixmaps,emacs,gnome-backgroun d-properties,sounds,gnome,games,desktop-directories}
--exclude=/var/log -zcvf
/mnt/CentOS-7.4-BaseImage.tar.gz /Step-4 安装和启动Docker安装CentOS的EPEL源和REMI源,然后安装Docker软件包,最后启动Docker服务。在Shell中运行一
下命令:# 安装EPEL源和REMI源rpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/pub/epel/6/x86_64/epel-release -6-8.noarch.rpmrpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/enterprise/remi-release-6.rpm#
安装Docker软件包yum install -y docker-io# 启动Docker
服务service docker start# 安装EPEL源和REMI源rpm
-Uvh
https://https://www.360docs.net/doc/0a13489758.html,/pub/epel/7/x86_64/e/epel-relea se-7-10.noarch.rpmrpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/enterprise/remi-release-7.rpm#
安装Docker软件包yum install -y docker-io# 启动Docker
服务systemctl start docker.serviceStep-5 导入镜像仓库将打包的文件系统,导入Docker的本地镜像仓库。在Shell
中运行以下命令:cat /mnt/CentOS-6.9-BaseImage.tar.gz | docker import - centos-tar:6.9cat
/mnt/CentOS-7.4-BaseImage.tar.gz | docker import - centos-tar:7.4.1708Step-6 验证检查Docker的本地镜像仓库,若如下图所示,则表明镜像创建成功:通过tar打包创
建的CentOS 6.9基础镜像通过tar打包创建的CentOS 7.4基础镜像运行一个示例容器,在控制台中输出提示信息,如下图所示:通过tar打包创建的CentOS 6.9基础镜像的运行示例通过tar打包创建的CentOS 7.4基础镜像的运行示例三、
通过mkimage-yum.sh脚本构建基础镜像Step-1 安装第三方源安装CentOS的EPEL源和REMI源。在Shell中运行以下命令:rpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/pub/epel/6/x86_64/epel-release -6-8.noarch.rpmrpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/enterprise/remi-release-6.rpmrp m -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/pub/epel/7/x86_64/e/epel-relea se-7-10.noarch.rpmrpm -Uvh
https://https://www.360docs.net/doc/0a13489758.html,/enterprise/remi-release-7.rpmSt ep-2 安装和启动Docker安装Docker软件包,然后启动Docker服务。在Shell中运行以下命令:yum install -y docker-ioservice docker startyum install -y
docker-iosystemctl start docker.serviceStep-3 创建脚本文件新建mkimage-yum.sh脚本,这个文件就是Docker官方的创建镜像的脚本文件,URL为:
https://https://www.360docs.net/doc/0a13489758.html,/moby/moby/blob/master/contrib/mkimag e-yum.sh在Shell中运行以下命令:vi
mkimage-yum.shCentOS 6.9和CentOS 7.4的脚本内容相同,如下所示:#!/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 -eusage() {cat $(basename $0) [OPTIONS] OPTIONS:-p '' The list of packages to install in the container.The default is blank.-g '' The groups of packages to install in the container.The default is
'Core'.-y The path to the yum config to install packages from. Thedefault is /etc/yum.conf for Centos/RHEL and
/etc/dnf/dnf.conf for FedoraEOOPTSexit 1}# option defaultsyum_config=/etc/yum.confif [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null;
thenyum_config=/etc/dnf/dnf.confalias
yum=dnffiinstall_groups='Core'while getopts ':y:p:g:h' opt; docase $opt
iny)yum_config=$OPTARG;;h)usagep)install_packages='$ OPTARG'g)install_groups='$OPTARG'\?)echo 'Invalid option: -$OPTARG'esacdoneshift $((OPTIND -
1))name=$1if [[ -z $name ]]; thentarget=$(mktemp -d
--tmpdir $(basename $0).XXXXXX)set -xmkdir -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 ]; thenmkdir -p -m 755
'$target'/etc/yumcp -a /etc/yum/vars '$target'/etc/yum/if [[ -n '$install_groups' ]];thenyum -c '$yum_config'
--installroot='$target' --releasever=/
--setopt=tsflags=nodocs
\--setopt=group_package_types=mandatory -y groupinstall '$install_groups'if [[ -n
'$install_packages' ]];--setopt=group_package_types=man datory -y install '$install_packages'yum -c '$yum_config'
--installroot='$target' -y clean allcat >
'$target'/etc/sysconfig/network
NETWORKING=yesHOSTNAME=localhost.localdomainE OF# effectively: febootstrap-minimize --keep-zoneinfo
--keep-rpmdb --keep-services '$target'.# localesrm -rf
'$target'/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localed ef,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/ldconfigversion=for file in
'$target'/etc/{redhat,system}-releasedoif [ -r '$file' ]; thenversion='$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/'
'$file')'breakif [ -z '$version' ]; thenecho >&2 'warning: cannot autodetect OS version, using '$name' as
tag'version=$nametar --numeric-owner -c -C '$target' . | docker import - $name:$versiondocker run -i -t --rm $name:$version /bin/bash -c 'echo success'rm -rf
'$target'Step-4 导入镜像仓库运行mkimage-yum.sh脚本,创建CentOS的基础镜像,CentOS 6.9和CentOS 7.4的命令相同:chmod 755 mkimage-yum.sh./mkimage-yum.sh centos-mkimageStep-5 验证通过mkimage-yum.sh脚本创建的CentOS 6.9基础镜像通过mkimage-yum.sh脚本创建的CentOS 7.4基础镜像通过mkimage-yum.sh脚本创建的CentOS 6.9基础镜像的示例通过mkimage-yum.sh脚本创建的CentOS 7.4基础镜像的示例四、两种方法对比在虚拟机-1中查看两种方法创建的CentOS 6.9的基础镜像,如下
图所示:两种创建CentOS 6.9基础镜像的方法对比在虚拟机-2中查看两种方法创建的CentOS 7.4的基础镜像,如下图所示:两种创建CentOS 7.4基础镜像的方法对比由上述两图可知,通过tar打包创建的基础镜像的体积大约是通过mkimage-yum.sh脚本创建的基础镜像的3倍左右。这是由于前者是基于最小化安装的CentOS系统而创建的,而后者是从CentOS官方源安装必要的软件包而创建的,前者比后者多安装了很多软件包,包括邮件工具、设备驱动程序,等等。这两种方法各有特点:通过tar打包创建:基础镜像体积较大,但可以按照需求自由定制,适用于各种版本的CentOS系统,灵活度较高。通过mkimage-yum.sh脚本创建:基础镜像体积较小,但只能安装官方源支持的软件包,并且只适用于最新版本的CentOS系统。用户可以根据自己的需求特点,选择本文介绍的一种方法来创建属于自己的CentOS基础镜像!