Example To Connect From Container To Host Service
Solution 1:
The endpoint you're looking for is 'http://host.docker.internal'
.
Running on MacOs. I will run a service on my macbook just using basic flask on a python:3.6 container with app.py in the root directory:
docker run -it -p 5000:5000 python:3.6 bash
pip install flask
python app.py
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=["GET"])
def main():
return {'a': 1, 'b': 2}
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)
Then just running in another container
import requests
r = requests.get('http://host.docker.internal:5000')
r.json()
{'a': 1, 'b': 2}
Solution 2:
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)
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.
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
It is possible from Linux based on what I read: Docker - a way to give access to a host USB or serial device?
Post a Comment for "Example To Connect From Container To Host Service"