文章目录
前言一、在Docker下载Nginx镜像二、复制默认配置文件到宿主机1.创建挂载目录2.复制默认配置文件到宿主机三、启动挂载命令1、删除关闭容器2、启动容器、挂载目录Nginx配置文件最终展示前言
挂载宿主机的Nginx配置文件,修改之后,重启docker即可。
一、在Docker下载Nginx镜像
docker pull nginx
二、复制默认配置文件到宿主机
1.创建挂载目录
在宿主机批量创建文件夹
mkdir -p /home/nginx/{conf,html,logs}
2.复制默认配置文件到宿主机
docker cp nginx:/etc/nginx/nginx.conf /home/nginx/nginx.confdocker cp nginx:/etc/nginx/conf.d/default.conf /home/nginx/conf/default.conf
编写一个index.html放到宿主机/home/nginx/html下
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><p>测试docker挂载nginx配置文件</p></body></html>
三、启动挂载命令
1、删除关闭容器
docker stop nginxdocker rm nginx
2、启动容器、挂载目录
docker run -d --network host --name nginx -p 80:80 -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/logs:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/conf:/etc/nginx/conf.d --privileged=true nginx
Nginx配置文件
worker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost;location / {root /usr/share/nginx/html; #指定容器中的路径index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html; #指定容器中的路径}}#include /etc/nginx/conf.d/*.conf;#优先加载自定义配置文件}
最终展示
访问宿主机80端口
如果觉得《docker自学系列:docker挂载Nginx配置文件》对你有帮助,请点赞、收藏,并留下你的观点哦!