how to create owned centos base image on centos

简介:
一般情况下, 我们需要制作image的话, 总需要一个基本的image, 那么这个base image是哪里来的呢?
如果不自制的话, 那就得去docker HUB下载.

其实base image也可以自制, 参考docker提供的mkimage开头的脚本 : 

例如mkimage-yum.sh是在centos中制作centos base image的脚本.
例如我在CentOS 6.x x64中可以制作centos 6 x64最新版的image.
脚本内容如下 : 
最后一步tar --numeric-owner -c -C "$target" . | docker import - $name:$version是使用docker import导入到本地镜像库.
[root@150 ~]# vi build.sh
#!/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.

usage() {
    cat <<EOOPTS
$(basename $0) [OPTIONS] <name>
OPTIONS:
  -y <yumconf>  The path to the yum config to install packages from. The
                default is /etc/yum.conf.
EOOPTS
    exit 1
}

# option defaults
yum_config=/etc/yum.conf
while getopts ":y:h" opt; do
    case $opt in
        y)
            yum_config=$OPTARG
            ;;
        h)
            usage
            ;;
        \?)
            echo "Invalid option: -$OPTARG"
            usage
            ;;
    esac
done
shift $((OPTIND - 1))
name=$1

if [[ -z $name ]]; then
    usage
fi

#--------------------

