您好,登錄后才能下訂單哦!
這篇文章主要介紹“Docker下nginx外掛文件的方法是什么”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Docker下nginx外掛文件的方法是什么”文章能幫助大家解決問題。
文件不受docker鏡像文件的約束,可以修改,重啟容器,可以使用更新后的文件,不會(huì)被鏡像還原
容器運(yùn)行過程中記錄的文件如日志等信息,可以被自動(dòng)保存在外部存儲(chǔ)上,不會(huì)由于容器重啟而丟失
docker run命令
docker-compose命令
docker run命令方式,通過-v參數(shù)掛載外部主機(jī)目錄到容器內(nèi)的路徑上,有多個(gè)掛載點(diǎn),就通過多個(gè)-v參數(shù)指定,而且只能使用絕對路徑;docker-compose命令則通過service的方式描述容易,準(zhǔn)確的說一個(gè)服務(wù)下面可以包含多個(gè)容器,也是通過-v參數(shù)配置外部路徑的掛載配置,好處是可以使用相對路徑,當(dāng)然是相對與docker-compose.yml文件的路徑。還有一個(gè)好處是,docker-compose啟動(dòng)容器的命令比較簡單。
├── 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)時(shí)間</title> <body> <div id="datetime"> <script> setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();",1000); </script> </div> </body> </head> </html>
其實(shí)就是顯示當(dāng)前時(shí)間的一個(gè)頁面而已。
logs下面為空,目的是讓容器運(yùn)行時(shí)的日志寫到外部存儲(chǔ),即使容器停止或鏡像銷毀,運(yùn)行過的日志仍然可以保留。
conf.d下面為nginx個(gè)性化配置,內(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; }
其實(shí)也就是在nginx默認(rèn)的default.conf基礎(chǔ)上修改了端口和root路徑,目的是說明nginx的配置文件也可以使用外部存儲(chǔ)的,如果是自己的程序可以修改配置文件,那通過這樣的方式,可以在容器運(yùn)行過程中修改配置文件;修改的配置文件實(shí)際存儲(chǔ)在外部存儲(chǔ)上,因此不會(huì)隨著容器的停止運(yùn)行而消失,也不會(huì)恢復(fù)為鏡像內(nèi)部的文件。
為了方便,可以把運(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個(gè)-v,分別對應(yīng)不同的外部存儲(chǔ)的掛載,映射到容器內(nèi)的不同目錄下。
-p(注意是小寫)后面的端口分別為主機(jī)端口和容器端口,也就是主機(jī)的15180端口映射到容器的5180端口,這樣容器所啟動(dòng)的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
會(huì)顯示完整的信息,其中"Mounts"部分可以看到完整的存儲(chǔ)掛載映射情況。
直接看主機(jī)的nginx-vol/logs下面,可以看到容器中的nginx運(yùn)行日志自動(dòng)寫到了外部主機(jī)的存儲(chǔ)上。
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
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:外部存儲(chǔ)掛載映射
啟動(dòng)容器
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更簡單。
關(guān)于“Docker下nginx外掛文件的方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。