我试图运行sudo docker-compose来运行我的应用程序并连接到本地主机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 }, ] }"
最初,我尝试用host.docker.internal
替换本地主机,让mongo与坞网络连接,但它似乎找不到它。我也在最新版本的码头,所以这不是一个不受支持的对接版本的问题。
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 being the router
发布于 2022-07-03 08:53:10
将此添加到docker组合中的"app“服务中
extra_hosts:
- "host.docker.internal:host-gateway"
然后,您的应用程序将能够连接到运行在主机上的mongodb。
https://stackoverflow.com/questions/70505750
复制相似问题