我试图运行 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 }, ] }"
Run Code Online (Sandbox Code Playgroud)
最初,我尝试用 localhost 替换,host.docker.internal让 mongo 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
Run Code Online (Sandbox Code Playgroud)
我以前有一个extra_hosts零件,但用该networks部分替换了它。
extra_hosts:
- "host.docker.internal:127.0.0.1"
Run Code Online (Sandbox Code Playgroud)
我的.env文件包含 URI 和应用程序的一些其他必要变量
DB_URI=mongodb://host.docker.internal:27017
CITY_DB=nht_cities
COL_USER=user
COL_CITY=city
USER_AUTH_TOKEN=50dbafa...
Run Code Online (Sandbox Code Playgroud)
我的应用程序本身在端口 8080 上运行
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
Run Code Online (Sandbox Code Playgroud)
小智 8
将其添加到 docker compose 中的“app”服务
extra_hosts:
- "host.docker.internal:host-gateway"
Run Code Online (Sandbox Code Playgroud)
然后您的应用程序将能够连接到主机上运行的 mongodb。
| 归档时间: |
|
| 查看次数: |
8371 次 |
| 最近记录: |