How to access the Host machine's localhost 127.0.0.1 from docker container

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?

easiest way it to do a --net=host. But this cannot be done during docker build.
But why don’t you COPY/ADD the necessary repository?
If this is roughly okay for you, maybe a Multi-Stage build is what you are looking for.

1 Like

In docker document I found ‘$ docker build --network=“host”’ so it will expose the ports of container to host machine and vice versa … so git daemon in running on hostmachine and using git protocol clone works fine in container