简体   繁体   English

将 docker 容器暴露给外部服务

[英]Exposing docker container to outside service

This is not dockerized version: currently I am having service that is using two addresses to communicate with outside world.这不是 dockerized 版本:目前我有一个使用两个地址与外界通信的服务。 First address is a tcp 0.0.0.0:5000 and this is web service so when I just type http://localhost:5000 or any other address it works.第一个地址是一个 tcp 0.0.0.0:5000 ,这是网络服务,所以当我只输入http://localhost:5000或任何其他地址时,它就可以工作。 Other is an udp service that binds to address as listener and send messages to outside service for example:另一个是 udp 服务,它绑定到作为侦听器的地址并将消息发送到外部服务,例如:

UDP client receives messages on ip endpoints 0.0.0.0:8000 and sends them on for example localhost:8001 (there is a service on host that receives and resend messages) UDP 客户端在 ip 端点0.0.0.0:8000上接收消息并在例如 localhost:8001 上发送它们(主机上有一个接收和重新发送消息的服务)

So it works without a docker.所以它可以在没有 docker 的情况下工作。

Now a problem, I have turn it to docker image and component but have a problem to send messages through udp to service on docker host.现在有一个问题,我已将其转换为 docker 映像和组件,但在通过 udp 向 docker 主机上的服务发送消息时遇到问题。

So I tried redirecting port in docker compose using:所以我尝试使用以下命令在 docker compose 中重定向端口:

    version: '3.9'

        services:
          app:
            build: 
              context: ../../
              dockerfile: /app/_docker/Dockerfile
            container_name: app
            restart: always
            ports:
               - 5000:5000 #for web 
               - 8000:8000 #for udp receiver
               - 8001:8001 #for udp sender

Now web works (I have checked) and UDP receiver probably works because it binds to broadcast address, but I am unable to send message to outside host (UDP receiver on host is also binded to 0.0.0.0:8001 and sending message to remote ip - that means it should be ip of my docker image).现在网络工作(我已经检查过)并且 UDP 接收器可能工作,因为它绑定到广播地址,但我无法向外部主机发送消息(主机上的 UDP 接收器也绑定到0.0.0.0:8001并将消息发送到远程 ip - 这意味着它应该是我的 docker 镜像的 ip)。

I tired also:我也累了:

    services:
      app:
        build: 
          context: ../../
          dockerfile: /app/_docker/Dockerfile
        container_name: app
        restart: always
        network_mode: "host"

But now nothing works (I type localhost:5000 and nothing).但现在没有任何效果(我输入localhost:5000什么也没有)。

So is that type of communication is possible and if yes what am I doing wrong?那么这种类型的交流是可能的,如果是的话,我做错了什么?

Also I am using windows but this will be deployed on linux as well, and can't dockerize service on host that response to my UDP requests.此外,我正在使用 Windows,但这也将部署在 linux 上,并且无法在响应我的 UDP 请求的主机上 dockerize 服务。

EDIT:编辑:

Maybyu I have changed port forwarding to look like: Maybyu 我已将端口转发更改为如下所示:

   - 5000:5000
   - 50005:50005/udp
   - 50000:50000/udp

And I noticed if I type http://172.21.232.161:5000/ (as 172.21.232.161 is my HyperV interface address or docker0 in case of linux) it works for web我注意到如果我输入http://172.21.232.161:5000/ (因为 172.21.232.161 是我的 HyperV 接口地址或 docker0 在 linux 的情况下)它适用于网络

I have inspected my docker container for gateway and container ip and it looks like:我已经检查了我的 docker 容器的网关和容器 ip,它看起来像:

"Gateway": "172.31.0.1",
"IPAddress": "172.31.0.4"

So I may expect when bind UDP receiver on 172.21.232.161:8000 on host computer and send frame from docker to 172.31.0.1:8000 on UDP it should work.所以我可能期望当在主机上的 172.21.232.161:8000 上绑定 UDP 接收器并将帧从 docker 发送到 UDP 上的 172.31.0.1:8000 时,它应该可以工作。 But it does not.但事实并非如此。 So what should I put to sending endpoint (172.31.0.1:8000) to receive message on host?那么我应该把什么放在发送端点(172.31.0.1:8000)上以在主机上接收消息?

经过多次战斗,答案很简单..如果我的 Windows 主机有外部 IP,例如 10.0.0.4 容器将在 10.0.0.4:8000(或 host.docker.internal)上发送消息并在 0.0.0.0:8001 上接收消息

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

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