7

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:

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.

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

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
Dhairya
  • 533
  • 2
  • 9
  • 27
  • 1
    ANSWER: Hi guys found solution after reading the docker documentation, from both side i.e. from Host-to-Container and Contaier-TO-Host we need to map all the ports so for that net to specify the network mode `docker build --network="host"` it will set the networking mode for the RUN instructions during build (default "default"), now from container to host loopback is working file. thanks guys for giving time. Thanks @VonC – Dhairya Nov 02 '17 at 11:01
  • 1
    Good point. I have edited my answer accordingly, with links to the doc and moby releases including that feature. – VonC Nov 02 '17 at 12:11

1 Answers1

11

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

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).

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"

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

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


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

  • 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 .
    

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

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks @VonC for reply, I am tring to map the local file system to docker container while building docker container, I also posted similar question https://stackoverflow.com/questions/47030342/how-to-map-the-host-os-file-to-the-container-at-the-time-of-container-build-proc , So I came up with the local git daemon checkout but still I am not able to map the host machines file inside docker while build, SO I have only option like ADD and COPY but they have limitations as you mentioned above i.e 'file need to be inside docker context dir'. – Dhairya Nov 02 '17 at 06:55
  • So is there any alternative for this case ? – Dhairya Nov 02 '17 at 06:55
  • @Dhairya Yes: simply clone your git repo within the context folder of your build. – VonC Nov 02 '17 at 06:56
  • Hi @VonC I am new to docker and I have a query: when 2 docker containers are communicating with each other, the docker bridge 'docker0' will do the port forwarding in between two docker container, so suppose I hosted service like git-daemon on host machine on port 9418, how we can access these service from docker container? – Dhairya Nov 02 '17 at 07:20
  • 1
    @Dhairya My point is: you don't need tow containers: just one where you copy your repo content. – VonC Nov 02 '17 at 07:21
  • 2
    @Dhairya a docker daemon on the host machine is not visible by any containers. You could create a second container mounting the repo, and hosting a docker daemon, then visible by your first container (https://docs.docker.com/engine/userguide/networking/)... but this seems overkill. Simple COPY and move on. – VonC Nov 02 '17 at 07:22
  • For me network_mode: "bridge" worked better with the port forwarding see – tschomacker Jun 04 '20 at 07:39