chipmk / docker-mac-net-connect

Connect directly to Docker-for-Mac containers via IP address 🐳 💻
MIT License
428 stars 45 forks source link

Can this tool help me connect port when docker run use --net="host" #8

Open 631068264 opened 2 years ago

gregnr commented 2 years ago

Hi @631068264, I assume you are trying to use --net=host so that your container listens on a port from your macOS host instead of the Linux VM host (correct me if I'm wrong).

Currently this package does not solve that, although I can think of some options to make that happen (ie. monitor /proc/net/tcp and iptables on Linux VM and add port forwarding NAT rules on macOS using pfctl). Let me know if you think this would be useful.

In the mean time, you can replace Docker Desktop with Rancher Desktop (using dockerd runtime) which supports --net=host macOS host binding out of the box (via lima). I've been using Rancher Desktop as a drop-in replacement for Docker Desktop for about a month now without issues.

631068264 commented 2 years ago

Thanks for your reply.

I just wanted to find a solution or a tool help me, when use --net=host on OSX can work like in linux. Because I always test my application on local.

I'm not good at these(docker network on OSX, etc) so I'm sorry that I can' tell you sth more valuable. I even don't know how to connect the Linux VM on OSX.

gregnr commented 2 years ago

Thanks for clarifying. If you are not tied to the official Docker Desktop, I would give something like Rancher Desktop a try as it supports what you are trying to do.

I'll keep this feature in mind for this package though and let you know if it's gets added in the future.

alexandertsukanov commented 2 years ago

BTW a very nice feature to have, I have tried to map the docker container somehow to my local machine 127.0.0.1, but don't have success with that.

dro-ex commented 1 year ago

This is exactly what I am looking for as well - if there is a way to do this it would be great. I am specifically running into the issue where the discoverable IoTs are on the host network and home assistant cannot auto discover devices and some devices do not report back due to lack of host networking in docker desktop for mac.

gregnr commented 1 year ago

@alexandertsukanov @dro-ex Thanks for the comments.

@dro-ex That's a neat use case - unfortunately I don't think this tool will solve your problem. Assuming Home Assistant uses mDNS to discover devices, mDNS packets can't traverse layer 3 network boundaries, at least not without an mDNS reflector. I haven't been able to find any macOS-based mDNS reflectors yet unfortunately.

FWIW, Rancher Desktop creates a layer 2 bridged network between macOS and the container VM (compared to a layer 3 network on Docker Desktop), which means protocols like mDNS are supported. This means that all you need to do is attach your container to a host or macvlan network to get access to your physical layer 2 LAN network.

I just tried this using --net=host and avahi-browser and successfully got a list of mDNS devices running on my LAN network from inside the container:

$ docker run --net=host hsfeng/avahi-browse --all
...
dro-ex commented 1 year ago

Unless I am overlooking something (amd I am sure I am), the above is not working with home assistant:

nerdctl run -d \         
  --name homeassistant \
  --privileged \
  -e TZ=TZ=America/Los_Angeles \
  -v /Users/test/homeassistant/config:/config \
  --net=host \    
  ghcr.io/home-assistant/home-assistant:stable

I am still getting "no devices on network"

gregnr commented 1 year ago

@dro-ex I've never used Home Assistant before, but just gave it a quick try here. For me no devices were discovered at first, but then I realized that the default network interface picked by Home Assistant for discovery was wrong under Settings > System > Network. For me the correct interface for my LAN was rd0 (it had picked eth0). After a restart all my devices started showing up.

Let me know if that helps.

dro-ex commented 1 year ago

It's very strange - I am not getting any network settings in home assistant using Rancher Desktop - just a blank page - no errors. It is working but no -net=hose access. I've tried both nerdctl and docker as engines. I am going to try some different things and see if I can the network settings to show up.

gregnr commented 1 year ago

@dro-ex I had to set my profile to "Advanced Mode" to see the Network Settings. See: https://www.home-assistant.io/integrations/network/

Was also getting a blank screen without that enabled.

dro-ex commented 1 year ago

Thank you! It is indeed discovering local IoT devices - this is such good news. Thank you! I am now trying to learn what I need to to ditch portainer and docker desktop and recreate all the containers in rancher. Thank you again!