创建CentOS系统的Docker基础镜像

Step-1 卸载不必要的软件包

为了缩小文件系统的体积,需要删除一些不必要的软件包。在Shell中执行以下命令:

CentOS 6.9系统(虚拟机-1):

yum remove -y iwl* ql* xorg* ipw* *firmware* --exclude=kernel-firmware

CentOS 7.4.1708系统(虚拟机-2):

yum remove -y iwl* *firmware* --exclude=kernel-firmware

Step-2 清除yum缓存

清除yum缓存,进一步缩小文件系统的体积。在Shell中执行以下命令:

yum clean all

yum clean allrm -rf /var/cache/yum

Step-3 打包文件系统

将文件系统打包,排除所有运行时才创建的目录,以及不必要的目录。在Shell中执行以下命令:

tar --numeric-owner --exclude=/proc --exclude=/sys --exclude=/mnt --exclude=/var/cache --exclude=/usr/share/{foomatic,backgrounds,perl5,fonts,cups,qt4,groff,kde4,icons,pixmaps,emacs,gnome-background-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,cups,qt4,groff,kde4,icons,pixmaps,emacs,gnome-background-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://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-6.rpm# 安装Docker软件包yum install -y docker-io# 启动Docker服务service docker start

# 安装EPEL源和REMI源rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpmrpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm# 安装Docker软件包yum install -y docker-io# 启动Docker服务systemctl start docker.service

Step-5 导入镜像仓库

将打包的文件系统,导入Docker的本地镜像仓库。在Shell中运行以下命令:

cat /mnt/CentOS-6.9-BaseImage.tar.gz | docker import - centos-tar:6.9

cat /mnt/CentOS-7.4-BaseImage.tar.gz | docker import - centos-tar:7.4.1708

Step-6 验证

检查Docker的本地镜像仓库,若如下图所示,则表明镜像创建成功:

通过tar打包创建的CentOS 6.9基础镜像

通过tar打包创建的CentOS 7.4基础镜像

运行一个示例容器,在控制台中输出提示信息,如下图所示:

通过tar打包创建的CentOS 6.9基础镜像的运行示例

通过tar打包创建的CentOS 7.4基础镜像的运行示例

 

 

使用:

git clone https://github.com/jpetazzo/pipework
cp pipework/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework

vim /etc/sysconfig/network-scripts/ifcfg-em1 
vim /etc/sysconfig/network-scripts/ifcfg-br0

systemctl restart network

docker images

docker run -it --name centos6.8 --net=none b7feffaa0bdc /bin/bash
docker ps -a
docker start fc017c4eb893
pipework br0 fc017c4eb893 192.168.10.48/[email protected]
docker exec fc017c4eb893 ip add
docker exec -it fc017c4eb893 /bin/bash
docker ps -a

 

docker run -h datanode7 -e TZ='Asia/Shanghai' --storage-opt size=1024G --name centos6.8 -it --net=none fcca04213152 /bin/bash
docker start 1d45a29e2b81
pipework br0 centos6.8 192.168.10.45/[email protected]
docker exec centos6.8 ip add
docker exec -it centos6.8 /bin/bash
docker ps -a

 

你可能感兴趣的:(创建CentOS系统的Docker基础镜像)