Docker容器技术原理解析(自定义创建镜像)

Odele ·
更新时间:2024-09-21
· 863 次阅读

scratch镜像 scratch镜像是一个虚拟镜像,因为它根本不存在,但它确实所有基础倾向发原始模板镜像. 我们可以从官网上去查看 Base Image 的Dockerfile 是怎么写的 点击 docker官网 搜索你想用的linux版本(这里以centos来说) 选择查询的版本号7.4.1708 网页如下图
在这里插入图片描述 yum源用的是阿里云的 [root@es2 ~]# yum -y install docker [root@es2 ~]# systemctl start docker [root@es2 ~]# docker version

Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
Go version: go1.10.3
Git commit: 4ef4b30/1.13.1
Built: Tue Jan 21 17:16:25 2020
OS/Arch: linux/amd64

Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-108.git4ef4b30.el7.centos.x86_64
Go version: go1.10.3
Git commit: 4ef4b30/1.13.1
Built: Tue Jan 21 17:16:25 2020
OS/Arch: linux/amd64
Experimental: false

[root@es2 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

查看镜像 scratch [root@es2 ~]# docker search scratch

INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/scratch an explicitly empty image, especially for … 621 [OK]
docker.io docker.io/scratchy/nativescript-cli This repo provides nativescript CLI includ… 6 [OK]
docker.io docker.io/astefanutti/scratch-node Scratch Node.js Docker Images 4
docker.io docker.io/grammarly/scratch Empty image used for data volumes and othe… 2
docker.io docker.io/darkmagus/scratch3 Dockerized version of Scratch 3 editor fro… 1
docker.io docker.io/kihamo/scratch-ca-certs Scratch image with certs 1 [OK]
docker.io docker.io/microbox/scratch 1
docker.io docker.io/aosqe/scratch 0
docker.io docker.io/area51/scratch-base A minimal docker image containing the esse… 0
docker.io docker.io/connecthq/scratch-ssl A scratch image that has only one file in … 0
docker.io docker.io/crashvb/scratch This docker image is an abstraction of tia… 0 [OK]
docker.io docker.io/daixijun1990/scratch scratch base image 0 [OK]
docker.io docker.io/dtschan42/scratch2-singularity Scratch 2 cont… 0 [OK]
docker.io docker.io/ghmlee/scratch 0 [OK]
docker.io docker.io/gridx/scratch 0
docker.io docker.io/kelog/scratch scratch 0
docker.io docker.io/manifoldco/scratch-certificates A Docker scratch image with ca-certificate… 0
docker.io docker.io/mcloone/scratch-with-ca 0
docker.io docker.io/mikenavarro/scratch-go 0
docker.io docker.io/omniprojects/scratch 0
docker.io docker.io/onedr0p/scratch 0
docker.io docker.io/pvtmert/scratch from scratch 0 [OK]
docker.io docker.io/qubell/scratch 0 [OK]
docker.io docker.io/resin/scratch A materialised FROM scratch image 0
docker.io docker.io/sublimino/scratch 0

下载镜像 scratch 会报错 [root@es2 ~]# docker pull scratch

Using default tag: latest
Error response from daemon: ‘scratch’ is a reserved name

制作 scratch 镜像 由于基础镜像是基于 scratch 创建的,添加了一个文件 centos-7-docker.tar.xz 想要实现基础镜像就需要scratch 和centos-7-docker.tar.xz scratch 是一个空镜像,可以采用如下方法获得(不能下载) 方法1: 使用 /dev/null 创建一个空文件,命令如下 创建tar 文件 ]# tar cf scratch.tar --files-from /dev/null ]# ls

scratch.tar
使用 docker import 导入这个空文件到镜像,命令如下

]# docker import scratch.tar scratch 方法2: 一条命令搞定 (" - " 为管道后的标准输出 ) ]# tar -cv --files-from /dev/null | docker import - scratch

sha256:19fade5b1bd2a08546a120091194ac28425e02c2a494a7533bc7b1264b8c5180

