查找 host.docker.internal:没有此主机

huangapple go评论99阅读模式
英文:

lookup host.docker.internal: no such host

问题

我试图运行sudo docker-compose up来启动我的应用程序并连接到本地的MongoDB,但是遇到了这个错误:

time="2021-12-28T08:31:54Z" level=fatal msg="server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: host.docker.internal:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup host.docker.internal: no such host }, ] }"

最初,我尝试将localhost替换为host.docker.internal,以便将MongoDB URI连接到Docker网络,但似乎无法找到它。我正在使用最新版本的Docker,所以不是不支持的Docker版本的问题。

docker-compose.yaml文件内容如下:

version: '3.3'
services:
  app: 
    build: 
      context: .
      dockerfile: dockerfile
    ports: 
      - "8080:20717"
    restart: unless-stopped
    env_file: .env
    networks:
      - ext
      - int

networks:
 ext:
 int:
  internal: true

我之前有一个extra_hosts部分,但是用networks部分替换了它。

    extra_hosts:
        - "host.docker.internal:127.0.0.1"

我的.env文件包含了应用程序所需的URI和其他必要变量。

DB_URI=mongodb://host.docker.internal:27017
CITY_DB=nht_cities
COL_USER=user
COL_CITY=city
USER_AUTH_TOKEN=50dbafa...

我的应用程序本身在端口8080上运行。

fmt.Println("Server running at port 8080")
log.Fatal(http.ListenAndServe(":8080", r)) // r是路由器
英文:

I was trying to run sudo docker-compose up to get my app running and connect to a localhost mongodb but I ran into this error

time="2021-12-28T08:31:54Z" level=fatal msg="server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: host.docker.internal:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup host.docker.internal: no such host }, ] }"

Initially, I tried to replace localhost with host.docker.internal instead to have the mongo uri connect with the docker network but it doesn't seem to be able find it. I'm on the latest version of docker too, so it's not a question of unsupported docker version.

docker-compose.yaml

version: '3.3'
services:
  app: 
    build: 
      context: .
      dockerfile: dockerfile
    ports: 
      - "8080:20717"
    restart: unless-stopped
    env_file: .env
    networks:
      - ext
      - int

networks:
 ext:
 int:
  internal: true

I previously had an extra_hosts part but replaced that with the networks portion.

    extra_hosts:
        - "host.docker.internal:127.0.0.1"

My .env file contains the URI and some other necessary variables for the app

DB_URI=mongodb://host.docker.internal:27017
CITY_DB=nht_cities
COL_USER=user
COL_CITY=city
USER_AUTH_TOKEN=50dbafa...

My app itself runs on port 8080

fmt.Println("Server running at port 8080")
log.Fatal(http.ListenAndServe(":8080", r)) // r being the router

答案1

得分: 6

将以下内容添加到docker compose中的"app"服务中:

extra_hosts:
  - "host.docker.internal:host-gateway"

然后你的应用程序将能够连接到在主机上运行的mongodb。

英文:

Add this to "app" service in docker compose

extra_hosts:
  - "host.docker.internal:host-gateway"

Then your app will be able to connect to mongodb running on host machine.

huangapple
  • 本文由 发表于 2021年12月28日 18:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70505750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定