Docker容器技術-鏡像分發

1、鏡像分發

1.鏡像及鏡像庫的命名方式
指定鏡像名稱和標籤的方法:node

  • 在狗偶見鏡像時
  • 經過docker tag命令
[root@bogon ~]# cd identidock/
[root@bogon identidock]# docker build -t "identidock:0.1" .
[root@bogon identidock]# docker tag "identidock:0.1" "hdlptz/identidock:0.1"
[root@bogon identidock]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED                  SIZE
hdlptz/identidock             0.1                 95f825228fdb        28 seconds ago           703MB
identidock                    0.1                 95f825228fdb        28 seconds ago           703MB

注意:
當沒有指定標籤時,Docker會用latest做爲默認值,但除此以外,它不具有任何特殊含義。不少倉庫會把它做爲最新穩定版鏡像的別名,但這只是一種慣例,並不是規定。你仍須要執行docker pull命令來獲取最新版。
當執行docker run或docker pull時,若是指定鏡像名稱不帶標籤,那麼Docker會使用帶latest標籤的鏡像,若不存在則報錯。nginx

標籤名稱規則:git

  • 必須由大小寫字母、數字、以及.和-符號組成。
  • 長度必須爲1~128個字符
  • 第一個字符不能是.或-符號

2.Docker Hubgithub

[root@bogon identidock]# docker tag "identidock:latest" "hdlptz/identidock:0.1"
[root@bogon identidock]# docker push hdlptz/identidock:0.1

別名形式: / docker

3.自動構建
每當推送任何代碼修改,Docker Hub就會構建新鏡像,並將其保存到倉庫。緩存

自動構建是經過Hub的網頁界面配置的,而不是經過命令行。安全

  • 點擊右上角「Create」下拉菜單,選擇「Create A托馬特定Build」,找到你要的代碼倉庫
  • 選擇倉庫後,跳轉值配置頁面,倉庫名稱默認爲代碼庫(能夠修改)
  • 填寫倉庫說明
  • 第一個Tag字段爲Branch,名稱爲master(跟蹤主分支)
  • 最後點擊「Create」跳轉至新倉庫構建頁面,點擊「Triger a Build」生成自動構建鏡像
添加README.md
[root@bogon identidock]# cat README.md 
identidock
==========
Simple identicon server based on monsterid from Kevin Gaudin.

From "Using Docker" bu Adrian Mouat published by O'Reilly Media.

推送到Github
[root@bogon identidock]# git add README.md
[root@bogon identidock]# git commit -m "Added README"
[root@bogon identidock]# git push

2、私有分發

1.運行本身的寄存服務bash

本地運行寄存服務(使用官方鏡像)
[root@bogon identidock]# docker run -d -p 5000:5000 registry:2

給鏡像加標籤並推送
[root@bogon identidock]# docker run -d -p 5000:5000 registry:2
9c72374fc20e044c6e0b69c0c2ac368b055832f0a39f2bdaad77e8cbd3f81c63
[root@bogon identidock]# docker tag hdlptz/identidock:0.1 localhost:5000/identidock:0.1
[root@bogon identidock]# docker push localhost:5000/identidock:0.1
The push refers to a repository [localhost:5000/identidock]
334ab24b425b: Pushed 
30e2362e5771: Pushed 
72811d39473b: Pushed 
f6cff21775f2: Pushed 
b3aa260c2eff: Pushed 
2fcec228e1b7: Pushed 
97d2d3bae505: Pushed 
95aadeabf504: Pushed 
b456afdc9996: Pushed 
d752a0310ee4: Pushed 
db64edce4b5b: Pushed 
d5d60fc34309: Pushed 
c01c63c6823d: Pushed 
0.1: digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f size: 3046

測試刪除後從新下載
[root@bogon identidock]# docker rmi localhost:5000/identidock:0.1
Untagged: localhost:5000/identidock:0.1
Untagged: localhost:5000/identidock@sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f

[root@bogon identidock]# docker pull localhost:5000/identidock:0.1
0.1: Pulling from identidock
Digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
Status: Downloaded newer image for localhost:5000/identidock:0.1

寄存器爲鏡像生成一個摘要值(digest),這是基於鏡像和它的元數據產生的一個惟一的散列值。
可以使用這個值來下載鏡像服務器

[root@bogon identidock]# docker pull localhost:5000/identidock@sha256:\
> 6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f: Pulling from identidock
Digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
Status: Image is up to date for localhost:5000/identidock@sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f

