docker快速入門3-docker鏡像

docker快速入門3-docker鏡像

Docker鏡像含有啓動容器所須要的文件系統及其內容,所以,其用於建立並啓動docker容器。html

docker鏡像採用分層構建機制,最底層爲bootfs,次之爲rootfs。node

  • bootfs用於系統引導的文件系統,包括bootloader和kernel,容器啓動完成後會被卸載以節約內存資源
  • rootfs由內核掛載爲只讀模式,然後經過聯合掛載技術額外掛載一個可寫

Docker鏡像層

Docer的鏡像是一層一層堆疊而成,位於下層的鏡像稱爲父鏡像,最底層的稱爲基礎鏡像。nginx

最上層爲可讀寫層,其下的均爲只讀docker

aufs和overlayfs

aufs是advanced multi-layered unification filesystem 高級多層統一文件系統。json

aufs和overlayfs都是用於Linux文件系統實現聯合掛載,除了這兩個,docker還支持btrfs,devicemapper和vfs等文件系統。ubuntu

Docer早期版本,在ubuntu下使用的是aufs,在CentOS7上使用的是devicemapper,而如今較新的Docer版本使用的是overlayfs,如今已發展到overlay2的版本。app

Docer鏡像獲取順序:當一個容器啓動時,docker daemon會試圖從本地獲取相關鏡像,本地鏡像不存在時,將從Registry中下載該鏡像並保存到本地。curl

很是著名的第三方鏡像倉庫: https://quay.io/ide

基於容器製做鏡像

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB

# 建立容器,並在容器內建立文件
root@node01:~# docker container run -i -t --name bbox-01 busybox:latest
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -pv /data/html
created directory: '/data/'
created directory: '/data/html'
/ # echo "<h1>Hello Word!</h1>" > /data/html/index.html
/ # cat /data/html/index.html
<h1>Hello Word!</h1>

另起一個終端,基於容器製做鏡像this

root@node01:~# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
787e4b3b2516        busybox:latest      "sh"                4 minutes ago       Up 4 minutes                            bbox-01
root@node01:~# docker container commit -p -m "Create /data/html/index.html" bbox-01 busybox-httpd:v0.1
sha256:806601ab556504829ff18d36ff308becab6ae351c7ae8f7b2adaecace52787bc
root@node01:~# docker image ls busybox*
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd       v0.1                806601ab5565        44 seconds ago      1.22MB
busybox             latest              c7c37e472d31        2 weeks ago         1.22MB

退出容器bbox-01並刪除,並以製做的鏡像運行一個容器

root@node01:~# docker container rm bbox-01

root@node01:~# docker container run -t -i --name bbox-http-01 busybox-httpd:v0.1
/ # ls data/html/
index.html
/ # cat data/html/index.html
<h1>Hello Word!</h1>
/ #

新鏡像中的/data/html/index.html得以保留,因此咱們在原有的鏡像基礎上製做了一個新的鏡像,新鏡像較原鏡像多了/data/html/index.html文件。

busybox鏡像中能夠運行一個httpd服務,既然建立了/data/html/index.html文件,那能夠把鏡像製做成一個提供httpd服務的鏡像。原鏡像默認啓動sh命令,只須要把啓動時運行的命令修改爲httpd相關命令便可。

root@node01:~# docker container ls
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
6d6e33d7acc4        busybox-httpd:v0.1   "sh"                5 minutes ago       Up 5 minutes                            bbox-http-01
root@node01:~# docker container commit -p -m "run httpd" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' bbox-http-01 busybox-httpd:v0.2
sha256:985f056d206d3b3c3e1b62c66bcda672c5daf902245d9cf62e27a5bdd1de5b37
root@node01:~# docker container run -d --name bbox-httpd-02 busybox-httpd:v0.2
97ca3d6118ecbbb8ad5d4ec611b7ec425b66a959b663ea8f3dd689931e8930f4
root@node01:~# docker container ls
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS               NAMES
97ca3d6118ec        busybox-httpd:v0.2   "/bin/httpd -f -h /d…"   8 seconds ago       Up 6 seconds                            bbox-httpd-02
6d6e33d7acc4        busybox-httpd:v0.1   "sh"                     11 minutes ago      Up 11 minutes                           bbox-http-01

docker container commit -p -m "run httpd" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' bbox-http-01 busybox-httpd:v0.2 ,經過-c選項能夠修改容器啓動時的運行命令。

root@node01:~# docker container inspect bbox-httpd-02 | grep -i ipaddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
root@node01:~# curl http://172.17.0.3
<h1>Hello Word!</h1>

已成功把busybox鏡像製做成了一個提供httpd服務的鏡像。

鏡像上傳下載

本地製做好的鏡像能夠上傳到docker hubaliyun的鏡像平臺或者私有的鏡像倉庫等,便於把鏡像共享給他人使用。

REPOSITORY這個名稱要與遠程倉庫名稱與之對應,以推送到docker hub爲例子,首先須要註冊一個docker hub賬號,建立相應的倉庫,以下圖

docker快速入門3-docker鏡像

REPOSITORY就應該是zhaochj/httpd,那把能提供httpdbusybox鏡像上傳到docker hub

先對相應的鏡像從新打上tag,以符合上傳的名稱要求

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        32 minutes ago      1.22MB
busybox-httpd            v0.1                806601ab5565        47 minutes ago      1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB
root@node01:~# docker image tag busybox-httpd:v0.2 zhaochj/httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        35 minutes ago      1.22MB
zhaochj/httpd            v0.1                985f056d206d        35 minutes ago      1.22MB
...

在上傳前須要先login

root@node01:~# docker login -u zhaochj
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
root@node01:~# docker image push zhaochj/httpd:v0.1
The push refers to repository [docker.io/zhaochj/httpd]
aba5952cfa04: Pushed
8b84b15db17e: Pushed
50761fe126b6: Mounted from library/busybox
v0.1: digest: sha256:63ecaa4990fb60f8aab701d690e5ef12cb664491753fee0208d080ca9ee8c28c size: 941

推送完成後在docker hub上就能夠查看到

docker快速入門3-docker鏡像

若是是大陸使用,建議使用aliyun的開發平臺中的鏡像服務。

鏡像的導入導出

本地鏡像能夠導出成一個壓縮包,把此壓縮包拷貝到其餘主機後能夠導入。

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
busybox-httpd            v0.1                806601ab5565        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB

# 將多個鏡像打包導出
root@node01:~# docker image save -o myimages.gz busybox:latest busybox-httpd:v0.1
root@node01:~# ls
myimages.gz

root@node01:~# docker image rm busybox:latest busybox-httpd:v0.1
Untagged: busybox:latest
Untagged: busybox@sha256:9ddee63a712cea977267342e8750ecbc60d3aab25f04ceacfa795e6fce341793
Untagged: busybox-httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB
# 導入已打包的鏡像
root@node01:~# docker image load -i myimages.gz
Loaded image: busybox:latest
Loaded image: busybox-httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
busybox-httpd            v0.1                806601ab5565        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB
相關文章
相關標籤/搜索