查看镜像文件(大小为0B,镜像为虚拟镜像) [root@es1 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
scratch latest 19fade5b1bd2 5 seconds ago 0 B

制作 centos-7-docker.tar.xz centos-7-docker 其实就是系统的根目录下的所有文件 由于系统在未安装之前,我们没有任何命令可以使用,所有只能使用最基本的添加文件方式来创建这个源. yum 软件安装到指定根目录 - insallroot ,最基本的,能登录操作的系统需要的软件包有:
命令 作用 安装包
yum 安装软件使用 yum
bash 用户登录操作环境 bash
ls/pwd 常用基本系统命令 coreutils
1.创建文件夹,安装软件 [root@es2 ~]# mkdir vroot [root@es2 ~]# yum -y install --installroot=/root/vroot bash yum coreutils [root@es2 ~]# cd vroot/ [root@es2 vroot]# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr

或者

[root@es2 ~]# chroot /root/vroot bash-4.2# bash-4.2# ls -a /etc/skel/ //查看解释器 . .. .bash_logout .bash_profile .bashrc bash-4.2# cp -a /etc/skel/.[!.]* /root/ bash-4.2# ls -la /root

总用量 12
dr-xr-x— 2 root root 62 2月 17 19:24 .
dr-xr-xr-x 17 root root 224 2月 17 18:41 …
-rw-r–r-- 1 root root 18 4月 11 2018 .bash_logout
-rw-r–r-- 1 root root 193 4月 11 2018 .bash_profile
-rw-r–r-- 1 root root 231 4月 11 2018 .bashrc

bash-4.2# exit [root@es2 ~]# chroot /root/vroot [root@es2 /]# 配置 YUM 源 [root@es2 /]# cd /etc/yum.repos.d/ [root@es2 yum.repos.d]# ls

CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo
CentOS-Debuginfo.repo CentOS-Sources.repo

[root@es2 yum.repos.d]# rm -rf * [root@es2 yum.repos.d]# exit exit [root@es2 ~]# ls /etc/yum.repos.d/ elasticsearch.repo local.repo [root@es2 ~]# cp /etc/yum.repos.d/local.repo /root/vroot/etc/yum.repos.d/ 打包(要以虚拟目录作为根打包) [root@es2 ~]# cd /root [root@es2 ~]# ls scratch.tar vroot [root@es2 ~]# tar -acf centos-7-docker.tar.xz -C vroot ./ [root@es2 ~]# ls centos-7-docker.tar.xz scratch.tar vroot [root@es2 ~]# tar -atvf centos-7-docker.tar.xz | head

dr-xr-xr-x root/root 0 2020-02-17 18:41 ./
drwxr-xr-x root/root 0 2020-02-17 18:41 ./var/
drwxr-xr-x root/root 0 2020-02-17 18:41 ./var/lib/
drwxr-xr-x root/root 0 2020-02-17 18:43 ./var/lib/rpm/
-rw-r–r-- root/root 0 2020-02-17 18:39 ./var/lib/rpm/.dbenv.lock
-rw-r–r-- root/root 5226496 2020-02-17 18:42 ./var/lib/rpm/Packages
-rw-r–r-- root/root 8192 2020-02-17 18:42 ./var/lib/rpm/Name
-rw-r–r-- root/root 512000 2020-02-17 18:42 ./var/lib/rpm/Basenames
-rw-r–r-- root/root 8192 2020-02-17 18:42 ./var/lib/rpm/Group
-rw-r–r-- root/root 49152 2020-02-17 18:42 ./var/lib/rpm/Requirename

[root@es2 ~]# mkdir aabb [root@es2 ~]# mv centos-7-docker.tar.xz aabb/ [root@es2 ~]# ls aabb scratch.tar vroot [root@es2 ~]# cd aabb/ [root@es2 aabb]# ls

centos-7-docker.tar.xz

官网上去找初始Docker文件 [root@es2 aabb]# vim Dockerfile FROM scratch ADD centos-7-docker.tar.xz / LABEL name="CentOS Base Image" \ vendor="CentOS" \ license="GPLv2" \ build-date="20170911" CMD ["/bin/bash"] 创建自定义镜像 [root@es2 aabb]# docker build -t centos:latest . Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? [root@es2 aabb]# systemctl start docker [root@es2 aabb]# docker build -t centos:latest .

Sending build context to Docker daemon 51.91 MB
Step 1/4 : FROM scratch
—>
Step 2/4 : ADD centos-7-docker.tar.xz /
—> Using cache
—> d60c3b58ba23
Step 3/4 : LABEL name “CentOS Base Image” vendor “CentOS” license “GPLv2” build-date “20170911”
—> Using cache
—> 013e6a237897
Step 4/4 : CMD /bin/bash
—> Using cache
—> 180a71e4838f
Successfully built 180a71e4838f

查看镜像 [root@es2 aabb]# docker history centos

IMAGE CREATED CREATED BY SIZE COMMENT
180a71e4838f 48 minutes ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
013e6a237897 48 minutes ago /bin/sh -c #(nop) LABEL name=CentOS Base … 0 B
d60c3b58ba23 48 minutes ago /bin/sh -c #(nop) ADD file:3383b6d576d74ca… 306 MB

运行自定义制作的centos镜像 [root@es2 aabb]# docker run -it centos [root@152eca359652 /]# [root@152eca359652 /]# ifconfig bash: ifconfig: command not found [root@152eca359652 /]# yum repolist Loaded plugins: fastestmirror Determining fastest mirrors local_repo | 3.6 kB 00:00 (1/2): local_repo/group_gz | 166 kB 00:00 (2/2): local_repo/primary_db | 5.9 MB 00:00 repo id repo name status local_repo CentOS-7 - Base 9911 repolist: 9911 [root@152eca359652 /]# yum -y install net-tools [root@152eca359652 /]# yum -y install psmisc [root@152eca359652 /]# pstree -p bash(1)---pstree(148) [root@152eca359652 /]# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr 保存镜像文件 [root@152eca359652 /]# exit exit [root@es2 aabb]# ls centos-7-docker.tar.xz Dockerfile [root@es2 aabb]# docker save centos -o centos.tar [root@es2 aabb]# docker images
作者:A?J'aime?



自定义 技术原理 镜像 Docker

需要 登录 后方可回复, 如果你还没有账号请 注册新账号