简体   繁体   中英

Linux Docker container not using host localhost

Apologises, I know this question is very common but I have now come up against a brick wall and I need a point in the right direction. Thanks.

I am struggling to use a API that is on localhost within a docker container. I have followed many guides but I seem to be missing something. My steps:

In Windows Command prompt, I use the CURL command to fire a GET request to the API on localhost. The request succeeds:

curl http://localhost:57888/api/reference

[HttpGet()]
public ActionResult CheckIfOnline()
{
    // Breakpoint hits here
    return Ok();
}

Now I would like to call this end-point inside my Docker container. I've tried to do this in the compose file like:

container-api:
    container_name: container-api
    build:
        context: ..
        dockerfile: Dockerfile
    ports:
        - "3007:3001"
    env_file:
        - ./file.env
    extra_hosts:
        - "host.docker.internal:host-gateway"

I assume from my research this essentially means the container can now 'see' the host machine and, therefore could use localhost? (Happy to be proved wrong)

So when I create the container, I first ping host.docker.internal to see if it's available.

ping host.docker.internal   
PING host.docker.internal (192.168.65.2) 56(84) bytes of data

As you can see, there is a response, but I am not entirely sure what IP 192.168.65.2 is. Looking around the web, it is apparently a 'magic' IP that represents the host IP, I am not sure if this is right as I don't see this IP using 'ipconfig', but for now, I will continue.

For Docker on Mac, there is a magic ip 192.168.65.2 in docker VM which represent host machine, or you can just use host.docker.internal inside docker VM will ok.

Lastly I use 'CURL' in the bash container to see if I can hit the API that I hit at the start of this post. However I get this error:

# curl http://host.docker.internal:57888/api/reference
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>

Can anyone point me in the right direction please?

Thanks!

With curl http://host.docker.internal:57888/api/reference you are indeed connected to your API.

I know that because you get some HTML back. Curl doesn't generate HTML when things go wrong, so the HTML must come from somewhere else: Your API.

Maybe the API doesn't like to be called with a Host: header containing host.docker.internal and that's why it's returning the 400 error. To figure that out, we'd need more information on how the API is coded and hosted.

@Hans led me in the right direction. host.docker.internal was indeed connected to my API, but the API didn't like the hostname so it caused the HTTP 400 error.

Therefore in the Docker Compose file, I changed the extra_hosts to this

container-api:
    container_name: container-api
    build:
        context: ..
        dockerfile: Dockerfile
    ports:
        - "3007:3001"
    env_file:
        - ./file.env
    extra_hosts:
        - "localhost:host-gateway"

Now the container use http://localhost:57888/api/reference and it connects to the API successfully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM