Windows下docker中的应用程序或者数据库暴露到局域网中

在Windows环境的Docker中,通过Nginx进行反向代理,可以将应用程序或数据库暴露给局域网中的其他用户。文章详细介绍了如何安装Nginx,配置vhosts和streams文件,以及设置Nginx自启动,以便于在容器IP变化时仍能通过固定域名访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows下docker中的应用程序或者数据库暴露到局域网中

  1. 你是不是在Windows环境下docker中的应用或者数据无法暴露给同事连接或者访问呢
  2. 因为Windows中安装docker都是在虚拟机或者wsl2中安装所以无法暴露到局域网
  3. 我们可以在Windows中安装nginx采用反向代理解决这个难题
  4. 容器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
  1. 将下载后的winws.exe文件放到nginx目录并修改文件名为nginx-service.exe 
    在这里插入图片描述
  2. 在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>
  1. 打开CMD执行命令
nginx-service.exe install
sc start nginx

在这里插入图片描述
这样服务就启动成功啦,你可以到WIN+R输入去查看服务:services.msc
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值