Docker中怎么运行nginx并挂载本地目录到镜像中

发布时间 - 2023-05-22 00:00:00    点击率:

1 从hup上pull镜像

docker pull nginx

2 创建将要挂载的目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

3 先要有配置文件才能启动容器

3.1 vim /data/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;
}

3.2 vim /data/nginx/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  /data/nginx/html; 
    # root  /usr/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; 
  #} 
}

4 启动容器

#将容器中nginx的80端口映射到本地的81端口
docker run --name nginx81 -d -p 81:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest

5 查看启动的容器

[root@dc01 ~]# docker ps
container id image command created status ports names
fa56f865bd26 nginx:latest "nginx -g 'daemon of…" 4 weeks ago up 3 seconds 0.0.0.0:80->80/tcp vigilant_swirles
[root@dc01 ~]#

6 网页访问nginx


# docker  # nginx  # 要有  # 镜像  # 配置文件  # 端口映射  # http_user_agent  # http_referer  # http_x_forwarded_for  # sendfile  # tcp_nopush  # access_log 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  Laravel如何生成和使用数据填充?(Seeder和Factory示例)  网站建设要注意的标准 促进网站用户好感度!  Android自定义listview布局实现上拉加载下拉刷新功能  EditPlus中的正则表达式实战(5)  Laravel如何编写单元测试和功能测试?(PHPUnit示例)  如何用美橙互联一键搭建多站合一网站?  如何做网站制作流程,*游戏网站怎么搭建?  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  如何快速打造个性化非模板自助建站?  Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】  安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出  如何在橙子建站中快速调整背景颜色?  Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程  如何在阿里云域名上完成建站全流程?  音乐网站服务器如何优化API响应速度?  HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】  企业网站制作这些问题要关注  重庆市网站制作公司,重庆招聘网站哪个好?  如何用搬瓦工VPS快速搭建个人网站?  绝密ChatGPT指令:手把手教你生成HR无法拒绝的求职信  javascript中对象的定义、使用以及对象和原型链操作小结  如何在宝塔面板创建新站点?  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  Laravel如何自定义错误页面(404, 500)?(代码示例)  瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口  微信公众帐号开发教程之图文消息全攻略  JavaScript如何实现错误处理_try...catch如何捕获异常?  Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】  微信小程序 HTTPS报错整理常见问题及解决方案  米侠浏览器网页背景异常怎么办 米侠显示修复  Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道  制作旅游网站html,怎样注册旅游网站?  Laravel distinct去重查询_Laravel Eloquent去重方法  手机网站制作与建设方案,手机网站如何建设?  使用spring连接及操作mongodb3.0实例  如何用免费手机建站系统零基础打造专业网站?  网站建设整体流程解析,建站其实很容易!  php在windows下怎么调试_phpwindows环境调试操作说明【操作】  Windows10如何更改计算机工作组_Win10系统属性修改Workgroup  如何用IIS7快速搭建并优化网站站点?  如何选择PHP开源工具快速搭建网站?  Laravel怎么清理缓存_Laravel optimize clear命令详解  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  如何快速生成专业多端适配建站电话?  浅谈javascript alert和confirm的美化  如何用景安虚拟主机手机版绑定域名建站?  Laravel Eloquent性能优化技巧_Laravel N+1查询问题解决  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】