Docker Base Image自己创建具体实现

2020-06-17 06:38:09易采站长站整理

Docker Base Image 创建

            本着学习的态度,想了解创建 Docker Base Image的方法,在官网,在百度,在相关论坛,查找相关资料,这里记录下实现的步骤,

一. 环境

宿主机操作系统: OS X  ,需要安装VirtualBox  ;
容器环境:centos7

二. 用VirtualBox 安装系统,这里以centos 7为例(CentOS-7-x86_64-Minimal-1503-01.iso)

创建虚拟机,并安装centos7,以下记录了详细安装过程。

* 安装过程简单,我这里网速太慢了,就不上图了,有需要的朋友留个邮箱,我发pdf。*

安装完后,重启后进入系统。

三. 修改网络配置


修改网络配置 /etc/sysconfig/network-script/ifcfg-enp0s3。
删除 UUID,HWADDR ;
修改ONBOOT=no 为 ONBOOT=yes , 然后保存 。

$ ifconfig
Cannot find a valid baseurl for repo: base/7/x86_6

重启网络接口:
[root@centos7 ~]#ifdown enps03
[root@centos7 ~]#ifup enps03

$ yum install ifconfig
提示:Nothing to do
通过” yum provides” 命令列出那个包提供ifconfig命令
$ yum provides ifconfig
// 安装
$ yum -y install net-tools

四. 安装Docker


安装 EPEL (参考:http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/ )
[root@centos7 ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

修改/etc/yum.repos.d/CentOS-Base.repo 把enabled=0 改成enabled=1
vim /etc/yum.repos.d/CentOS-Base.repo
[centosplus]enabled=1

安装 Docker
[root@centos7 yum.repos.d]# yum install docker-io

启动 Docker
[root@centos7 yum.repos.d]# service docker start

五. 创建基础镜像


替换变量
把 /etc/yum.repos.d/CentOS-Base.repo 文件中
$releasever 替换成 7
$basearch 替换成 x86_64

快速替换方法:
vim /etc/yum.repos.d/CentOS-Base.repo
:%s/$releasever/7/g
:%s/$basearch/x86_64/g

创建Docker image 生成脚本
[root@centos7 yum.repos.d]# cd /root
[root@centos7 ~]# mkdir scripts
[root@centos7 ~]# cd scripts/
[root@centos7 scripts]# vim createimage.sh
然后把 https://github.com/docker/docker/blob/master/contrib/mkimage-yum.sh 文件中内容粘贴进去

[root@centos7 scripts]# chmod +x createimage.sh

创建image
[root@centos7 scripts]# ./createimage.sh centos7base

创建成功后查看,并把当前运行的容器删除掉
[root@centos7 tmp]# docker images