外掛文件的目的:
-
文件不受docker鏡像文件的約束,可以修改,重啟容器,可以使用更新后的文件,不會被鏡像還原
-
容器運行過程中記錄的文件如日志等信息,可以被自動保存在外部存儲上,不會由于容器重啟而丟失
而運行容器有兩種方式:
-
docker run命令
-
docker-compose命令
docker run命令方式,通過-v參數掛載外部主機目錄到容器內的路徑上,有多個掛載點,就通過多個-v參數指定,而且只能使用絕對路徑;docker-compose命令則通過service的方式描述容易,準確的說一個服務下面可以包含多個容器,也是通過-v參數配置外部路徑的掛載配置,好處是可以使用相對路徑,當然是相對與docker-compose.yml文件的路徑。還有一個好處是,docker-compose啟動容器的命令比較簡單。
假設鏡像打包路徑結構如下:
├──?build.sh ├──?docker-compose.yml ├──?Dockerfile ├──?mynginx.conf ├──?nginx-vol │???├──?conf.d │???│???└──?mynginx.conf │???├──?html │???│???└──?index.html │???└──?logs │???????├──?access.log │???????└──?error.log └──?run.sh
Dockerfile為構建鏡像的配置文件,內容如下:
FROM?nginx LABEL?maintainer="xxx"?email="<xxx>"?app="nginx?test"?version="v1.0" ENV?WEBDIR="/data/web/html" RUN?mkdir?-p?${WEBDIR} EXPOSE?5180</xxx>
以nginx為基礎,指定新的數據文件路徑為/data/web/html,暴露端口為5180。
通過以下命令編譯新的鏡像:
docker?build?-t?nginx:test-v1?.
編譯出來的鏡像tag為test-v1,可以查看本地鏡像:
docker?images REPOSITORY???TAG???????IMAGE?ID???????CREATED??????????SIZE nginx????????test-v1???d2a0eaea3fac???56?minutes?ago???141MB nginx????????latest????605c77e624dd???9?days?ago???????141MB
可以看到TAG為test-v1的鏡像是剛剛編譯出來的新鏡像。
創建nginx外掛卷nginx-vol以及相關的conf.d、logs、html文件夾,并把對應的內容放入各自對應的目錄下。如html文件夾下的iindex.html內容如下:
???????? ????????????????<meta> ????????????????<title>系統時間</title> ???????? ????????????????<div> ????????????????????????<script> setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();",1000); </script> ????????????????</div> ???????? ????????
其實就是顯示當前時間的一個頁面而已。
logs下面為空,目的是讓容器運行時的日志寫到外部存儲,即使容器停止或鏡像銷毀,運行過的日志仍然可以保留。
conf.d下面為nginx個性化配置,內容如下:
server?{ ????listen???????5180; ????#listen??[::]:5180; ????server_name??localhost; ????#access_log??/var/log/nginx/host.access.log??main; ????location?/?{ ????????root???/data/web/html; ????????index??index.html?index.htm; ????} ????#error_page??404??????????????/404.html; ????#?redirect?server?error?pages?to?the?static?page?/50x.html ????# ????error_page???500?502?503?504??/50x.html; ????location?=?/50x.html?{ ????????root???/usr/share/nginx/html; ????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80 ????#location?~?.php$?{ ????#????proxy_pass???http://127.0.0.1; ????#} ????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on?127.0.0.1:9000 ????#????root???????????html; ????#????fastcgi_pass???127.0.0.1:9000; ????#????fastcgi_index??index.php; ????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name; ????#????include????????fastcgi_params; ????#?deny?access?to?.htaccess?files,?if?Apache's?document?root ????#?concurs?with?nginx's?one ????#location?~?/.ht?{ ????#????deny??all; }
其實也就是在nginx默認的default.conf基礎上修改了端口和root路徑,目的是說明nginx的配置文件也可以使用外部存儲的,如果是自己的程序可以修改配置文件,那通過這樣的方式,可以在容器運行過程中修改配置文件;修改的配置文件實際存儲在外部存儲上,因此不會隨著容器的停止運行而消失,也不會恢復為鏡像內部的文件。
docker run模式
為了方便,可以把運行命令寫到shell腳本中,如run.sh,內容如下:
docker?run?--name?nginx-v1?-p?15180:5180?-v?/home/project/nginx-test/nginx-vol/logs:/var/log/nginx?-v?/home/project/nginx-test/nginx-vol/conf.d:/etc/nginx/conf.d?-v?/home/project/nginx-test/nginx-vol/html:/data/web/html?-d?nginx:test-v1
可以看到命令中有3個-v,分別對應不同的外部存儲的掛載,映射到容器內的不同目錄下。
-p(注意是小寫)后面的端口分別為主機端口和容器端口,也就是主機的15180端口映射到容器的5180端口,這樣容器所啟動的nginx服務端口5180就可以通過訪問主機的15180端口而被映射起來。
查看運行中的容器:
docker?ps?-a CONTAINER?ID???IMAGE???????????COMMAND??????????????????CREATED?????????STATUS?????????PORTS?????????????????????????????????????????????????NAMES cf2275da5130???nginx:test-v1???"/docker-entrypoint.…"???6?seconds?ago???Up?5?seconds???80/tcp,?0.0.0.0:15180->5180/tcp,?:::15180->5180/tcp???nginx-v1
詳細映射查看:
docker?inspect?nginx-v1
會顯示完整的信息,其中”Mounts”部分可以看到完整的存儲掛載映射情況。
直接看主機的nginx-vol/logs下面,可以看到容器中的nginx運行日志自動寫到了外部主機的存儲上。
ls?-l?nginx-vol/logs/ total?12 -rw-r--r--?1?root?root?1397?1月???8?15:08?access.log -rw-r--r--?1?root?root?4255?1月???8?15:59?error.log
停止容器:
docker?stop?nginx-v1
刪除容器:
docker?rm?nginx-v1
docker-compose模式
安裝docker-compose
apt-get?install?docker-compose
編寫docker-compose.yml文件
version:?"3" services: ????????nginx: ????????????????container_name:?mynginx ????????????????image:?nginx:test-v1 ????????????????ports: ????????????????????????-?80:5180 ????????????????volumes: ????????????????????????-?./nginx-vol/html:/data/web/html ????????????????????????-?./nginx-vol/logs:/var/log/nginx ????????????????????????-?./nginx-vol/conf.d:/etc/nginx/conf.d ????????????????restart:?always
container_name:指定容器名稱
image:要使用的鏡像以及對應的標簽
ports:主機端口與容器端口映射
volumes:外部存儲掛載映射
啟動容器
docker-compose?up?-d Creating?network?"nginxtest_default"?with?the?default?driver Creating?mynginx?... Creating?mynginx?...?done
查看容器
docker?ps?-a CONTAINER?ID???IMAGE???????????COMMAND??????????????????CREATED??????????STATUS??????????PORTS???????????????????????????????????????????NAMES 635e2999c825???nginx:test-v1???"/docker-entrypoint.…"???24?seconds?ago???Up?22?seconds???80/tcp,?0.0.0.0:80->5180/tcp,?:::80->5180/tcp???mynginx
可以看到容器按照docker-compose.yml配置運行,端口、名稱、掛載都正常。訪問主機的80端口即可對應容器的5180服務。
停止容器
docker-compose?down Stopping?mynginx?...?done Removing?mynginx?...?done Removing?network?nginxtest_default
可以看到,使用docker-compose更簡單。