Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lookup host.docker.internal: no such host [duplicate]

Tags:

docker

mongodb

go

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
like image 569
Lazlo Avatar asked Mar 20 '26 01:03

Lazlo


1 Answers

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.

like image 141
Vladimir Osinskiy Avatar answered Mar 22 '26 16:03

Vladimir Osinskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!