简体   繁体   English

Docker - 访问主机网络和 docker 网络

[英]Docker - access to host network and docker network

I have a docker container and I want to give it --network=host AND --network=postgres .我有一个 docker 容器,我想给它--network=host AND --network=postgres I need to connect to the host network in order to use the host datadog server (UDP) and the postgres network for its database.我需要连接到主机网络才能将主机数据狗服务器 (UDP) 和postgres网络用于其数据库。

Trying to add both network results in docker: conflicting options: cannot attach both user-defined and non-user-defined network-modes.尝试在docker: conflicting options: cannot attach both user-defined and non-user-defined network-modes. . .

Any idea what's the correct way to handle this?知道处理这个问题的正确方法是什么吗?

You could use the the IP address of the host machine from within you container in order to send requests to the host machine.您可以使用容器内的主机 IP 地址向主机发送请求。

A better solution, however, would be to use --add-host=host.docker.internal:host-gateway or但是,更好的解决方案是使用--add-host=host.docker.internal:host-gateway

extra_hosts:
    - host.docker.internal:host-gateway

where host.docker.internal is then the host name of the host network inside your docker container.其中host.docker.internal是 docker 容器内的主机网络的主机名。 This name can of course be changed to your liking.此名称当然可以根据您的喜好更改。

Credit: https://medium.com/@TimvanBaarsen/how-to-connect-to-the-docker-host-from-inside-a-docker-container-112b4c71bc66信用: https ://medium.com/@TimvanBaarsen/how-to-connect-to-the-docker-host-from-inside-a-docker-container-112b4c71bc66

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

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