使用摘要的優勢是:保證下載的鏡像確實是你想要的,並保證完整性。網絡

2.遠程使用寄存服務器存在的問題
(1)直接調用時會報錯

[root@bogon identidock]# docker pull 192.168.1.100:5000/identidock:0.1

報錯緣由:
當Docker守護進程拒絕鏈接到遠程主機時,由於它沒有一個有效的傳輸層安全證書(Transport Layer Security,TLS)。
解決方法:
A.對將要訪問寄存器的全部Docker守護進程加上--insecure-registry 192.168.1.100:5000參數,而後重啓Docker守護進程;
B.在主機上安裝一個來自可信的證書頒發機構簽署的證書(如HTTPS訪問那樣);
C.在主機上安裝一個自簽名的證書(self-signed certificate),並同時給須要訪問寄存服務器的每一個Docker守護進程都安裝一份。

(2)建立自簽證書
在一臺長期用做寄存服務器的電腦上進行

[root@bogon ~]# mkdir registry_certs

[root@bogon ~]# openssl req -newkey rsa:4096 -nodes -sha256 \
> -keyout registry_certs/domain.key -x509 -days 365 \
> -out registry_certs/domain.crt

Generating a 4096 bit RSA private key
..........................................................................................++
.......................++
writing new private key to 'registry_certs/domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:China
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Anzhi
Organizational Unit Name (eg, section) []:Autometa
Common Name (eg, your name or your server's hostname) []:bogon
Email Address []:hdlptz@163.com

[root@bogon ~]# ls registry_certs/
domain.crt  domain.key

以後將證書複製到每一臺客戶端
[root@bogon ~]# mkdir -p /etc/docker/certs.d/bogon:5000
[root@bogon ~]# cp registry_certs/domain.crt /etc/docker/certs.d/bogon:5000/ca.crt
[root@bogon ~]# /bin/systemctl restart docker

啓動寄存服務(以數據卷方式把證書放入)
[root@bogon ~]# docker run -d -p 5000:5000 \
> -v $(pwd)/registry_certs:/certs \
> -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
> -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
> --restart=always --name registry registry:2
71d621a877b749a5f9b8dc798e78abf65faeebc2232dfa7e73dfc282d2608a0c

說明:

  • 建立一個x509的自簽證書和4096位的RSA私鑰,此證書使用SHA256的摘要簽署,有效期365天
  • Common Name必須與用來訪問的服務器名字相同,不可以使用IP地址
  • crt證書與各個客戶端共用,key密鑰必須嚴密保存

測試寄存服務:

[root@bogon ~]# docker pull debian:wheezy
wheezy: Pulling from library/debian
39e552a2b1f7: Pull complete 
Digest: sha256:e2a7ec190cf20118aa2a958c92cb9467d89b112a430848d3ff08461b60a0957f
Status: Downloaded newer image for debian:wheezy

[root@bogon ~]# docker tag debian:wheezy bogon:5000/debian:local

[root@bogon ~]# docker push bogon:5000/debian:local
The push refers to a repository [bogon:5000/debian]
dd7b8d3c2585: Pushed 
local: digest: sha256:2118c3e3e3b047084bf6716f63958594111078f257ee91fd52612b3c6bdbd71d size: 529

注意如下兩點:

  • 將證書複製到Docker引擎主機上的/etc/docker/certs.d/ /ca.crt
  • 確保Docker引擎可以解析寄存服務器地址
  • 寄存服務的選項由鏡像中的一個YAML文件配置,可經過數據卷將其替換,/go/src/github.com/docker/distribution/cmd/registry/config.yml,該文件僅適用於開發環境,若要用於生產,需進行大幅度優化

3.寄存服務的主要功能
(1)存儲
寄存服務器的鏡像默認使用文件系統驅動,全部數據和鏡像將會保存在文件系統上。
例如:如下的config.yml將配置寄存服務使用文件系統驅動,並將數據保存在/var/lib/registry,並且要把它定義爲一個數據卷。

storage:
    filesystem:
        rootdirectory: /var/lib/registry

另外,還能夠用Ceph分佈式對象存儲,利用Redis做爲內存緩存來加速鏡像層的訪問效率。

(2)身份驗證
A.在寄存服務以前設置一個代理(nginx)負責驗證用戶。
https://docs.docker.com/registry/recipes/nginx
可以使用docker login登陸

B.使用基於JSON網絡令牌(JSON Web Token)實現的令牌認證。
https://github.com/cesanta/docker_auth

(3)HTTP
配置寄存服務的HTTP接口

http:
    addr: bogon:5000
    secret: SAI4351-ALD346-3434-3DFG-23ADG341DF43
    tls:
        certificate: /certs/domain.crt
        key: /certs/domain.key

secert:用來簽署客戶端存儲的狀態信息的一個隨機字符串,爲了防止信息被篡改。

3、縮減鏡像大小

鏡像是由多個層所組成的,每一個鏡像對應Dockerfile以及其上的全部Dockerfile的每個命令。鏡像的總大小是全部鏡像層的總和。

對比兩個鏡像:

[root@bogon test]# cat Dockerfile 
FROM debian:wheezy

RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB
RUN rm /bigfile
[root@bogon test]# docker build -t filetest .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM debian:wheezy
 ---> f47fe1c60a2f
Step 2/3 : RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB
 ---> Running in d33340bf5012
1+0 records in
1+0 records out
50000000 bytes (50 MB) copied, 0.555286 s, 90.0 MB/s
 ---> 0f33a1416b1e
Removing intermediate container d33340bf5012
Step 3/3 : RUN rm /bigfile
 ---> Running in c00c9b9d05be
 ---> 7829ebda71e6
Removing intermediate container c00c9b9d05be
Successfully built 7829ebda71e6
Successfully tagged filetest:latest
[root@bogon test]# docker history filetest
IMAGE               CREATED                  CREATED BY                                      SIZE                COMMENT
7829ebda71e6        13 seconds ago           /bin/sh -c rm /bigfile                          0B                  
0f33a1416b1e        15 seconds ago           /bin/sh -c dd if=/dev/zero of=/bigfile cou...   50MB                
f47fe1c60a2f        Less than a second ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           Less than a second ago   /bin/sh -c #(nop) ADD file:4a0b4ab0f637224...   85.1MB

能夠看到,鏡像比基礎鏡像大了50MB,而rm刪除的文件並無對大小有所影響,這是由於rm在dd之上又添加了一個鏡像層,這是又Dockerfile文件所決定,所以對父層並無影響。

[root@bogon test]# cat Dockerfile 
FROM debian:wheezy

RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB && rm /bigfile
[root@bogon test]# docker build -t filetest .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM debian:wheezy
 ---> f47fe1c60a2f
Step 2/2 : RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB && rm /bigfile
 ---> Running in 8714793afce3
1+0 records in
1+0 records out
50000000 bytes (50 MB) copied, 0.247367 s, 202 MB/s
 ---> e9cab407d89b
Removing intermediate container 8714793afce3
Successfully built e9cab407d89b
Successfully tagged filetest:latest
[root@bogon test]# docker images filetest
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
filetest            latest              e9cab407d89b        11 seconds ago      85.1MB
[root@bogon test]# docker history filetest
IMAGE               CREATED                  CREATED BY                                      SIZE                COMMENT
e9cab407d89b        21 seconds ago           /bin/sh -c dd if=/dev/zero of=/bigfile cou...   0B                  
f47fe1c60a2f        Less than a second ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           Less than a second ago   /bin/sh -c #(nop) ADD file:4a0b4ab0f637224...   85.1MB

咱們修改了Dockerfile文件,使之在同一鏡像層中建立文件後再刪除,則文件不會被包含在鏡像中。

其餘示例:

RUN apt-get upfate \
         && apt-get install -y curl numactl \
         && rm -rf /var/lib/apt/lists/*

使用docker export及docker import會獲得只含有一個層的鏡像

[root@bogon test]# docker create identidock:latest
427acbf9d8be57bfa9e3d97320228e86d85d0e9128130c052407fc641c08692a

[root@bogon test]# docker export $(docker ps -lq) |docker import -
sha256:09ca0e0f1f6b5a610e0b1e12c9c189a8ee1e7b504f01aa2d2468bab7ea8e9c8b

[root@bogon test]# docker tag 09ca0e0 identidock:import

[root@bogon test]# docker images identidock
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
identidock          import              09ca0e0f1f6b        About a minute ago   686MB
identidock          0.1                 95f825228fdb        2 hours ago          703MB
identidock          latest              3966bef511e5        5 hours ago          699MB

[root@bogon test]# docker history identidock:import
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
09ca0e0f1f6b        2 minutes ago                           686MB               Imported from -

缺點:

  • 須要從新處理全部未反映在文件系統裏的Dockerfile指令(EXPOSE、CMD)
  • 與鏡像關聯的全部元數據將會丟失
  • 不再能與其餘具備相同父層的鏡像共享空間
相關文章
相關標籤/搜索