Windows下docker中的应用程序或者数据库暴露到局域网中
- 你是不是在Windows环境下docker中的应用或者数据无法暴露给同事连接或者访问呢
- 因为Windows中安装docker都是在虚拟机或者wsl2中安装所以无法暴露到局域网
- 我们可以在Windows中安装nginx采用反向代理解决这个难题
- 容器ip要是变动的话,你配置Windows hosts然后就反向代理域名
安装nginx
- 官网地址:http://nginx.org/en/download.html
- 安装稳定版本
- 下载之后你找个文件路径去解压例如:D:\app
安装之后你配置一下nginx.conf
你到conf目录下分别创建两个文件夹
vhosts
streams
主要配置 include 下的东西
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include D:/app/nginx-1.24.0/conf/vhosts/*.conf;
}
stream {
include D:/app/nginx-1.24.0/conf/streams/*.conf;
}
vhosts/xxx_api.conf
server{
listen 8088;
server_name 0.0.0.0;
index index.php index.html index.htm;
location / {
proxy_pass http://127.0.0.1:8088; # 转发规则
proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
streams/mysql.conf
upstream mysql {
# localhost 可修改为对应的 IP 地址
# 3306 可修改为对应的数据库端口
# weight 权重
server myhao.com:3307 weight=1 max_fails=3 fail_timeout=30s;
}
server {
# 监听的端口
listen 3307;
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass mysql;
}
然后使用nginx命令检查一下
nginx -t
设置nginx自启动
- 你不需要自启动你可以不用管
- 我是用winws设置的
- 下载地址:https://github.com/winsw/winsw/releases
- 将下载后的winws.exe文件放到nginx目录并修改文件名为nginx-service.exe
- 在nginx安装目录下新建一个ngingx-service.xml文件
你自己改一下logpath、executabl、stopexecutable路径
<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<logpath>D:\app\nginx-1.24.0</logpath>
<logmode>roll</logmode>
<depend></depend>
<executable>D:\app\nginx-1.24.0\nginx.exe</executable>
<stopexecutable>D:\app\nginx-1.24.0\nginx.exe -s stop</stopexecutable>
</service>
- 打开CMD执行命令
nginx-service.exe install
sc start nginx
这样服务就启动成功啦,你可以到WIN+R输入去查看服务:services.msc