target=$(mktemp -d --tmpdir $(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

yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
    --setopt=group_package_types=mandatory -y groupinstall Core
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".  Stolen from mkimage-rinse.sh
#  locales
rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
#  docs
rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
#  cracklib
rm -rf "$target"/usr/share/cracklib
#  i18n
rm -rf "$target"/usr/share/i18n
#  sln
rm -rf "$target"/sbin/sln
#  ldconfig
rm -rf "$target"/etc/ld.so.cache
rm -rf "$target"/var/cache/ldconfig/*

version=
if [ -r "$target"/etc/redhat-release ]; then
    version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$target"/etc/redhat-release)"
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 $name:$version echo success

rm -rf "$target"

yum的installroot用于将包安装到指定的root, 而不是本地.
man yum
       --installroot=root
              Specifies an alternative installroot, relative to which all packages will be installed.
              Configuration Option: installroot

[root@150 ~]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

跑这个脚本, 就可以创建centos6的镜像了, 自动tag为最新版本.
[root@150 ~]# chmod 500 ./build.sh
[root@150 ~]# ./build.sh 
build.sh [OPTIONS] <name>
OPTIONS:
  -y <yumconf>  The path to the yum config to install packages from. The
                default is /etc/yum.conf.

运行过程的输出提取如下 : 
[root@150 ~]# ./build.sh -y /etc/yum.conf centos6

+ mkdir -m 755 /tmp/build.sh.BEGiT6/dev
+ mknod -m 600 /tmp/build.sh.BEGiT6/dev/console c 5 1
+ mknod -m 600 /tmp/build.sh.BEGiT6/dev/initctl p
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/full c 1 7
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/null c 1 3
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/ptmx c 5 2
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/random c 1 8
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/tty c 5 0
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/tty0 c 4 0
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/urandom c 1 9
+ mknod -m 666 /tmp/build.sh.BEGiT6/dev/zero c 1 5
+ yum -c /etc/yum.conf --installroot=/tmp/build.sh.BEGiT6 --releasever=/ --setopt=tsflags=nodocs --setopt=group_package_types=mandatory -y groupinstall Core
..........................................................................................................................................................
Complete!
+ yum -c /etc/yum.conf --installroot=/tmp/build.sh.BEGiT6 -y clean all
Loaded plugins: fastestmirror, refresh-packagekit, security, versionlock
Cleaning repos: base extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors
+ cat
+ rm -rf /tmp/build.sh.BEGiT6/usr/lib/locale /tmp/build.sh.BEGiT6/usr/share/locale /tmp/build.sh.BEGiT6/usr/lib/gconv /tmp/build.sh.BEGiT6/usr/lib64/gconv /tmp/build.sh.BEGiT6/usr/bin/localedef /tmp/build.sh.BEGiT6/usr/sbin/build-locale-archive
+ rm -rf /tmp/build.sh.BEGiT6/usr/share/man /tmp/build.sh.BEGiT6/usr/share/doc /tmp/build.sh.BEGiT6/usr/share/info /tmp/build.sh.BEGiT6/usr/share/gnome/help
+ rm -rf /tmp/build.sh.BEGiT6/usr/share/cracklib
+ rm -rf /tmp/build.sh.BEGiT6/usr/share/i18n
+ rm -rf /tmp/build.sh.BEGiT6/sbin/sln
+ rm -rf /tmp/build.sh.BEGiT6/etc/ld.so.cache
+ rm -rf /tmp/build.sh.BEGiT6/var/cache/ldconfig/aux-cache
+ version=
+ '[' -r /tmp/build.sh.BEGiT6/etc/redhat-release ']'
++ sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' /tmp/build.sh.BEGiT6/etc/redhat-release
+ version=6.6
+ '[' -z 6.6 ']'
+ tar --numeric-owner -c -C /tmp/build.sh.BEGiT6 .
+ docker import - centos6:6.6
c459824791f12b110ae8c2bd83847b2cd34c5a36d9afbb69db4e2acfe2c7c79c
+ docker run -i -t centos6:6.6 echo success


success
+ rm -rf /tmp/build.sh.BEGiT6

运行完后, 可以在本地查看到刚才生成的image.
[root@150 ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6                           6.6                 c459824791f1        34 seconds ago      192.2 MB

使用这个image
[root@150 ~]# docker run -t -i --rm centos6:6.6 /bin/bash
bash-4.1# cat /etc/redhat-release   
CentOS release 6.6 (Final)
bash-4.1# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                9.9G  354M  9.0G   4% /
/dev/mapper/docker-8:33-3407874-86d271118901ababe886058c9cfca4790857f38117c4a3b9f09b8b03d1f1ebb5
                      9.9G  354M  9.0G   4% /
tmpfs                  48G     0   48G   0% /dev
shm                    64M     0   64M   0% /dev/shm
/dev/sdc1             221G  156G   54G  75% /.dockerinit
/dev/sda1              39G  4.4G   34G  12% /etc/resolv.conf
/dev/sdc1             221G  156G   54G  75% /etc/hostname
/dev/sdc1             221G  156G   54G  75% /etc/hosts
tmpfs                  48G     0   48G   0% /proc/kcore



提交到docker hub或私有docker registry
提交到docker hub

[root@150 tmp]# docker ps -a
CONTAINER ID        IMAGE                                     COMMAND             CREATED             STATUS                      PORTS               NAMES
64b683e221ae        centos6:6.6                               echo success        19 minutes ago      Exited (0) 19 minutes ago                       ecstatic_fermi      

[root@150 ~]# docker commit -a "digoal@126.com" -m "this is centos6.6 base image created on centos6.5 x64" -p 64b683e221ae digoal/centos6:6.6
6f7b94e36cd2bb846209418195b7569a048bc03a4badc51095a488ff6ddc8709

[root@150 ~]# docker push digoal/centos6:6.6
The push refers to a repository [digoal/centos6] (len: 1)
Sending image list
Pushing repository digoal/centos6 (1 tags)
Pushing tag for rev [6f7b94e36cd2] on {https://cdn-registry-1.docker.io/v1/repositories/digoal/centos6/tags/6.6}

上传到私有registry
[root@150 ~]# docker tag centos6:6.6 172.16.3.221:5000/digoal/centos6:6.6

[root@150 ~]# docker push 172.16.3.221:5000/digoal/centos6:6.6
The push refers to a repository [172.16.3.221:5000/digoal/centos6] (len: 1)
Sending image list
Pushing repository 172.16.3.221:5000/digoal/centos6 (1 tags)
c459824791f1: Image successfully pushed 
Pushing tag for rev [c459824791f1] on {http://172.16.3.221:5000/v1/repositories/digoal/centos6/tags/6.6}

[参考 ]
3. man docker-import
目录
相关文章
|
6月前
|
网络协议 Linux 虚拟化
CentOS 8 错误: Error setting up base repository
CentOS 8 错误: Error setting up base repository
178 2
centos8 Failed to download metadata for repo ‘base‘: Cannot download repomd.xml
centos8 Failed to download metadata for repo ‘base‘: Cannot download repomd.xml
465 1
|
2月前
|
存储 安全 Linux
CentOS安装SeaweedFS
通过上述步骤,您应该能够在CentOS系统上成功安装并启动SeaweedFS。记住,根据实际部署规模和需求,可能还需要进一步调整配置参数和优化网络布局。SeaweedFS的灵活性和扩展性意味着随着使用深入,您可能需要探索更多高级配置和管理策略。
107 64
|
2月前
|
存储 安全 Linux
CentOS安装SeaweedFS
通过上述步骤,您应该能够在CentOS系统上成功安装并启动SeaweedFS。记住,根据实际部署规模和需求,可能还需要进一步调整配置参数和优化网络布局。SeaweedFS的灵活性和扩展性意味着随着使用深入,您可能需要探索更多高级配置和管理策略。
116 61
|
18天前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第8天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括系统准备、配置安装源、安装 SQL Server 软件包、运行安装程序、初始化数据库以及配置远程连接。通过这些步骤,您可以顺利地在 CentOS 系统上部署和使用 SQL Server 2019。
|
19天前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第7天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括系统要求检查与准备、配置安装源、安装 SQL Server 2019、配置 SQL Server 以及数据库初始化(可选)。通过这些步骤,你可以成功安装并初步配置 SQL Server 2019,进行简单的数据库操作。
|
2月前
|
Linux 网络安全 数据安全/隐私保护
Linux系统之Centos7安装cockpit图形管理界面
【10月更文挑战第12天】Linux系统之Centos7安装cockpit图形管理界面
92 1
Linux系统之Centos7安装cockpit图形管理界面
|
26天前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,并与使用 RPM 包安装进行了对比
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,并与使用 RPM 包安装进行了对比。通过具体案例,读者可以了解如何准备环境、下载源码、编译安装、配置服务及登录 MySQL。编译源码安装虽然复杂,但提供了更高的定制性和灵活性,适用于需要高度定制的场景。
73 3
|
27天前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码安装 MySQL 数据库的详细步骤,并与使用 RPM 包安装进行了对比。
本文介绍了在 CentOS 7 中通过编译源码安装 MySQL 数据库的详细步骤,并与使用 RPM 包安装进行了对比。内容涵盖准备工作、下载源码、编译安装、配置服务、登录设置及实践心得,帮助读者根据需求选择最适合的安装方法。
49 2
|
2月前
|
NoSQL 数据可视化 Linux
redis学习四、可视化操作工具链接 centos redis,付费Redis Desktop Manager和免费Another Redis DeskTop Manager下载、安装
本文介绍了Redis的两个可视化管理工具:付费的Redis Desktop Manager和免费的Another Redis DeskTop Manager,包括它们的下载、安装和使用方法,以及在使用Another Redis DeskTop Manager连接Redis时可能遇到的问题和解决方案。
133 1
redis学习四、可视化操作工具链接 centos redis,付费Redis Desktop Manager和免费Another Redis DeskTop Manager下载、安装