简体   繁体   English

如何让docker容器使用主机网络?

[英]How to make docker container to use host network?

I am running docker for mac.我正在运行 docker for mac。 My docker compose configuration file is:我的 docker compose 配置文件是:

version: "2.3"
services:
  base:
    build:
      context: .

  dev:
    network_mode: "host"
    extends:
      service: base

when the container is launched via docker-compose run --rm dev sh , it can't ping a IP address (172.25.36.32).当容器通过docker-compose run --rm dev sh ,它无法 ping 一个 IP 地址 (172.25.36.32)。 But I can ping this address from host.但是我可以从主机 ping 这个地址。 I have set network_mode: "host" on the configuration file.我在配置文件上设置了network_mode: "host" How can I make the docker container share host network?如何让docker容器共享主机网络?

I found that host network doesn't work for Mac.我发现host网络不适用于 Mac。 Is there a solution for that in Mac? Mac 中是否有解决方案?

Below is the docker network inspect ID output:以下是docker network inspect ID输出:

[
    {
        "Name": "my_container_default",
        "Id": "0441cf2b99b692d2047ded88d29a470e2622a1669a7bfce96804b50d609dc3b0",
        "Created": "2019-08-27T06:06:30.984427063Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "22d3e7500ccfdc7fcd192a9f5977ef32e086e340908b1c0ff007e4144cc91f2e": {
                "Name": "time-series-api_dev_run_b35174fdf692",
                "EndpointID": "23924b4f68570bc99e01768db53a083533092208a3c8c92b20152c7d2fefe8ce",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "time-series-api",
            "com.docker.compose.version": "1.24.1"
        }
    }
]

I think you need to start the container with up option not run since run override many options:我认为您需要使用up选项不run启动container ,因为运行覆盖了许多选项:

docker-compose up dev

or you may try with --use-aliases with run或者您可以尝试使用--use-aliases run

--use-aliases Use the service's network aliases in the network(s) the container connects to. --use-aliases 在容器连接的网络中使用服务的网络别名。

see this看到这个

PS After your update PS更新后

the following will work on MAC以下将适用于MAC

dev:

  network: host
  extends:
    service: base

i believe you need to add network option during the build .我相信您需要在build过程中添加network选项。 Try with试试

version: "2.3"
services:
  base:
    build:
      context: .
      network: host
  dev:
    network_mode: "host"
    extends:
      service: base

EDIT : Works on Linux, please see documentation for Mac编辑:适用于 Linux,请参阅Mac文档

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.主机网络驱动程序仅适用于 Linux 主机,在 Docker Desktop for Mac、Docker Desktop for Windows 或 Docker EE for Windows Server 上不受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM