Docker使用第一步–制作基础镜像 三 (准备篇)
概要
使用Docker 首先要了解容器和镜像的关系:
运行容器需要镜像,就像运行电脑需要操作系统一样,
镜像可以在容器中制作,就像ghost一样,
但是我们一开始并没有镜像,所以需要一个基础镜像,就像系统盘一样。
在使用Docker容器时,我们经常需要基于一些基础镜像(Base Image)来构建符合自己要求的镜像。通常,我们会使用DockerHub或其他镜像市场提供的基础镜像。虽然这样很方便,但是我们并不知道第三方提供的基础镜像的实现细节和安全性,若在生产环境贸然使用这些基础镜像,则可能会带来技术隐患。因此,有必要创建自己的基础镜像!
这里是在Centos7 上面制作Centos7的基础镜像
Centos6 上可以使用 febootstrap 制作Docker镜像 不过Centos7上面已经改为了supermin
supermin安装
1 |
yum install supermin* |
安装后运行命令man supermin可以查看关于这个工具的基本信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
NAME supermin - Tool for creating supermin appliances SYNOPSIS supermin [-o OUTPUTDIR] --names LIST OF PKGS ... supermin [-o OUTPUTDIR] PKG FILE NAMES ... DESCRIPTION Supermin is a tool for building supermin appliances. These are tiny appliances (similar to virtual machines), usually around 100KB in size, which get fully instantiated on-the-fly in a fraction of a second when you need to boot one of them. Originally "fe" in "febootstrap" stood for "Fedora", but this tool is now distro-independent and can build supermin appliances for several popular Linux distros, and adding support for others is reasonably easy. For this reason, starting with version 4, we have renamed the tool "supermin". Note that this manual page documents supermin 4.x which is a complete rewrite and quite different from febootstrap 2.x. If you are looking for the febootstrap 2.x tools, then this is not the right place. |
运行命令supermin5 –help可以查看基本的使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
[root@localhost ~]# supermin5 --help supermin - tool for creating supermin appliances Copyright (C) 2009-2014 Red Hat Inc. Usage: supermin --prepare LIST OF PACKAGES ... supermin --build INPUT [INPUT ...] For full instructions, read the supermin(1) man page. Options: --build Build a full appliance --copy-kernel Copy kernel instead of symlinking --dtb Obsolete option, do not use -f chroot|ext2 Set output format --format -"- --host-cpu ARCH Set host CPU architecture --if-newer Only build if needed --include-packagelist Add a file with the list of packages --list-drivers Display list of drivers and exit --lock LOCKFILE Use a lock file --names Give an error for people needing supermin 4 -o OUTPUTDIR Set output directory --packager-config CONFIGFILE Set packager config file --prepare Prepare a supermin appliance --size Set the size of the ext2 filesystem --use-installed Use installed files instead of accessing network -v Enable debugging messages --verbose -"- -V Display version and exit --version -"- -help Display this list of options --help Display this list of options [root@localhost ~]# |
制作镜像
制作镜像需要使用root用户制作,其他用户制作的镜像可能会出现一些预料不到的问题
创建一个临时目录(这个不重要)
1 2 3 4 5 |
[root@localhost ~]# cd /home/soft [root@localhost soft]# uname -a Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux [root@localhost soft]# mkdir centos7 [root@localhost soft]# cd centos7 |
顺便查看一下系统版本
步骤-1
1 |
supermin5 -v --prepare bash coreutils -o supermin.d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
[root@localhost centos7]# supermin5 -v --prepare bash coreutils -o supermin.d supermin: version: 5.1.19 supermin: rpm: detected RPM version 4.11 supermin: package handler: fedora/rpm supermin: prepare: bash coreutils BDB2053 Freeing read locks for locker 0xd: 3655/139784403130432 BDB2053 Freeing read locks for locker 0xf: 3655/139784403130432 BDB2053 Freeing read locks for locker 0x10: 3655/139784403130432 BDB2053 Freeing read locks for locker 0x11: 3655/139784403130432 supermin: packages specified on the command line: - bash-4.2.46-20.el7_2.x86_64 - coreutils-8.22-18.el7.x86_64 supermin: writing supermin.d.cxpuwzob/packages supermin: after resolving dependencies there are 39 packages: - basesystem-10.0-7.el7.centos.noarch - bash-4.2.46-20.el7_2.x86_64 - ca-certificates-2015.2.6-73.el7.noarch - centos-release-7-3.1611.el7.centos.x86_64 - chkconfig-1.7.2-1.el7.x86_64 - coreutils-8.22-18.el7.x86_64 - filesystem-3.2-21.el7.x86_64 - gawk-4.0.2-4.el7.x86_64 - glibc-2.17-196.el7_4.2.x86_64 - glibc-common-2.17-196.el7_4.2.x86_64 - gmp-1:6.0.0-12.el7_1.x86_64 - grep-2.20-2.el7.x86_64 - info-5.1-4.el7.x86_64 - keyutils-libs-1.5.8-3.el7.x86_64 - krb5-libs-1.15.1-8.el7.x86_64 - libacl-2.2.51-12.el7.x86_64 - libattr-2.4.46-12.el7.x86_64 - libcap-2.22-8.el7.x86_64 - libcom_err-1.42.9-10.el7.x86_64 - libffi-3.0.13-18.el7.x86_64 - libgcc-4.8.5-16.el7_4.1.x86_64 - libselinux-2.5-11.el7.x86_64 - libsepol-2.5-6.el7.x86_64 - libstdc++-4.8.5-11.el7.x86_64 - libtasn1-3.8-3.el7.x86_64 - libverto-0.2.5-4.el7.x86_64 - ncurses-5.9-13.20130511.el7.x86_64 - ncurses-base-5.9-13.20130511.el7.noarch - ncurses-libs-5.9-13.20130511.el7.x86_64 - nss-softokn-freebl-3.16.2.3-14.4.el7.x86_64 - openssl-libs-1:1.0.2k-8.el7.x86_64 - p11-kit-0.20.7-3.el7.x86_64 - p11-kit-trust-0.20.7-3.el7.x86_64 - pcre-8.32-17.el7.x86_64 - popt-1.13-16.el7.x86_64 - sed-4.2.2-5.el7.x86_64 - setup-2.8.71-7.el7.noarch - tzdata-2016g-2.el7.noarch - zlib-1.2.7-17.el7.x86_64 Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.neusoft.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.aliyun.com (1/5): centos-release-7-5.1804.el7.centos.x86_64.rpm | 24 kB 00:00:01 (2/5): info-5.1-5.el7.x86_64.rpm | 233 kB 00:00:03 (3/5): openssl-libs-1.0.2k-12.el7.x86_64.rpm | 1.2 MB 00:00:04 (4/5): bash-4.2.46-30.el7.x86_64.rpm | 1.0 MB 00:00:04 (5/5): glibc-2.17-222.el7.x86_64.rpm | 3.6 MB 00:00:05 supermin: there are 57 config files supermin: writing supermin.d.cxpuwzob/base.tar.gz ./etc/issue ./etc/issue.net ./etc/os-release ./etc/system-release-cpe ./etc/yum.repos.d/CentOS-Base.repo ./etc/yum.repos.d/CentOS-CR.repo ./etc/yum.repos.d/CentOS-Debuginfo.repo ./etc/yum.repos.d/CentOS-Media.repo ./etc/yum.repos.d/CentOS-Sources.repo ./etc/yum.repos.d/CentOS-Vault.repo ./etc/yum.repos.d/CentOS-fasttrack.repo ./etc/yum/vars/infra ./etc/aliases ./etc/bashrc ./etc/csh.cshrc ./etc/csh.login ./etc/environment ./etc/exports ./etc/filesystems ./etc/group ./etc/gshadow ./etc/host.conf ./etc/hosts ./etc/hosts.allow ./etc/hosts.deny ./etc/inputrc ./etc/motd ./etc/passwd ./etc/printcap ./etc/profile ./etc/protocols ./etc/securetty ./etc/services ./etc/shadow ./etc/shells ./etc/subgid ./etc/subuid ./etc/krb5.conf ./etc/pki/ca-trust/ca-legacy.conf ./etc/default/nss ./etc/pki/tls/openssl.cnf ./etc/GREP_COLORS ./etc/profile.d/colorgrep.csh ./etc/profile.d/colorgrep.sh ./usr/share/info/dir ./etc/ld.so.conf ./etc/nsswitch.conf ./etc/rpc ./usr/lib64/gconv/gconv-modules ./etc/DIR_COLORS ./etc/DIR_COLORS.256color ./etc/DIR_COLORS.lightbgcolor ./etc/profile.d/colorls.csh ./etc/profile.d/colorls.sh ./etc/skel/.bash_logout ./etc/skel/.bash_profile ./etc/skel/.bashrc supermin: renaming supermin.d.cxpuwzob to supermin.d |
步骤-2
1 |
supermin5 -v --build --format chroot supermin.d -o appliance.d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@localhost centos7]# supermin5 -v --build --format chroot supermin.d -o appliance.d supermin: version: 5.1.19 supermin: rpm: detected RPM version 4.11 supermin: package handler: fedora/rpm supermin: build: supermin.d supermin: reading the supermin appliance supermin: build: visiting supermin.d/base.tar.gz type gzip base image (tar) supermin: build: visiting supermin.d/packages type uncompressed packages supermin: mapping package names to installed packages BDB2053 Freeing read locks for locker 0x12: 9952/139750258079808 BDB2053 Freeing read locks for locker 0x14: 9952/139750258079808 BDB2053 Freeing read locks for locker 0x15: 9952/139750258079808 BDB2053 Freeing read locks for locker 0x16: 9952/139750258079808 supermin: resolving full list of package dependencies supermin: build: 39 packages, including dependencies supermin: build: 18602 files supermin: build: 18602 files, after matching excludefiles supermin: build: 18602 files, after adding hostfiles supermin: build: 5445 files, after removing unreadable files supermin: build: 5475 files, after munging supermin: renaming appliance.d.f9ye8xxw to appliance.d |
步骤-3
1 2 3 |
[root@localhost centos7]# echo 7 > appliance.d/etc/yum/vars/releasever [root@localhost centos7]# tar --numeric-owner -cpf centos-7.tar -C appliance.d . [root@localhost centos7]# |
注意最后面的 .
可以看到已经创建好的文件:
步骤-4 导入tar文件到docker
1 2 3 |
[root@localhost centos7]# cat centos-7.tar | docker import - poxiao/centos:v7 sha256:1fcd0e0567a88beaea179606c5d88c020bf9c59cbdc1da88643cdde937d9b1b0 [root@localhost centos7]# |
查看镜像:
1 2 3 4 |
[root@localhost centos7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE poxiao/centos v7 1fcd0e0567a8 47 seconds ago 79.9MB [root@localhost centos7]# |
运行镜像
1 2 3 4 |
[root@localhost centos7]# docker run -t -i poxiao/centos:v7 /bin/bash bash-4.2# uname -a Linux fdfbd9cf16bd 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux bash-4.2# |
参考资料:
https://blog.csdn.net/hjh00/article/details/72859287
http://sheng.iteye.com/blog/2317185
http://baijiahao.baidu.com/s?id=1581420975184566963