Can't connect to localhost of the host machine from inside of my Docker container

Viewed 1790

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)

1 Answers

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.

Related