2

The question is basic: how to I connect to the localhost of the host machine from inside of a Docker container?

I tried answers from this post, using add-host host.docker.internal:host-gateway or writing --network=host when running my container but none of these methods seem to work.

I have a simple hello world webserver up on my machine, and I can see it's contents with curl localhost:8000 from my host, but I can't curl it from inside the container. I tried curl host.docker.internal:8000, curl localhost:8000, and curl 127.0.0.1:8000 from inside the container (based on the solution I used to make localhost available there) but none of them seem to work and I get a Connection refused error every time.

I asked somebody else to try this out for me on their own machine and it worked for them, so I don't think I'm doing anything wrong.

Does anybody have any idea what is wrong with my containers?

Host machine: Ubuntu 20.04.01

Docker version: 20.10.7

Used image: Ubuntu 20.04 (and i386/ubuntu18.04)

Roozbeh Sayadi
  • 318
  • 2
  • 13
  • 1
    How is this question different from the canonical question you link to? How is the service on the host set up; does it only listen on the 127.0.0.1 `lo0` interface or on 0.0.0.0 all interfaces? – David Maze Nov 01 '21 at 14:00
  • Those methods didn't work for me so I thought my problem is something else than that answer. It listens on 0.0.0.0. Here's my code: https://pastebin.ubuntu.com/p/dpf4VB8tRP/ @DavidMaze – Roozbeh Sayadi Nov 01 '21 at 14:07
  • I also tried it with ```python3 -m http.server``` which sets up a web server on 0.0.0.0 port 8000 and it still doesn't work so I'm sure I don't have a bug in my webserver. @DavidMaze – Roozbeh Sayadi Nov 01 '21 at 14:34
  • same problem for me... same host machine version and docker version, host.docker.internal:xxxx not works even with `host.docker.internal:host-gateway` option... it resolves to 172.17.0.1 but not working due to bridge docker0 has no interface and containers attached and other bridges are created with docker-compose... my docker container is using 172.19.0.2 for eth0 but not works too as docker0 is DOWN ( based on ifconfig)... I want to connect to java REST under localhost:7777, works with postman, from docker container not... some ubuntu networking related bug? – Marek Bernád Dec 07 '21 at 15:42

1 Answers1

1

Temporary solution

This does not completely solve the problem for production purposes, but at least in order to get the localhost working, by adding these lines into docker-compose.yml it solved my issue for now (source):

services:
  my-service:
    network_mode: host

I am using apache nifi to use Java REST endpoints with the same ubuntu and docker versions, so in my case, it looks like this:

services:
  nifi:
    network_mode: host

After changing docker-compose.yml, I recommend stopping docker container, removing containers(docker-compose rm - do not use if you need some containers, otherwise use docker container rm container_id) and build with docker-compose up --build again.

In this case, I needed to use another localhost IP for my service to access with a browser (nifi started on other ip - 127.0.1.1 but works fine as well).

Searching for the problem / deeper into ubuntu-docker networking

Firstly, I will write down some useful commands that may be useful to find out a solution for the docker-ubuntu networking issue:

  • ip a - show all routing, network devices, interfaces and tunnels (mainly I can observe state DOWN with docker0)
  • ifconfig - list all interfaces
  • brctl show - ethernet bridge administration (docker0 has no attached interface / veth pair)
  • docker network ls - manages docker networks - names, drivers, scope...
  • docker network inspect bridge - I can see for docker0 bridge has no attached docker containers - empty and not used bridge

(useful link for ubuntu-docker networking explanation)

enter image description here

I guess that problem lays within veth pair (see link above), because when docker-compose occurs, there is a new bridge created (not docker0) that is connected to veth pair in my case, and docker0 is not used. My guess is that if docker0 is used, then host.docker.internal:host-gateway would work. Somehow in ubuntu networking, there is docker0 not used as the default bridge and this maybe should be changed.

I don't have much time left actually, well, I suppose someone can use this information and resolve the core of the problem later on.

Marek Bernád
  • 481
  • 2
  • 8
  • 28