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

查看:3809
本文介绍了如何从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容器连接主机的本地主机?

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容器联网

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所在的位置,因为它必须相对于正在构建的源目录(构建的上下文).
  • 在Dockerfile中使用 COPY指令,复制代表已签出的Git存储库的主机文件夹.
    注意:将.git/:添加到您的.dockerignore(与Dockerfile相同的文件夹中的文件),以便 not 复制该克隆存储库的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天全站免登陆