资讯专栏INFORMATION COLUMN

走进docker(03):如何绕过docker运行hello-world?

robin / 3223人阅读

摘要:相关工具本文将用到三个工具,分别是和。根据生成的的就是运行容器时需要的东西的集合。使用运行该有了后,就可以用来运行该容器了这里直接用的代替命令,如果你自己编译了的,那么用命令也是一样的。

上一篇介绍了image的格式,这里我们就来用一下hello-world这个image,看怎么输出和docker run hello-world同样的内容。

相关工具

本文将用到三个工具,分别是skopeo、oci-image-tools和runc。

skopeo: 用来从Docker Hub上拉取image,并保存为OCI格式

oci-image-tools: 包含几个用来操作本地image的工具

runc: 运行容器

runc可以用docker自带的docker-runc命令替代,效果是一样的,skopeo的安装可以参考上一篇最后的介绍或者github上的主页,oci-image-tools的安装请参考github上的主页。

获取hello-world的image

利用skopeo获得hello-world的oci格式的image

dev@debian:~/images$ skopeo copy docker://hello-world oci:hello-world
dev@debian:~/images$ tree hello-world/
hello-world/
├── blobs
│   └── sha256
│       ├── 0a2ad94772e366c2b7f2266ca46daa0c38efe08811cf1c1dee6558fcd7f2b54e
│       ├── 78445dd45222097f5f8d5a16e48dc19c4ca162dcdb80010ab6f1ccfc7e2c0fa3
│       └── 998a60597add14861de504277c0d850e9181b1768011f51c7daaf694dfe975ef
├── oci-layout
└── refs
    └── latest

然后我们看看hello-world这个image的文件系统都有些什么文件

#利用oci-image-tool unpack,将image解压到hello-world-filesystem目录
dev@debian:~/images$ mkdir hello-world-filesystem
dev@debian:~/images$ oci-image-tool unpack --ref latest hello-world hello-world-filesystem
dev@debian:~/images$ tree hello-world-filesystem/
hello-world-filesystem/
└── hello

0 directories, 1 file
dev@debian:~/images$ file hello-world-filesystem/hello
hello-world-filesystem/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=4999eecfa472a2341b53954c0eca1e893f01305c, stripped

从上面的结果可以看出,hello-world这个image就只包含一个名字叫做hello的静态链接的可执行文件。

根据image生成runtime的bundle

runtime的bundle就是运行容器时需要的东西的集合。

dev@debian:~/images$ mkdir hello-world-bundle
dev@debian:~/images$ oci-image-tool create --ref latest hello-world hello-world-bundle
dev@debian:~/images$ tree hello-world-bundle
hello-world-bundle
├── config.json
└── rootfs
    └── hello

1 directory, 2 files

从这里生成的bundle可以看出,bundle里面就是一个配置文件加上rootfs,rootfs里面的东西就是image里面的文件系统部分,config.json是对容器的描述,比如rootfs的路径,容器启动后要运行什么命令等,后续介绍runtime标准的时候再详细介绍。

使用runc运行该bundle

有了bundle后,就可以用runc来运行该容器了

这里直接用docker的docker-runc代替runc命令,如果你自己编译了opencontainer的runc,那么用runc命令也是一样的。

dev@debian:~/images$ cd hello-world-bundle/
#oci-image-tool帮我们生成的config文件版本和runc需要的版本不一致,
#所以这里先将它删掉,然后用runc spec命令生成一个默认的config文件
dev@debian:~/images/hello-world-bundle$ rm config.json
dev@debian:~/images/hello-world-bundle$ docker-runc spec

#默认生成的config里面指定容器启动的进程为sh,
#我们需要将它换成我们的hello程序
#这里请用自己熟悉的编辑器修改config.json文件,
#将里面的"args": ["sh"]改成"args": ["/hello"]
dev@debian:~/images/hello-world-bundle$ vim config.json

#然后用runc运行该容器,这里命令行里的hello是给容器取的名字,
#可以是任意不和其它容器冲突的字符串
dev@debian:~/images/hello-world-bundle$ sudo docker-runc run hello
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.
 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

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
结束语

该篇展示了如何不通过docker而运行hello-world容器,主要目的为了了解镜像以及runc之间的关系,同时也触发我们思考一个问题,既然我们可以绕过docker运行容器,那我们为什么还要用docker呢?

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/26898.html

相关文章

  • 走进docker系列:开篇

    摘要:包含的内容本系列主要介绍三个上的项目由于只介绍核心的东西,所以不会包含下面这些项目使用语言开发,将多个相关的容器配置在一起,从而可以同时创建启动停止和监控它们。由于本人时间安排发生变化,本系列停止更新,后面不确定是否会继续,非常抱歉。 本人docker初学者,边学习边总结,一方面加深自己的理解,另一方面希望对其他想深入了解docker的同学有所帮助。 由于本人缺乏实战经验,错误在所难免...

    darkbug 评论0 收藏0
  • 走进docker(01):hello-world的背后发生了什么?

    摘要:进程启动后,就会按照的标准准备好相关运行时环境,然后启动进程。涉及到标准输入输出重定向,这里不细说这里是它们之间的交互流程流程对应的文字描述如下客户端发送创建容器请求给,收到请求后,发现本地没有相应的额,于是返回失败。 在程序员的世界里,hello world是个很特殊的存在,当我们接触一门新的语言、新的开发库或者框架时,第一时间想了解的一般都是怎么实现一个hello world,然后...

    cikenerd 评论0 收藏0
  • 走进docker(02):image(镜像)是什么?

    摘要:包含的内容一个由可选和四部分组成。对于这两种不同类型的文件格式,标准定义了两个新的,分别是和。最新的标准里面并没有涉及到,不过估计后续会加上。 上一篇介绍了hello-world的大概流程,那么hello-world的image里面到底包含了些什么呢?里面的格式是怎么样的呢? image所包含的内容以及格式都是有标准的,由Open Containers Initiative(OCI)负...

    xiaowugui666 评论0 收藏0
  • 走进docker(07):docker start命令背后发生了什么?

    摘要:首先来看看收到客户端的启动容器请求后,做了些什么。待上面的文件都准备好了之后,通过的方式给发送请求,通知启动容器。主要功能是启动并管理运行时的所有。包含容器中进程相关的一些属性信息,后续在这个容器上执行命令时会用到这个文件。 在上一篇介绍过了docker create之后,这篇来看看docker start是怎么根据create之后的结果运行容器的。 启动容器 在这里我们先启动上一篇中...

    Thanatos 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<