網(wǎng)站首頁 編程語言 正文
外掛文件的目的:
- 文件不受docker鏡像文件的約束,可以修改,重啟容器,可以使用更新后的文件,不會被鏡像還原
- 容器運(yùn)行過程中記錄的文件如日志等信息,可以被自動保存在外部存儲上,不會由于容器重啟而丟失
而運(yùn)行容器有兩種方式:
- docker run命令
- docker-compose命令
docker run命令方式,通過-v參數(shù)掛載外部主機(jī)目錄到容器內(nèi)的路徑上,有多個掛載點(diǎn),就通過多個-v參數(shù)指定,而且只能使用絕對路徑;docker-compose命令則通過service的方式描述容易,準(zhǔn)確的說一個服務(wù)下面可以包含多個容器,也是通過-v參數(shù)配置外部路徑的掛載配置,好處是可以使用相對路徑,當(dāng)然是相對與docker-compose.yml文件的路徑。還有一個好處是,docker-compose啟動容器的命令比較簡單。
假設(shè)鏡像打包路徑結(jié)構(gòu)如下:
├── 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為構(gòu)建鏡像的配置文件,內(nèi)容如下:
FROM nginx LABEL maintainer="xxx" email="<xxx@xxx.com>" app="nginx test" version="v1.0" ENV WEBDIR="/data/web/html" RUN mkdir -p ${WEBDIR} EXPOSE 5180
以nginx為基礎(chǔ),指定新的數(shù)據(jù)文件路徑為/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的鏡像是剛剛編譯出來的新鏡像。
創(chuàng)建nginx外掛卷nginx-vol以及相關(guān)的conf.d、logs、html文件夾,并把對應(yīng)的內(nèi)容放入各自對應(yīng)的目錄下。如html文件夾下的iindex.html內(nèi)容如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>系統(tǒng)時間</title> <body> <div id="datetime"> <script> setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();",1000); </script> </div> </body> </head> </html>
其實就是顯示當(dāng)前時間的一個頁面而已。
logs下面為空,目的是讓容器運(yùn)行時的日志寫到外部存儲,即使容器停止或鏡像銷毀,運(yùn)行過的日志仍然可以保留。
conf.d下面為nginx個性化配置,內(nèi)容如下:
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默認(rèn)的default.conf基礎(chǔ)上修改了端口和root路徑,目的是說明nginx的配置文件也可以使用外部存儲的,如果是自己的程序可以修改配置文件,那通過這樣的方式,可以在容器運(yùn)行過程中修改配置文件;修改的配置文件實際存儲在外部存儲上,因此不會隨著容器的停止運(yùn)行而消失,也不會恢復(fù)為鏡像內(nèi)部的文件。
docker run模式
為了方便,可以把運(yùn)行命令寫到shell腳本中,如run.sh,內(nèi)容如下:
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,分別對應(yīng)不同的外部存儲的掛載,映射到容器內(nèi)的不同目錄下。
-p(注意是小寫)后面的端口分別為主機(jī)端口和容器端口,也就是主機(jī)的15180端口映射到容器的5180端口,這樣容器所啟動的nginx服務(wù)端口5180就可以通過訪問主機(jī)的15180端口而被映射起來。
查看運(yùn)行中的容器:
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
詳細(xì)映射查看:
docker inspect nginx-v1
會顯示完整的信息,其中"Mounts"部分可以看到完整的存儲掛載映射情況。
直接看主機(jī)的nginx-vol/logs下面,可以看到容器中的nginx運(yùn)行日志自動寫到了外部主機(jī)的存儲上。
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:要使用的鏡像以及對應(yīng)的標(biāo)簽
ports:主機(jī)端口與容器端口映射
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配置運(yùn)行,端口、名稱、掛載都正常。訪問主機(jī)的80端口即可對應(yīng)容器的5180服務(wù)。
停止容器
docker-compose down Stopping mynginx ... done Removing mynginx ... done Removing network nginxtest_default
可以看到,使用docker-compose更簡單。
原文鏈接:https://blog.csdn.net/brucemiao/article/details/122617768
相關(guān)推薦
- 2022-11-01 C++中的pair使用詳解_C 語言
- 2022-04-14 error: failed to push some refs to ‘http://git.tex
- 2022-09-22 NCL:Improving Graph Collaborative Filtering with N
- 2022-11-10 Rust?use關(guān)鍵字妙用及模塊內(nèi)容拆分方法_相關(guān)技巧
- 2022-09-04 使用Python去除小數(shù)點(diǎn)后面多余的0問題_python
- 2022-06-01 React函數(shù)式組件與類組件的不同你知道嗎_React
- 2022-06-11 sql?server排查死鎖優(yōu)化性能_MsSql
- 2022-05-13 當(dāng)你敲完Hello World后的第一步——C語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支