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

Windows下Docker Compose如何实现Linux中host网络模式的等效效果?

Windows Docker Desktop 实现 localhost: 访问容器的方法

不用被迫使用随机IP,有两种可靠方式实现和Linux上类似的localhost:<port>访问体验:

1. 端口映射(推荐)

删除network_mode: "host"配置,在docker-compose.yml中为容器添加端口映射规则,把容器端口绑定到宿主机的localhost端口:

services:
  demo-service:
    image: your-image
    ports:
      - "8080:8080" # 宿主机localhost:8080 对应容器内8080端口

配置完成后,直接通过localhost:8080就能访问容器服务。

2. 利用host.docker.internal(适配容器与宿主机互访场景)

如果需要容器内部访问宿主机服务,同时宿主机用localhost:<port>访问容器:

  • 宿主机访问容器:依然用端口映射的方式,通过localhost:<port>访问
  • 容器访问宿主机:不用写宿主机IP,直接用host.docker.internal:<port>替代localhost:<port>

关键提醒

  • Windows Docker Desktop完全不支持network_mode: "host",必须移除该配置才能正常使用上述方法
  • 端口映射时要确保宿主机的目标端口未被其他程序占用,否则容器启动会报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 23:25:16