Docker 安裝 Nginx 并掛載目錄的實現(xiàn)示例
1.拉取鏡像
docker pull nginx
2.創(chuàng)建容器
docker docker run -d --name nginx -p 80:80 nginx
Bash 執(zhí)行 docker ps 查看

3.創(chuàng)建掛載目錄
為什么要創(chuàng)建掛載目錄?因為docker容器內(nèi)部有自己的文件系統(tǒng),在主機創(chuàng)建掛載目錄并關(guān)聯(lián) Nginx 容器的配置目錄后,以后可以通過修改主機下的配置目錄文件來直接影響到容器內(nèi)的配置。
mkdir -p /data/nginx/{conf,log,html}
把 Nginx 容器中的文件復(fù)制到主機
docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf docker cp nginx:/etc/nginx/conf.d /data/nginx/conf/conf.d docker cp nginx:/usr/share/nginx/html /data/nginx/
4.停止并刪除 Nginx 容器
執(zhí)行 docker stop + 容器ID的前兩位
docker stop 6a docker rm 6a
5.重新創(chuàng)建容器
docker run -d --name nginx -p 80:80 \ -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /data/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /data/nginx/log:/var/log/nginx \ -v /data/nginx/html:/usr/share/nginx/html \ --privileged=true nginx
執(zhí)行docker ps 查看運行狀態(tài),如果啟動失敗執(zhí)行 docker logs + 容器ID前兩位 查看報錯。
6.驗證
curl localhost
出現(xiàn)以下內(nèi)容說明安裝成功
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a rel="external nofollow" >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
或者打開瀏覽器輸入服務(wù)器 IP 驗證

到此這篇關(guān)于Docker 安裝 Nginx 并掛載目錄的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Docker Nginx掛載目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
騰訊云服務(wù)器docker開啟端口后無法訪問的解決方法
本文主要介紹了騰訊云服務(wù)器docker開啟端口后無法訪問的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
docker Get https://registry-1.docker.io/v2/:
本文主要介紹了docker Get https://registry-1.docker.io/v2/: net/http: request canceled 報錯,具有一定的參考價值,感興趣的可以了解一下2025-03-03
無網(wǎng)絡(luò)docker鏡像遷移的實現(xiàn)
本文主要介紹了無網(wǎng)絡(luò)docker鏡像遷移的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-06-06

