如何从 docker 容器访问主机的 localhost 127.0.0.1 [英] How to access the Host's machine's localhost 127.0.0.1 from docker container

查看:175
本文介绍了如何从 docker 容器访问主机的 localhost 127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地主机上托管 Git 守护进程,即 '/usr/bin/git daemon --listen=127.0.0.1 --base-path=/opt' 作为 systemd代码> 服务,我正在尝试从 docker 容器访问它.我没有提到端口,因为我不想将端口暴露给外部网络.

I hosted Git daemon on local host i.e. '/usr/bin/git daemon --listen=127.0.0.1 --base-path=/opt' as a systemd service and I am trying to access it from docker container. I didn't mentioned the port because I don't want to expose the port to outside network.

Dockerfile:

Dockerfile:

RUN git clone git://127.0.0.1/repo/ repo_dir

但它不起作用,它看起来像在容器内部,它试图连接容器的本地主机.

But its not working, its looks like inside container its trying to connect localhost of container.

那么如何从Docker容器连接主机的localhost?

So How to connect connect localhost of Host machine from Docker container?

推荐答案

它不工作,它看起来像在容器内,它试图连接容器的本地主机.

its not working, its looks like inside container its trying to connect localhost of container.

是的,这就是容器提供的隔离背后的全部想法,甚至是 docker build(它为每个 Dockerfile 行构建一个容器,并在中间映像中提交).

Yes, that is the all idea behind the isolation provided by container, even docker build (which builds one container per Dockerfile line, and commits it in an intermediate image).

正如 OP dhairya 所评论的,以及我在评论中提到的文档中提到的:Docker 容器网络" 和 docker build,可以设置hostRUN 构建期间的指令:然后 localhost 将引用主机 localhost.

As commented by the OP dhairya, and mentioned in the documentation I referred in my comments: "Docker container networking" and docker build, you can set to host the networking mode for the RUN instructions during build: then localhost would refer to the host localhost.

 docker build --network="host"

这是因为 仅适用于 API 1.25+, 在 docker v.1.13.0-rc5(2017 年 1 月)

This is since API 1.25+ only, in docker v.1.13.0-rc5 (January 2017)

POST/build 接受 networkmode 参数来指定构建期间使用的网络.

POST /build accepts networkmode parameter to specify network used during build.

<小时>

但是如果您在实际构建的映像中不需要 Git(为了运行它的主要进程),那么


But if you don't need Git in your actual built image (for its main process to run), it would be easier to

  • 在本地(直接在主机上)克隆存储库在 docker 构建之前.
    您需要将它克隆到 Dockerfile 所在的位置,因为它必须相对于正在构建的源目录(构建的上下文).
  • 使用 COPY 指令 在 Dockerfile 中,复制代表检出的 Git 存储库的主机文件夹.
    注意:将 .git/: 添加到您的 .dockerignore(与 Dockerfile 位于同一文件夹中的文件),以便复制 <代码>repo_dir/.git
    克隆存储库的文件夹,如果您的目标图像中没有 git.

  • clone the repo locally (directly on the host) before the docker build.
    You need to clone it where your Dockerfile is, as it must be relative to the source directory that is being built (the context of the build).
  • use a COPY directive in the Dockerfile, to copy the host folder representing the checked out Git repo.
    Note: add .git/: to your .dockerignore (file in the same folder as your Dockerfile), in order to not copy the repo_dir/.git folder of that cloned repo, if you don't have git in your target image.

COPY repo_dir .

(这里'.'代表正在构建的镜像中的当前WORKDIR)

(Here '.' represent the current WORKDIR within the image being built)

这篇关于如何从 docker 容器访问主机的 localhost 127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