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)建掛載目錄并關聯(lián) Nginx 容器的配置目錄后,以后可以通過修改主機下的配置目錄文件來直接影響到容器內(nèi)的配置。
mkdir -p /data/nginx/{conf,log,html}
把 Nginx 容器中的文件復制到主機
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>
或者打開瀏覽器輸入服務器 IP 驗證
到此這篇關于Docker 安裝 Nginx 并掛載目錄的實現(xiàn)示例的文章就介紹到這了,更多相關Docker Nginx掛載目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
docker Get https://registry-1.docker.io/v2/:
本文主要介紹了docker Get https://registry-1.docker.io/v2/: net/http: request canceled 報錯,具有一定的參考價值,感興趣的可以了解一下2025-03-03