【问题标题】:Cannot access docker host from macos无法从 macos 访问 docker 主机
【发布时间】:2021-09-11 13:01:17
【问题描述】:

我正在尝试从 docker 容器访问我的主机系统

已尝试以下所有方法,而不是 127.0.0.1 和 localhost:

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

但似乎没有一个工作。

如果我使用 --net=host 运行我的 docker run 命令,我确实可以访问 localhost,但是我的端口映射都不会被暴露并且无法从外部 docker 访问。

我使用的是 Docker 版本 20.10.5,构建 55c4c88

更多信息。我正在运行一个名为 impervious 的软件(比特币闪电网络之上的一层)。它需要连接到我在 localhost:10001 上的本地 Polar 闪电节点。这是工具本身使用的配置文件(参见lnd 部分):

# 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

我使用以下 docker 命令运行不透水:

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

但它只是在尝试连接到 host.docker.internal 的节点时挂起

【问题讨论】:

  • 你能再解释一下你的问题吗?
  • 您能否提供一个minimal reproducible example 来显示您正在尝试建立的连接,以及您遇到的实际错误是什么?主机网络在 MacOS 上的工作方式与您期望的不同(并且通常不是最佳实践)。
  • 添加了更多信息@kishore

标签: docker


【解决方案1】:

运行桌面版 docker 的 Mac。 docker 不是在主机上运行,​​而是使用一种包含 Linux 内核的虚拟机。此虚拟机的网络与主机不同。要从您的 Mac 主机连接到正在运行的 docker 容器,请使用一种 VPN 连接:

当您使用--net host 开关运行 docker 时,您将容器连接到虚拟机网络,而不是连接到您的主机网络,因为它在 Linux 上运行。 然后尝试连接到 127.0.0.1 或 localhost 不允许连接到正在运行的容器。

解决此问题的方法是从正在运行的容器中公开所需的端口:

docker run -p 8080:8080

如果您需要公开容器中的所有端口,您可以使用-P 开关。

对于反向连接,请使用容器中的host.docker.internal URL。

More documentation about docker desktop for Mac networking

【讨论】:

  • 试过host.docker.internal ..没有骰子
  • 本地使用--net host时如何连接docker容器。你能告诉我更多细节吗?
【解决方案2】:

你试过docker-mac-net-connect吗?

问题与 macOS 有关。与 Linux 上的 Docker 不同,macOS 的 Docker 不会直接在 macOS 主机上公开容器网络。

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多