3-Docker映象介紹
阿新 • • 發佈:2021-07-13
1、Docker映象介紹
Docker映象含有啟動容器所需要的檔案系統及其內容,因此docker images是用於建立並啟動docker容器。docker映象採用分層構建機制,最底層為bootfs,其他為rootfs。
- bootfs,用於系統引導的檔案系統,包括bootloader和kernel,容器啟動完成後會被解除安裝以節約記憶體
- rootfs:位於boofs之上,表現為docker容器的根檔案系統
- 傳統模式中,系統啟動之時,核心掛載rootfs時會首先掛載為只讀模式,完整性自檢完成後將其重新掛載為讀寫模式
- docker中,rootfs由核心掛載為只讀模式,而後通過聯合掛載技術額外掛載一個“可寫”層
2、Docker分層映象檔案系統
- 高階多層統一檔案系統
- 用於為linux檔案系統實現“聯合掛載”
- aufs是之前的UnionFS的實現,但從來沒有編譯到linux核心中
- docker最初使用aufs作為容器的檔案系統層
- aufs的競爭產品是overlayfs,overlayfs從3.18版本後被合併到linux核心
- docker的分層映象,除了aufs、還支援btrfs、devicemapper和vfs等
- 在Ubuntu,預設使用aufs,在centos7上預設使用devicemapper。
3、Docker Registry
Registry用於儲存docker映象,包括映象的層次結構和元資料
使用者可以自建Registry,也可以使用官方的Docker Hub
docker Registry中的映象通常由開發人員製作,然後推送到公共或者私有倉庫儲存。供其他人員使用,例如部署到生產環境。
一個registry一般由兩部分組成
- Repository
- 由特定的docker映象的所有迭代版本組成的映象長褲
- 一個registry中可以存在多個Repository
- 每個倉庫可以包含多個Tag,每個Tag對應一個映象
- Index
- 維護使用者賬戶、映象校驗、以及公共名稱空間的資訊
- 相當於為registry提供一個完成使用者認證等功能的索引介面。
4、Docker映象製作
docker映象有以下生成途徑
-
Dockerfile
-
基於容器製作
- 在容器的可寫層做變更操作,完成後把當前執行的容器commit為新的容器
- 方式一:互動式映象
# 啟動容器 [root@localhost ~]# docker run --name b1 -it busybox:latest # 修改容器,建立個Http服務 / # mkdir -p /data/html / # echo 'hello docker' > /data/html/index.html # 製作新的映象,注意基於容器製作映象時要保持容器啟動狀態 [root@localhost ~]# docker commit -p b1 # -p表示暫停容器狀態,防止有新的內容寫入容器導致映象不完整。執行完成後會生成映象id sha256:210ce5042f58544e198c3cff6e592877ea42051d42d76c58ddff329d6d429bd7 # 檢視生成的映象 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 210ce5042f58 3 minutes ago 1.22MB # 上面生成的映象是沒有名稱和tag的。可通過tag指令指定。 [root@localhost ~]# docker tag 210ce5042f58 yull/bbox_http:v0.1 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yull/bbox_http v0.1 210ce5042f58 4 minutes ago 1.22MB # 通過生成映象啟動容器,檢視自定義的映象是否生效 [root@localhost ~]# docker run --name b2 -it yull/bbox_http:v0.1 /bin/sh WARNING: IPv4 forwarding is disabled. Networking will not work. / # cat /data/html/index.html hello docker # 檢視容器情況,在CMD欄中會顯示預設啟動的命令,比如sh表示預設啟動sh環境 [root@localhost ~]# docker inspect b2 ... "Cmd": [ "/bin/sh" ], ...
- 方式二:非互動式映象
# 啟動容器 [root@localhost ~]# docker run --name b1 -it busybox:latest # 修改容器,建立個Http服務 / # mkdir -p /data/html / # echo 'hello docker' > /data/html/index.html # 製作新的映象,注意基於容器製作映象時要保持容器啟動狀態 [root@localhost ~]# docker commit -a "larry.yu<[email protected]>" -c 'CMD ["/bin/httpd","-f","-h","/data/www"]' -p b3 yull/bbox_http:v0.2 sha256:005a20ba36bbea35f988fcc512fca868be50c56a021ca86ea517ba7178e82e17 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yull/bbox_http v0.2 005a20ba36bb 5 seconds ago 1.22MB # 基於自定義映象建立容器 [root@localhost ~]# docker run --name b4 yull/bbox_http:v0.2 WARNING: IPv4 forwarding is disabled. Networking will not work. # 啟動後沒有任何提示,是因為啟動的為非互動式容器,通過curl訪問容器的http服務驗證 [root@localhost ~]# curl http://172.17.0.3 hello busybox http # 檢視容器資訊 [root@localhost ~]# docker inspect b4 ... "Cmd": [ "/bin/httpd", "-f", "-h", "/data/www" ], ... "IPAddress": "172.17.0.3", ... # 檢視映象列表 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE yull/bbox_http v0.2 005a20ba36bb 6 days ago 1.22MB yull/bbox_http v0.1 210ce5042f58 6 days ago 1.22MB busybox latest 19485c79a9bb 2 weeks ago 1.22MB nginx stable-alpine 8587e8f26fc1 3 weeks ago 21.2MB redis latest f7302e4ab3a8 5 weeks ago 98.2MB
-
docker hub automated builds
5、將映象push到阿里雲
- 建立映象倉庫
建立完成後點選管理,可以檢視到倉庫資訊
- 建立倉庫登入密碼
- 終端登入阿里雲docker registry
[root@localhost ~]# docker login [email protected] registry.cn-shanghai.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
# 檢視需要push的映象
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
yull/bbox_http v0.2 005a20ba36bb 6 days ago 1.22MB
# 建立阿里雲專用tag
[root@localhost ~]# docker tag 005a20ba36bb registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd v0.2 005a20ba36bb 6 days ago 1.22MB
yull/bbox_http v0.2 005a20ba36bb 6 days ago 1.22MB
# push映象到阿里雲倉庫
[root@localhost ~]# docker push registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2
The push refers to repository [registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd]
04f9d779f461: Pushed
6c0ea40aef9d: Pushed
v0.2: digest: sha256:ecee3411fa4ef0f8da07b237f04561549add36a70edfc5176d246831641ee806 size: 734
# 成功後就可以在映象版本中看到
# 下載映象可以使用
docker pull registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2
6、Docker映象匯入匯出
- 映象匯出/儲存
[root@localhost ~]# docker save -o busybox-httpd.gz yull/bbox_http:v0.2
[root@localhost ~]# ls
anaconda-ks.cfg busybox-httpd.gz
- 映象匯入
[root@localhost ~]# docker load -i busybox-httpd.gz