1、Docker鏡像介紹
html
一、鏡像組成介紹nginx
分層構建的,底層的是bootfs,上面的是rootfsweb
bootfs的類型能夠是btrfs、aufs、lxc,還須要一個內核,可是這個內核僅僅是用於啓動容器的用戶控件的docker
rootfs表現爲一個根文件系統,這裏麪包括不少的文件和目錄apache
在啓動容器的時候,這兩層都是以只讀的方式來掛載的。json
構建鏡像的基本流程vim
先準備一個bootfsbash
而後安裝一個最小系統(base image)服務器
在系統安裝應用,若是是構建apache的鏡像,那麼就在base image上安裝apachecurl
注意:
鏡像都是隻讀的
當啓動容器的時候,會在鏡像的基礎上再添加一個當前容器的專用層,這層是讀寫的
在刪除容器的時候,這個容器的專屬的這個讀寫層就會被刪除,因此默認容器沒法實現數據的持久存儲。
二、鏡像倉庫
前面一講過了,專門用來存儲docker iamge的哪一個位置稱之爲 docker registry,在啓動容器的時候,本地的docker daemon會從指定的docker registry下載鏡像,並完成啓動。
docker registry是能夠分爲多類 的
Vendor registry:官方的倉庫
Mirror registry:像阿里雲之類的鏡像加速
Private registry:用戶本身建立鏡像倉庫,好比企業內部須要大規模部署時候,能夠本身定製鏡像,並放到本身倉庫中
通常的registry有兩部分組成:
第一部分:Repository
一個registry能夠有多個repository
Repository能夠分爲頂級倉庫和用戶倉庫,用戶倉庫的命名是:用戶名/倉庫名
Repository的名稱通常就是應用的名稱,並且在Repository中有應用的多個版本
第二部分:index
維護帳戶信息
提供檢索端口
三、從鏡像倉庫下載鏡像的方法
格式以下
docker pull <registry>[:port] /[<namespace>/]<name>:<tag>
registry:port 這裏是指定從哪一個docker服務器來獲取鏡像,若是用的是docker官方本身的倉庫(hub.docker.com),那麼這裏能夠省略。
namespace 這裏是指定來自於哪一個名稱空間,也就是哪一個用戶的倉庫, 若是的頂級的倉庫,那麼這個也能夠省略
除了https://hub.docker.com以後,其實還有別的,例如:https://hub.daocloud.io/,再例如CoreOS所維護的:https://quay.io
由於不是默認倉庫, 所以在下載鏡像的時候,須要指定地址
從quay.io 下載 flannel舉例以下
第一步:登陸https://quay.io,搜索flannel
第二步:找到項目地址
第三步:查看下載鏡像的方法
這種方法是不能用的,由於須要指定標籤
第四步:查看具體的標籤
第五步:下載鏡像
[root@host1 ~]# docker pull quay.io/coreos/flannel:v0.11.0-s390x
[root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest b534869c81f0 2 weeks ago 1.22MB nginx 1.14-alpine 8a2fb25a19f5 8 months ago 16MB quay.io/coreos/flannel v0.10.0-s390x 463654e4ed2d 23 months ago 47MB
2、製做鏡像
一、製做鏡像方法種類
基於dockerfile
基於現有的容器:基於容器作鏡像,通常就是先安裝一個最小容器,而後在這個容器中安裝應用程序,而後將這個安裝了程序的容器作成鏡像就能夠了
基於docker hub的自動建立功能
二、基於現有容器作鏡像
第一步:啓動一個busybox容器,並建立一個html頁面
[root@host1 ~]# docker run --name img1 -it busybox / # mkdir /data/html -p / # echo "test page[v1.0]">>/data/html/index.html
第二步:再開一個終端,將容器製做成鏡像
製做鏡像用命令commit
要製做鏡像的容器不能中止
將容器製做爲鏡像的時候,最好讓容器暫停一下,這須要用選項-p
默認製做的鏡像沒有tag,也不屬於任何的repository
[root@host1 ~]# docker commit -p img1 sha256:cd7cb2a774400c721ed71f62bd20abe2c000f1d0f7d51d3bf025db1239b86b7d
[root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> cd7cb2a77440 6 seconds ago 1.22MB
第三步:給鏡像打標籤
打標籤用tag命令
一個鏡像能夠有多個不一樣的標籤
[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:v1-0 [root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE zxhk/httpd v1-0 cd7cb2a77440 2 minutes ago 1.22MB
再打個標籤
[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:latest [root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE zxhk/httpd latest cd7cb2a77440 3 minutes ago 1.22MB zxhk/httpd v1-0 cd7cb2a77440 3 minutes ago 1.22MB
注意:
一個鏡像有多個標籤的話,如何進行刪除,須要將這多個鏡像都刪除才行,相似於系統的硬連接
第四步:基於這個進行啓動一個容器,並在容器中運行apache
[root@host1 ~]# docker run --name newhttpd -it zxhk/httpd:latest / # httpd -f -h /data/html
此時,而後apache能運行,可是每次新啓動一個容器,都需手動啓動apache,接下來對鏡像進行調整,實現啓動容器後自動運行apache
第五步:升級鏡像實現自動運行內部的apache
先看看咱們作的鏡像的詳細信息
[root@host1 ~]# docker inspect zxhk/httpd:latest
其中有一部分是Cmd,其中就是容器運行起來之後要執行的命令,以下
"Cmd": [ "sh" ],
commit建立鏡像的時候會能夠經過選項來設置這些內容
-a:指定做者
-c:更改基於鏡像啓動後執行的命令
-m:描述系想你
-p:暫停
再從新作個鏡像
[root@host1 ~]# docker commit \ > -a "zxhk<237745635@qq.com>" \ > -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' \ > -p img3 zxhk/httpd:v2.0
用這個鏡像啓動一個容器
[root@host1 ~]# docker run --rm --name test-httpd zxhk/httpd:v2.0
看一下容器中執行的命令
[root@host1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 243f050288bd zxhk/httpd:v2.0 "/bin/httpd -f -h /d…" 16 seconds ago Up 15 seconds test-httpd
看一下地址信息
[root@host1 ~]# docker inspect 243 | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.4", "IPAddress": "172.17.0.4",
在宿主機訪問測試容器中的站點
[root@host1 ~]# curl 172.17.0.4 test page[v1.0]
至此,鏡像建立完成
3、將製做的鏡像上傳到docker hub中
一、在https://hub.docker.com/註冊用戶
須要爬過牆頭才能註冊,你懂的!!!
註冊帳戶過程-略
二、在docker hub上建立repository和registry
注意:
建立的倉庫名稱必需要和鏡像的名稱一致
三、向本身的倉庫中上傳鏡像文件
第一步:登錄docker hub【個人用戶名是zxhk】
[root@localhost ~]# docker login -uzxhk 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
第二步:上傳鏡像到hub【此處咱們上傳httpd鏡像的二個版本都傳上去】
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE zxhk/httpd v2.0 89a647171235 18 hours ago 1.22MB zxhk/httpd latest cd7cb2a77440 19 hours ago 1.22MB zxhk/httpd v1-0 cd7cb2a77440 19 hours ago 1.22MB
[root@localhost ~]# docker push zxhk/httpd:v2.0 The push refers to repository [docker.io/zxhk/httpd] f577c88ef366: Pushed eac247cb7af5: Mounted from library/busybox v2.0: digest: sha256:c1c3e604e37652595563b8dc2be877620c77314c925115c7ba35f9969b1a77a0 size: 734
[root@localhost ~]# docker push zxhk/httpd:v1-0
第三步:在docker hub上查看一下
第四步:使用docker hub中咱們本身的鏡像
在docker hub中已經標識了鏡像的使用方法,以下:
爲了效果,現將本地的鏡像刪除
[root@localhost ~]# docker rmi 89 zxhk/httpd:v1-0 [root@localhost ~]# docker rmi 89 zxhk/httpd:v2.0
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest b534869c81f0 2 weeks ago 1.22MB
下載鏡像啓動容器
[root@localhost ~]# docker pull zxhk/httpd:v2.0 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE zxhk/httpd v2.0 89a647171235 19 hours ago 1.22MB busybox latest b534869c81f0 2 weeks ago 1.22MB [root@localhost ~]# docker run --rm --name web1 89a
查看一下容器的信息
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ec8687bb487 89a "/bin/httpd -f -h /d…" 16 seconds ago Up 15 seconds web1
[root@localhost ~]# docker inspect 0ec | grep "IPAddr" "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2",
[root@localhost ~]# curl 172.17.0.2 test page[v1.0]
4、將製做的鏡像上傳到阿里雲的鏡像倉庫中
一、在阿里雲註冊用戶
略
二、進入容器鏡像倉庫
三、使用阿里雲作鏡像加速的方法
去docker配置文件中添加一個鏡像文件
[root@localhost ~]# vim /etc/docker/daemon.json { "registry-mirrors": [ "https://registry.docker-cn.com", "https://mzxx8xy8.mirror.aliyuncs.com" ] }
重啓服務
[root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl restart docker
四、使用阿里雲建立倉庫
看看鏡像倉庫的使用方法
五、向阿里雲倉庫上傳鏡像
第一步:使用憑證登陸阿里雲
[root@localhost ~]# sudo docker login --username=zxhk registry.cn-hangzhou.aliyuncs.com 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@localhost ~]# docker tag 89a registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0 [root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0
第三步:從阿里雲拉取鏡像
[root@localhost ~]# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0