You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Docker与WSL环境中实现Windows与Linux容器互通的问题求助

解决WSL Linux容器与Windows容器通信问题的思路

方案1:使用Windows主机物理网卡IP通信

  • 通过ipconfig查看Windows主机物理网卡(以太网/Wi-Fi)的IPv4地址,比如192.168.1.100
  • 启动Windows容器时,将端口映射到该物理IP,避免仅绑定localhost:
    docker run -p 192.168.1.100:8080:8080 your-windows-image
    
  • WSL内的Linux容器直接用这个物理IP+端口访问Windows容器服务

方案2:配置跨平台共享物理网络

给Windows容器创建透明网络

  • 在Windows主机上创建透明模式Docker网络,让容器直接接入物理网段:
    docker network create -d transparent --subnet=192.168.1.0/24 --gateway=192.168.1.1 win-transparent-net
    
  • 启动Windows容器时加入该网络并指定固定IP:
    docker run --network win-transparent-net --ip 192.168.1.101 your-windows-image
    

给WSL Linux容器配置Macvlan网络

  • 在WSL中创建Macvlan网络,让Linux容器获取物理网段IP:
    docker network create -d macvlan \
      --subnet=192.168.1.0/24 \
      --gateway=192.168.1.1 \
      --parent eth0 linux-macvlan-net
    
  • 启动Linux容器时加入该网络并指定固定IP:
    docker run --network linux-macvlan-net --ip 192.168.1.102 your-linux-image
    
  • 此时两类容器处于同一物理网段,可直接通过各自IP通信

方案3:检查Windows容器端口绑定范围

  • 确保Windows容器的端口映射绑定到所有网卡(0.0.0.0),而非仅localhost:
    docker run -p 0.0.0.0:8080:8080 your-windows-image
    
  • 同时检查容器内部服务的监听配置,确认服务绑定到0.0.0.0,而非仅容器内的localhost

方案4:验证WSL网络镜像模式配置

  • 确认%USERPROFILE%\.wslconfig中的配置正确:
    [wsl2]
    networkingMode=mirrored
    experimental.hostAddressLoopback=true
    
  • 重启WSL服务使配置生效:
    wsl --shutdown
    wsl
    
  • 在WSL内ping Windows主机物理IP,确认基础网络连通性

补充排查步骤

  • 在WSL中用tcpdump抓包,验证请求是否发送到Windows主机:
    sudo tcpdump -i any port 8080
    
  • 在Windows主机执行netstat -ano,检查端口是否被正确监听(确认监听地址包含物理IP或0.0.0.0)

内容的提问来源于stack exchange,提问作者Diogo Ribeiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 02:32:37