首先拉取镜像 默认是最新版本
docker pull nginx:latest

创建挂载文件目录
mkdir -p /data/nginx/files
mkdir /data/nginx/conf.d

启动容器
docker run --name nginx -p 8090:80 -d nginx

把配置文件复制出来,删除容器(此处可以先按照下一步准备好配置文件然后放到映射目录conf.d文件夹下再直接启动容器)

  • 01
docker cp 容器ID:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf

docker rm -f nginx

修改容器配置文件
注意需要把此配置文件中上面已有的内容全部注释掉!(非常重要否则无法启动容器)

  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
autoindex on; # 显示目录 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # 显示文件时间 server { listen 80; # 监听端口 listen [::]:80; server_name localhost; # 地址 charset utf-8; # 中文名的文件不乱码 location / { # 访问首页路径 root /home/files/; # 根目录 index index.html index.htm; # 默认文件 add_after_body /home/files/autoindex.html; # 这个文件可以美化页面,可不加 } error_page 500 504 /50x.html; # 当出现以上状态码时从新定义到50x.html location = /50x.html { # 当访问50x.html时 root /home/www/html; # 50x.html 页面所在位置 } }

要隐藏只需要在文件名前加“.”
.autoindex.html
重新创建容器
docker run --name nginx_files -p 8090:80 -v /data/nginx/files:/home/files -v /data/nginx/conf.d/:/etc/nginx/conf.d/ -d nginx