Docker中安装Nginx并发布网站
1.安装Nginx
命令:
docker pull nginx:latest
查看镜像:
2.运行Nginx
命令:
docker run --name nginx-app -p 8080:80 -d nginx
- --name nginx-test:容器名称。
- -p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
- -d nginx: 设置容器在在后台一直运行。
在宿主机器上测试:
3.修改Nginx文件和配置
3.1 查看Nginx containerid
命令:docker ps
3.2 进入容器
命令:docker exec -it f4881659581a /bin/bash
进入html文件夹子:cd /usr/share/nginx/html
查看文件加文件:ls
写一句话到index.html中
echo "hello,Nginx,I am coming" > index.html
宿主机器测试:
3.3 修改挂载文件路径
docker run --name mynginx_V1 -p 8888:80 -v d:\html:/usr/share/nginx/html nginx
我这里在宿主机器的D盘生成了html文件。路径可根据自己的修改。
将容器的80端口对外暴露为8888端口。
Docker客户端查看:
在宿主机器上测试:
3.4修改网站的连接的后端接口
此时后台api和前端vue没有连接上,需要修改nginx配置才能访问数据。
nginx.conf配置文件在 /etc/nginx/
这样修改不是很方便,可以将宿主机器中的配置文件挂载到docker中。
docker run --name mynginx_V1 -p 8888:80 -v d:\nginx.conf:/etc/nginx/nginx.conf -d nginx
nginx.cof配置文件:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 80; server_name localhost; #root C:/web发布/dist/; location / { #root C:/web发布/dist/; root /usr/share/nginx/html; index index.html index.htm; } location @router { #root C:/web发布/dist/; rewrite ^.*$ /index.html last; } location ^~/api/ { #root C:/web发布/dist/; proxy_set_header Host $host; proxy_set_header x-forwarded-for $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://172.17.0.4:7004/; #实际的接口地址 } } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
这里需要注意配置中的html路径
后台接口ip要使用Docker中的jar ip
监听的端口也是Docker中的Nginx的端口
综上可以使用一个命令执行挂载 html和配置
docker run --name mynginx_V1 -p 8888:80 -v d:\html:/usr/share/nginx/html -v d:\nginx.conf:/etc/nginx/nginx.conf -d nginx
宿主机器访问网站并测试登录
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!