Docker如何运行nginx并挂载本地目录到镜像中

发布时间 - 2023-05-15 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  # default  # vim  # 要有  # 镜像  # 配置文件  # 端口映射  # img  # http_referer  # src  # oss  # cn  # public 


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


相关推荐: Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口  javascript如何操作浏览器历史记录_怎样实现无刷新导航  魔方云NAT建站如何实现端口转发?  Python文件操作最佳实践_稳定性说明【指导】  Laravel中的withCount方法怎么高效统计关联模型数量  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭  Linux系统运维自动化项目教程_Ansible批量管理实战  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  如何在云虚拟主机上快速搭建个人网站?  JavaScript如何实现音频处理_Web Audio API如何工作?  东莞市网站制作公司有哪些,东莞找工作用什么网站好?  教学论文网站制作软件有哪些,写论文用什么软件 ?  北京网站制作公司哪家好一点,北京租房网站有哪些?  bing浏览器学术搜索入口_bing学术文献检索地址  Laravel集合Collection怎么用_Laravel集合常用函数详解  简单实现Android验证码  如何在阿里云购买域名并搭建网站?  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  Laravel如何使用Guzzle调用外部接口_Laravel发起HTTP请求与JSON数据解析【详解】  高性价比服务器租赁——企业级配置与24小时运维服务  米侠浏览器网页背景异常怎么办 米侠显示修复  Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置  如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)  如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】  Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程  如何用5美元大硬盘VPS安全高效搭建个人网站?  网站制作软件有哪些,制图软件有哪些?  Swift中循环语句中的转移语句 break 和 continue  如何在Ubuntu系统下快速搭建WordPress个人网站?  西安专业网站制作公司有哪些,陕西省建行官方网站?  Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践  如何快速搭建高效服务器建站系统?  制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?  活动邀请函制作网站有哪些,活动邀请函文案?  如何在云指建站中生成FTP站点?  详解Android图表 MPAndroidChart折线图  怎么用AI帮你为初创公司进行市场定位分析?  ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】  高防服务器如何保障网站安全无虞?  如何注册花生壳免费域名并搭建个人网站?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  *服务器网站为何频现安全漏洞?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何在阿里云ECS服务器部署织梦CMS网站?  nodejs redis 发布订阅机制封装实现方法及实例代码  Laravel用户密码怎么加密_Laravel Hash门面使用教程  如何在Windows环境下新建FTP站点并设置权限?  香港网站服务器数量如何影响SEO优化效果?