简体   繁体   English

Linux Docker 容器未使用主机 localhost

[英]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.我正在努力使用 docker 容器中本地主机上的 API。 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.在 Windows 命令提示符下,我使用 CURL 命令向本地主机上的 API 发出 GET 请求。 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.现在我想在我的 Docker 容器中调用这个端点。 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   
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.如您所见,有一个响应,但我不完全确定 IP 192.168.65.2 是什么。 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.环顾 web,它显然是代表主机 IP 的“魔法”IP,我不确定这是否正确,因为我没有看到这个 IP 使用“ipconfig”,但现在,我将继续。

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.对于 Mac 上的 Docker,在 docker VM 中有一个神奇的 ip 192.168.65.2 代表主机,或者您可以只使用 host.docker.internal inside docker VM 就可以了。

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.最后,我在 bash 容器中使用“CURL”来查看我是否可以命中我在本文开头命中的 API。 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.使用curl http://host.docker.internal:57888/api/reference你确实连接到你的 API。

I know that because you get some HTML back.我知道,因为你得到了一些 HTML 回来。 Curl doesn't generate HTML when things go wrong, so the HTML must come from somewhere else: Your API.当 go 错误时,Curl 不会生成 HTML,因此 HTML 必须来自其他地方:您的 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.也许 API 不喜欢用Host: header 包含host.docker.internal ,这就是它返回 400 错误的原因。 To figure that out, we'd need more information on how the API is coded and hosted.要弄清楚这一点,我们需要有关 API 是如何编码和托管的更多信息。

@Hans led me in the right direction. @Hans 带领我朝着正确的方向前进。 host.docker.internal was indeed connected to my API, but the API didn't like the hostname so it caused the HTTP 400 error. host.docker.internal确实连接到我的 API,但 API 不喜欢主机名,因此导致 HTTP 400 错误。

Therefore in the Docker Compose file, I changed the extra_hosts to this因此在 Docker Compose 文件中,我将 extra_hosts 更改为此

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现在容器使用http://localhost:57888/api/reference并成功连接到 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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