docker 映象製作
1、基於映象 製作
(1)獲取基礎映象busybox,並啟動容器,--name指定容器名稱為bs1
(2)在容器內,建立目錄,並建立檔案index.html(根據業務需求製作)
index.html檔案內容自己定義
(3)新開一個終端,執行以下命令檢視引數介紹(映象製作過程中,容器必須處於執行狀態)
docker commit -h
(4)製作映象
# docker commit 的語法格式為: # docker commit [選項] <容器ID或容器名> [<倉庫名>[:<標籤>]] docker commit -p -a "caicai <[email protected]>" -m "增加index.html網頁" bs1 caicai/httpd:V0-1-1-2 # -a --author 是指定修改的作者 # -c --change list 應用dockerfile 修改CMD命令 # -m --message 則是記錄本次修改的內容 # -p Pause container during commit (default true)
(5)為映象指定tag標籤
(6)為映象執行其他標籤(一個映象可以對應多個標籤,但是一個標籤只能對應一個映象)
(7)修改cmd啟動命令
假設我們修改前面生成的映象啟動命令為httpd,
1、首先檢視現有映象的cmd命令
docker inspect busybox cmd命令,預設為/bin/sh # "Cmd": [ # "/bin/sh", # "-c", # "#(nop) ", # "CMD [\"sh\"]" # ]
2、啟動容器,檢視容易預設為sh
docker run --name t2 -it caicai/httpd:V0-1-1-2
3、啟動容器,檢視httpd使用方法
/ # httpd -h Usage: httpd [-D name] [-d directory] [-f file] [-C "directive"] [-c "directive"] [-k start|restart|graceful|graceful-stop|stop] [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X] Options: -D name : define a name for use in <IfDefine name> directives -d directory : specify an alternate initial ServerRoot -f file : specify an alternate ServerConfigFile -C "directive" : process directive before reading config files -c "directive" : process directive after reading config files -e level : show startup errors of level (see LogLevel) -E file : log startup errors to file -v : show version number -V : show compile settings -h : list available command line options (this page) -l : list compiled in modules -L : list available configuration directives -t -D DUMP_VHOSTS : show parsed vhost settings -t -D DUMP_RUN_CFG : show parsed run settings -S : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG -t -D DUMP_MODULES : show all loaded modules -M : a synonym for -t -D DUMP_MODULES -t : run syntax check for config files -T : start without DocumentRoot(s) check -X : debug mode (only one worker, do not detach)
3、啟動容器,docker commit -c修改cmd命令
docker commit -a "caicai <junchao837@foxmail>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p t2 caicai/httpd:V0-1-1-3 # -a 指定作者 # -c 修改cmd命令,格式為cmd list # -p 執行容器
4、執行容器(預設不是互動模式)
5、檢視已執行容器(執行命令為/bin/httpd)
6、訪問容器
# 通過以下方式獲取容器地址,檢視Networks對應的ip docker inspect t3 # t3為容器名 # 訪問容器 curl 172.17.0.7 # <h1>Busybox httpd server.</h1> 返回值為/data/html/index.html中內容
6、至此,一個微型web服務映象就製作成功了
慎用docker commit
使用docker commit
命令雖然可以比較直觀的幫助理解映象分層儲存的概念,但是實際環境中並不會這樣使用。
首先,如果仔細觀察之前的docker diff webserver
的結果,你會發現除了真正想要修改的/usr/share/nginx/html/index.html
檔案外,由於命令的執行,還有很多檔案被改動或添加了。這還僅僅是最簡單的操作,如果是安裝軟體包、編譯構建,那會有大量的無關內容被新增進來,如果不小心清理,將會導致映象極為臃腫。
此外,使用docker commit
意味著所有對映象的操作都是黑箱操作,生成的映象也被稱為黑箱映象,換句話說,就是除了製作映象的人知道執行過什麼命令、怎麼生成的映象,別人根本無從得知。而且,即使是這個製作映象的人,過一段時間後也無法記清具體在操作的。雖然docker diff
或許可以告訴得到一些線索,但是遠遠不到可以確保生成一致映象的地步。這種黑箱映象的維護工作是非常痛苦的。
而且,映象所使用的分層儲存,除當前層外,之前的每一層都是不會發生改變的,換句話說,任何修改的結果僅僅是在當前層進行標記、新增、修改,而不會改動上一層。如果使用docker commit
製作映象,以及後期修改的話,每一次修改都會讓映象更加臃腫一次,所刪除的上一層的東西並不會丟失,會一直如影隨形的跟著這個映象,即使根本無法訪問到。這會讓映象更加臃腫。
Union FS 是有最大層數限制的,比如 AUFS,曾經是最大不得超過 42 層,現在是不得超過 127 層。就是commit最多127次。