Host.docker.internal resolves to nothing in linux

I am trying to connect the local DB server from within a docker container and as part of it I am running the command
docker-compose run -w SERVICE [COMMAND]

I am getting an error

03:44:38.312 [error] GenServer #PID<0.259.0> terminating
(DBConnection.ConnectionError) tcp connect (host.docker.internal:5432): non-existing domain - :nxdomain
(connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
** (Mix) The database for DubberConnect.Repo couldn't be created: killed

Can anyone help me with this?

docker version
Client: Docker Engine - Community
 Version:           19.03.14
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        5eb3275d40
 Built:             Tue Dec  1 19:20:19 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          20.10.0-rc1
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       131bf7e
  Built:            Tue Nov 17 22:50:10 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.1
  GitCommit:        c623d1b36f09f8ef6536a057bd658b3aa8632828
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Operating System: Ubuntu 16.04.4 LTS

Could you post your config.<env>.exs that containing the database connection configuration?

I am using

HOST=host.docker.internal
PORT=5432
DATABASE_URL=ecto://postgres:postgres@host.docker.internal/demo_dev

in .env file

and in docker-compose.yml file calling the .env variable as

version: '3.2'

networks:
  net:

services:
  api:
    image: .............
    working_dir: ............
    networks:
      - net
    environment:
          - DATABASE_URL=${DATABASE_URL}
          - HOST=${HOST}
          - PORT=${PORT}

Seems the host.docker.internal hostname is a Mac Docker Desktop exclusive feature. Are you using Docker Desktop on Mac?

No… I am using Docker setup on Ubuntu… Are there any alternative for Ubuntu?

You can use any of the following approaches:

Using host network drive

In the docker-compose.yml.

...
services:
  api:
    network_mode: host
    ...

In the .env file,

DATABASE_URL=ecto://postgres:postgres@localhost/demo_dev

This approach makes your docker container directly use the network adapters on the host without creating subnet or NAT.

The network throughput of this approach is about 20% higher than the NAT approach, but it prevents the container to join any docker networks, and can expose ports that you don’t want to expose (for example, the epmd port 4369).

Add extra hosts to the container

In the docker-compose.yml,

...
services:
  api:
    extra_hosts:
      - "host.docker.internal:<the IP address of the host>"

This approach is less performant and less flexible, but is more secure.

1 Like

Thank you so much. :grinning: