设为首页 收藏本站
查看: 1224|回复: 0

[经验分享] 最小 Docker 镜像 hello-world 剖析

[复制链接]

尚未签到

发表于 2015-10-13 12:01:39 | 显示全部楼层 |阅读模式
  开始学习 Docker 的同学基本上都是按照官方的 guide 来安装,之后要测试是否已经安装成功,官方会让你 pull 一个 hello-world 示例镜像下来并运行,如下命令:


guohl@ghl-MBP ⮀ ~ ⮀ docker pull hello-world
31cbccb51277: Pull complete
e45a5af57b00: Pull complete
511136ea3c5a: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for hello-world:latest
guohl@ghl-MBP ⮀ ~ ⮀ docker run hello-world
Hello from Docker.
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(Assuming it was not already locally available.)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
For more examples and ideas, visit:
http://docs.docker.com/userguide/
  出现上面的输出信息表示你 Docker 安装成功。使用下面命令查看该镜像的信息,发现大小只有 910B:


guohl@ghl-MBP ⮀ ~ ⮀ docker images hello-world
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              e45a5af57b00        3 months ago        910 B
  那么该镜像运行怎么就输出上面这一串信息的呢?如果对 Docker 了解一点的话自然会去找该镜像的 Dockerfile,Dockerfile 详细描述了该镜像是如何建立的以及运行时执行的命令。从 Docker Hub 上 hello-world 的库简介 找到其 Dockerfile 在 github 上,简简单单的三条语句:


FROM scratch
ADD hello /
CMD ["/hello"]

  • 第一条FROM指令是 Docker 用来指定该镜像是基于哪个基础镜像构建的,这里指定为 scratch,实际上 scratch 镜像是一个空镜像,用来构建基础镜像或者极小的镜像。
  • 第二条ADD指令表示从 Dockerfile 所在目录拷贝文件到指定路径下,这里拷贝 hello 文件到根目录下,至于 hello 文件是什么稍后介绍。
  • 第三条CMD指令用来指示当运行 docker run 命令运行该镜像时要执行的命令,这里执行 /hello,就是执行第二步拷贝到根目录下的 hello 文件。
  hello 是一个可执行的二进制文件,从文章的开头可以推测该文件执行输出那一堆提示信息,那该文件怎样生成的呢?从 hello-world 的库中可以看到还有两个文件,一个是汇编文件 hello.asm,还有一个 Makefile 文件,可以猜测 hello 就是事先通过 hello.asm 编译得到的,如 Makefile 文件所描述的编译过程:


hello: hello.asm
nasm -o $@ $<
chmod +x hello
.PHONY: clean
clean:
-rm -vf hello
  该 Makefile 文件比较容易理解,主要是使用 nasm 汇编器将 hello.asm 编译生成可执行文件 hello。
  最后再来看看 hello.asm 文件,这是一段 Intel x86 汇编:


; this is especially thanks to:
; http://blog.markloiseau.com/2012/05/tiny-64-bit-elf-executables/
BITS 64
org 0x00400000  ; Program load offset
; 64-bit ELF header
ehdr:
;  1), 0 (ABI ver.)
db 0x7F, "ELF", 2, 1, 1, 0       ; e_ident
times 8 db 0                     ; reserved (zeroes)
dw 2              ; e_type: Executable file
dw 0x3e           ; e_machine:  AMD64
dd 1              ; e_version:  current version
dq _start         ; e_entry:    program entry address (0x78)
dq phdr - $$      ; e_phoff   program header offset (0x40)
dq 0              ; e_shoff no section headers
dd 0              ; e_flags no flags
dw ehdrsize       ; e_ehsize:   ELF header size (0x40)
dw phdrsize       ; e_phentsize:    program header size (0x38)
dw 1              ; e_phnum:    one program header
dw 0              ; e_shentsize
dw 0              ; e_shnum
dw 0              ; e_shstrndx
ehdrsize equ $ - ehdr
; 64-bit ELF program header
phdr:
dd 1              ; p_type: loadable segment
dd 5              ; p_flags read and execute
dq 0              ; p_offset
dq $$             ; p_vaddr:start of the current section
dq $$             ; p_paddr: "       "
dq filesize       ; p_filesz
dq filesize       ; p_memsz
dq 0x200000       ; p_align:    2^11=200000 = section alignment
; program header size
phdrsize equ $ - phdr
; Hello World!/your program here
_start:
; sys_write(stdout, message, length)
mov rax, 1           ; sys_write
mov rdi, 1           ; stdout
mov rsi, message     ; message address
mov rdx, length      ; message string length
syscall
; sys_exit(return_code)
mov rax, 60          ; sys_exit
mov rdi, 0           ; return 0 (success)
syscall
message:
db 'Hello from Docker.', 0x0A
db 'This message shows that your installation appears to be working correctly.', 0x0A
db 0x0A
db 'To generate this message, Docker took the following steps:', 0x0A
db ' 1. The Docker client contacted the Docker daemon.', 0x0A
db ' 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.', 0x0A
db '    (Assuming it was not already locally available.)', 0x0A
db ' 3. The Docker daemon created a new container from that image which runs the', 0x0A
db '    executable that produces the output you are currently reading.', 0x0A
db ' 4. The Docker daemon streamed that output to the Docker client, which sent it', 0x0A
db '    to your terminal.', 0x0A
db 0x0A
db 'To try something more ambitious, you can run an Ubuntu container with:', 0x0A
db ' $ docker run -it ubuntu bash', 0x0A
db 0x0A
db 'For more examples and ideas, visit:', 0x0A
db ' http://docs.docker.com/userguide/', 0x0A
length: equ $-message            ; message length calculation
; File size calculation
filesize equ $ - $$
  也比较简单,就调用了两个系统调用,sys_write 向标准输出打印一段信息,sys_exit 退出程序。
  至此,我们分析完了官方提供的 hello-world 镜像整个构建及运行过程,麻雀虽小,五脏俱全,理解最小的 Docker 镜像的工作机制对我们理解 Docker 有很大帮助。

版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-126254-1-1.html 上篇帖子: Docker创建支持ssh服务的容器和镜像 下篇帖子: Actor Platform 平台搭建(一) -平台介绍-Docker搭建方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表