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端口
在这里插入图片描述

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