从容器连接到主机服务的示例 [英] Example to connect from container to host service

查看:107
本文介绍了从容器连接到主机服务的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker和Drone编程的新手.我能够在Windows 10上将python脚本(包含dronekit代码)部署到docker容器.要运行该脚本,我需要连接到主机上的服务.我在下面提供了一个代码段,Windows运行了一个程序(Mavproxy SITL),该程序暴露了UDP的127.0.0.1:14550.我的图片应该连接到该地址.

I am new to Docker and Drone Programming. I was able to deploy a python script (that contains dronekit code) to docker container on my Windows 10. To run the script, I need to connect to a service on my host. I have provided a snippet below, Windows has a program running(Mavproxy SITL) which has exposed 127.0.0.1:14550 which is UDP. My image should connect to this address.

mydronectrlscript.py:

from dronekit import connect

# Connect to UDP endpoint.
vehicle = connect(‘udp:127.0.0.1:14550’, wait_ready=True)
# Use returned Vehicle object to query device state - e.g. to get the mode:
print("Mode: %s" % vehicle.mode.name)

我阅读了有关host.docker.internal的文档和响应: https://docs.docker.com/docker-for-windows/networking/ 如何从docker容器访问主机端口

I read documents and responses about host.docker.internal: https://docs.docker.com/docker-for-windows/networking/ How to access host port from docker container

响应类似的问题状态,在Windows/Mac上使用host.docker.internal(版本18.03 +).

Responses to similar question states to use host.docker.internal on Windows/Mac for version 18.03+.

我的问题是如何使用" host.docker.internal?它在docker run命令中传递了吗?能否请您分享一个使用方法的例子? 使用host.docker.internal允许py脚本访问主机的UDP 127.0.0.1:14550地址吗?

My questions is "how to use" host.docker.internal? Is it passed in the docker run command? Can you please share me an example of how is it used? Will the use of host.docker.internal allow py script to access host’s UDP 127.0.0.1:14550 address ?

推荐答案

简单来说,对我的问题的回答是:

Simply, response to my question is:

mydronectrlscript.py:

from dronekit import connect
# Connect to UDP endpoint.
vehicle = connect(‘udp:host.docker.internal:14550’, wait_ready=True)
# Use returned Vehicle object to query device state - e.g. to get the mode:
print("Mode: %s" % vehicle.mode.name)

此外,如果您使用的是Windows 10 Home Edition或需要虚拟盒子的OS版本,从我的尝试来看,这将无法正常工作.这适用于Windows 10专业版和Mac OS.

Also, from what I tried this does not work if you are using Windows 10 Home edition or a version of OS that needs virtual box. This worked on Windows 10 Professional and Mac OS.

由于此问题与无人机编程有关: 如果您最终尝试访问COM端口(以进行遥测),则目前无法使用Windows OS中托管的Docker映像:

Since this question is related to drone programming: If you are eventually trying to access COM ports (for telemetry), it is not possible at the moment with Docker image hosted in Windows OS: https://github.com/docker/for-win/issues/1018

根据我阅读的内容,可以从Linux中获取:

It is possible from Linux based on what I read: Docker - a way to give access to a host USB or serial device?

这篇关于从容器连接到主机服务的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