上一篇介紹了image的格式,這裏咱們就來用一下hello-world這個image,看怎麼輸出和docker run hello-world
一樣的內容。git
本文將用到三個工具,分別是skopeo、oci-image-tools和runc。github
skopeo: 用來從Docker Hub上拉取image,並保存爲OCI格式docker
oci-image-tools: 包含幾個用來操做本地image的工具json
runc: 運行容器ubuntu
runc能夠用docker自帶的docker-runc命令替代,效果是同樣的,skopeo的安裝能夠參考上一篇最後的介紹或者github上的主頁,oci-image-tools的安裝請參考github上的主頁。vim
利用skopeo得到hello-world的oci格式的imagesegmentfault
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的文件系統都有些什麼文件bash
#利用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的靜態連接的可執行文件。app
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標準的時候再詳細介紹。
有了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呢?