2

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)

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

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

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 ?

San123
  • 86
  • 1
  • 7
  • So connecting with `localhost` will connect you to the *container's* localhost, not the host machine, assuming I've understood that this script is inside the container and the other service is running elsewhere – C.Nivs Jul 18 '19 at 15:37
  • [This article](https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host) and answers [here](https://stackoverflow.com/a/24326540/7867968) might be helpful – C.Nivs Jul 18 '19 at 15:42
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – C.Nivs Jul 18 '19 at 15:43
  • "connecting with localhost will connect you to the container's localhost, not the host machine" -> I am not trying to achieve this. Script's connect statement from container connect(‘127.0.0.1:14550’) should connect with host's ‘127.0.0.1:14550’. YOur undesrtanding "I've understood that this script is inside the container and the other service is running elsewhere" is correct with "running elsewhere" mentioned as "running on host". – San123 Jul 18 '19 at 15:46
  • Thanks for the links. I did read them, my question is more of how to do this: "just connect to your mysql service using the host host.docker.internal." – San123 Jul 18 '19 at 15:59

2 Answers2

3

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}
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • Got it, just replace ip address with host.docker.internal. This is what I was looking for. Will try and update this evening. – San123 Jul 18 '19 at 19:03
  • Yep, like I said, the `127.0.0.1` in the container does not map back to the host machine, which is why you go about it this way. The documentation for which is [here](https://docs.docker.com/docker-for-windows/networking/) – C.Nivs Jul 18 '19 at 19:45
1

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?

San123
  • 86
  • 1
  • 7