Docker container not resolving to host

Trying to connect to the host machine from a docker container per the docker docs:

services:
  myService:
    extra_hosts:
      host.docker.internal: host-gateway

Inside the myService container, I’m using curl to try to connect to another service running on the host on port 8002:

> curl http://host.docker.internal:8002
curl: (7) Failed to connect to host.docker.internal port 8002: Connection timed out

What am I missing? Is this a bug?

3 Likes

I have the same issue. There must be a missing or conflicting firewall rule because it works if I stop the firewall. Docker’s firewall rules seems to be added but I’ve not spent the time to figure out what’s wrong and how to fix it.

I just ran into what appears to be the same issue today. I have host.docker.internal set in my /etc/hosts file on NixOS, but the docker containers I am running cannot access it from inside the container. It just works fine for my buddy running the same stack on M1 Mac with docker deskop (and without any Nix stuff).

Did anyone of you two have any luck with finding out the reason? @ivann or @timjnz?

Any luck here? I haven’t seen the issue as I don’t use NixOS much anymore for Docker stuff and haven’t tested anything but I’m curious as I did depend on something like this just the other month and would be dismayed if I could no longer do it on NixOS

yeah, it will probably pop up again I think. If it does I will investigate any iptables related issues that might be involved here.

1 Like

For my containers I do it like this:

Config on the service: nixos/default.nix at 3091d51bf59f43f7628bdb1abe21e34f45bf466e - nixos - Gitea: Git with a cup of tea

Allow the containers to access the DB on the host: nixos/default.nix at 3091d51bf59f43f7628bdb1abe21e34f45bf466e - nixos - Gitea: Git with a cup of tea

I reckon the first one you don’t need if you use compose.

2 Likes

Nice! I could never force myself to mess with iptables but now I have a good example. Many thanks.