win0系統 使用Docker在本機運行nginx 掛載本地文件夾的內容和配置

Docker的安裝和運行

Docker的安裝和運行等內容,請參考這篇文章
鏈接: 通過vscode方便使用docker創建mysql數據庫 https://blog.csdn.net/goodboy31985/article/details/106204527

下載並運行docker上的Redis鏡像

  1. 首先下載docker上的redis鏡像
    docker pull nginx
  2. 在本機上創建相應的目錄
    在這裏插入圖片描述
    目錄位置可以根據你自己的需求
    然後在各個目錄下分別創建相應的文件
    在這裏插入圖片描述
    conf目錄下創建 nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

conf.d 目錄下創建 default.conf

server {
    listen       80;
    server_name  localhost;
  
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
  
    location / {  
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        #autoindex  on;
        #try_files $uri /index/index/page.html;
        #try_files $uri /index/map/page.html;
    }  
  
    #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  
    #  
    #location ~ \.php$ {  
    #    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;  
    #}  
}

以上兩個都是服務器的配置文件

logs文件夾是存放日誌的,保持空目錄即可

www文件夾是放置網站文件的 這裏用一個index.html爲例子,後期可以換成你自己的網站內容

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系統時間</title>
</head>

<body>
    Hello world!
    <div id="datetime">
        <script>
            setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
        </script>
    </div>
</body> 
  1. docker中設置允許共享的文件夾
    在這裏插入圖片描述
    打開設置,添加D盤根目錄,這樣在D盤中的文件或者文件夾才能掛載到容器中,這裏根據你自己需要掛載的文件位置進行設置

  2. 啓動nginx

docker run --name nginx81 -p 81:80 -v /D/Docker/nginx/www:/usr/share/nginx/html -v /D/Docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /D/Docker/nginx/logs:/var/log/nginx -v /D/Docker/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest

參數
-p 81:80 => 映射端口 訪問本機的81端口,相當於訪問容器的80端口
–name nginx81 =>容器別名
-v /D/Docker/nginx/www:/usr/share/nginx/html 將本機上的文件或者文件夾掛載到容器中,nginx啓動時,會讀取本機的內容,這裏有4個文件夾或者文件需要掛載
-d 保持後臺運行
注意本地路徑的寫法, D:\abc ⇒ /D/abc

啓動容器後,訪問本機的81端口
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章