1

In my docker file while building i want to access files on my host machine from localhost server rather than copying them to the docker context folder (i.e the place where Dockerfile is located)

FROM busybox
RUN curl -L http://localhost/latest/myfile -o /root/myfile

I get errror:

$ docker build -t archlinux/test .
... from localhost : Failed to connect to localhost port 80: Connection refused

So how to do it.

Solution: to use localhost/file

Use IP of the localhost for that change the

Listen 127.0.0.1:80 to Listen 80

Or

Best solution is use --network=host

docker build --network=host -t test .
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • 2
    Which kind of server are you running on localhost? Are you sure it is accepting connections on port 80? – Thomas Kainrad Mar 05 '19 at 08:51
  • 1
    Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – schnaader Mar 05 '19 at 08:55
  • 1
    Sorry i am trying to build first. I am not runing a container. – Santhosh Mar 05 '19 at 08:57
  • 2
    change 'localhost' to ip of host and check if the server is running on your host. – inthevortex Mar 05 '19 at 08:59
  • my server is running on the host – Santhosh Mar 05 '19 at 09:00
  • 1
    Changing the localhost to ip worked. The connection was getting refused because of httpd/conf/httpd.conf settings. (Changing Listen 127.0.0.1:80 to Listen 80 worked) – Santhosh Mar 05 '19 at 09:24

2 Answers2

1

This curl request has some deeper sense?

Dockerfile can be used anywhere(not only in your machine). For example, I would like to download the Dockerfile and build an image based on this file... I need to serve a resource(myfile) with a www server and run docker build with --network=host parameter - a bit annoying.

I think you should use COPY command for this job.

Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42
  • Thats is the best way too. BUt the url of localhos is not working – Santhosh Mar 05 '19 at 09:01
  • AFAIK the docker intermediate container wont be able to connect directly with the host unless you do as @sadaf2605 mentioned. and it would be better to follow the `COPY` way – Mostafa Hussein Mar 05 '19 at 09:07
  • You can add `myfile` next to Dockerfile and add `COPY` command to your Dockerfile like this: `COPY myfile /root/myfile`. Then you can do anything with it, for example you can get a content of this file: `cat /root/myfile` – Krzysztof Raciniewski Mar 05 '19 at 09:08
0

If you are using Docker for Linux, You would need to use --network="host" as argument when you are running docker run or docker build command, then your host machine's localhost 127.0.0.1 will be accessible to your docker container.

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
sadaf2605
  • 7,332
  • 8
  • 60
  • 103