25

I am trying to access my host system from a docker container

have tried all the following instead of 127.0.0.1 and localhost:

gateway.docker.internal, docker.for.mac.host.internal, host.docker.internal , docker.for.mac.host.internal, docker.for.mac.localhost,

but none seem to work.

If I run my docker run command with --net=host, I can indeed access localhost however none of my port mappings get exposed and in accessible from outside docker.

I am using Docker version 20.10.5, build 55c4c88

some more info. I am running a piece of software called impervious (a layer on top of the bitcoin lightning network). It needs to connect to my local Polar lightning node on localhost:10001. Here is the config file the tool itself uses(see lnd section):

# Server configurations
server:
  enabled: true # enable the GRPC/HTTP/websocket server
  grpc_addr: 0.0.0.0:8881 # SET FOR DOCKER
  http_addr: 0.0.0.0:8882 # SET FOR DOCKER
# Redis DB configurations
sqlite3:
  username: admin
  password: supersecretpassword # this will get moved to environment variable or generated dynamically
###### DO NOT EDIT THE BELOW SECTION#####
# Services
service_list:
  - service_type: federate
    active: true
    custom_record_number: 100000
    additional_service_data:
  - service_type: vpn
    active: true
    custom_record_number: 200000
    additional_service_data:
  - service_type: message
    active: true
    custom_record_number: 400000
    additional_service_data:
  - service_type: socket
    active: true
    custom_record_number: 500000
    additional_service_data:
  - service_type: sign
    active: true
    custom_record_number: 800000
    additional_service_data:
###### DO NOT EDIT THE ABOVE SECTION#####

# Lightning
lightning:
  lnd_node:
    ip: host.docker.internal 
    port: 10001 #GRPC port of your LND node
    pub_key: 025287d7d6b3ffcfb0a7695b1989ec9a8dcc79688797ac05f886a0a352a43959ce #get your LND pubkey with "lncli getinfo"
    tls_cert: /app/lnd/tls.cert # SET FOR DOCKER
    admin_macaroon: /app/lnd/admin.macaroon # SET FOR DOCKER
federate:
  ttl: 31560000 #Federation auto delete in seconds
  imp_id: YOUR_IMP_ID #plain text string of your IMP node name
vpn:
  price: 100 #per hour
  server_ip: http://host.docker.internal #public IP of your VPN server
  server_port: 51820 #port you want to listen on
  subnet: 10.0.0.0/24 #subnet you want to give to your clients. .1 == your server IP.
  server_pub_key: asdfasdfasdf #get this from your WG public key file
  allowed_ips: 0.0.0.0/0 #what subnets clients can reach. Default is entire world.
  binary_path: /usr/bin/wg #where your installed the "wg" command.
  dns: 8.8.8.8 #set your preferred DNS server here.
socket:
  server_ip: 1.1.1.1 #public IP of your socket server

I run impervious using the following docker comand:

docker run -p8881:8881 -p8882:8882  -v /Users/xxx/dev/btc/impervious/config/alice-config-docker.yml:/app/config/config.yml -v /Users/xxx/.polar/networks/1/volumes/lnd/alice/tls.cert:/app/lnd/tls.cert -v /Users/xxx/.polar/networks/1/volumes/lnd/alice/data/chain/bitcoin/regtest/admin.macaroon:/app/lnd/admin.macaroon  -it impant/imp-releases:v0.1.4

but it just hangs when it tries to connect to the node at host.docker.internal

3
  • can you explain your question a bit more? Commented Sep 11, 2021 at 13:03
  • 1
    Can you provide a minimal reproducible example showing the connection you're trying to make, and what the actual error you're getting? Host networking doesn't work the way you expect on MacOS (and isn't a best practice in general). Commented Sep 11, 2021 at 13:44
  • added some more info @kishore Commented Sep 13, 2021 at 10:06

5 Answers 5

14

You can use host.docker.internal which gives the localhost of the macos.

https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host

The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.

Sign up to request clarification or add additional context in comments.

1 Comment

localhost host.docker.internal based on /etc/hosts file would mean, that container would see it as his own localhost or am I wrong? Or what is the logic behind it? Container network might use host machine network, but it is not one and the same, afaik.
8

Host networking is also supported on Docker Desktop version 4.29 and later for Mac, Windows, and Linux as a beta feature. To enable this feature, navigate to the Features in development tab in Settings, and then select Enable host networking.

The reference is here

5 Comments

This certainly looks promising, but I couldn't get it working. I enabled the feature, restarted docker, then started container with docker run --rm -it --net=host node:20 /bin/bash and inside, attempted curl http://localhost:5173 with "Failed to connect" as the result. The same curl works directly on my mac.
Actually, the problem was resolved on the server side, by running vite with the --host option. Not sure exactly what that does, but it seems to make the server addressable by IP, instead of just localhost. So, anyway, this works great!
Exactly, If you use vite then you have to activate the host on vite.config.js file like this: server: {host: true, port: 5173, open: true}
The "host networking" feature is now general available with Docker Desktop version 4.34 and later
The "host networking" feature seems to be disabled by default. It is located here: Docker Desktop (v 4.36) -> Settings -> Resources -> Network -> Host Networking
3

Have you tried docker-mac-net-connect?

The problem is related to macOS.Unlike Docker on Linux, Docker for macOS does not expose container networks directly on the macOS host.

6 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Does it work anymore ?
@codeaprendiz yes, I'm using docker-mac-net-connect right now
@AlbertoLanaro on which version MacOS are you? It doesn't seem to work on 13.5.2 (Ventura)
Hi @MaartenWolzak, currently I'm still on macOS 12.5.1 (Monterey) with Docker Desktop 4.17.0 and docker-mac-net-connect 'v0.1.2' and everything is working. There is an open issue related to macOS ventura: github.com/chipmk/docker-mac-net-connect/issues/21. If you're having the same issue a solution seems to be to update Docker Desktop to 4.17.0
|
1

Mac running the desktop version of docker. The docker isn't running on the host machine and using a kind of virtual machine that includes Linux kernel. The network of this virtual machine is different from the host machine. To connect from your Mac host to running docker container used a kind of VPN connection:

enter image description here

When you run your docker with --net host switch you connect the container to a virtual machine network instead connect to your host machine network as it's working on Linux. Then trying to connect to 127.0.0.1 or to localhost isn't allow connections to the running container.

The solution to this issue is to expose needed ports from running container:

docker run -p 8080:8080

If you need to expose all ports from your container you can use -P switch.

For opposite connection use host.docker.internal URL from container.

More documentation about docker desktop for Mac networking

5 Comments

tried the host.docker.internal .. no dice
How to connect docker container when use --net host on local. Can you tell me more detail?
Does not work for me
If it was not clear from @ozlevka's answer, you need both of these measures docker run -it --net host ... Then from the container curl -X GET -H 'Accept: application/json' 'http://host.docker.internal:<port>/<path>
can someone specify the exact command? is it docker run -it --net host 192.168.65.0/24 with the Mac subnetwork?
1

In Docker desktop 4.38.0 (MAC) it's no longer possible to connect from the host into containers, I tried all possible port/network setup, but nothing helped. I reverted to 4.34.4, and the problem was solved. Just download the old version and install over the 4.38.0, and everything is running again.

2 Comments

And going forward!? :(
4.45.0 not working with host.docker.internal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.