Host.docker.internal resolves to nothing in linux

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