How to connect to host during a Docker build?
From your container's perspective localhost is the container itself.  If you want to communicate with the host machine, you will need the IP address of the host itself.  Depending on your Operating System and Docker configuration, this IP address varies.

edit host.docker.internal is recommended from 18.03 onward. 

Here is an example of communicating with the Docker host from a container on Mac:

# Dockerfile

FROM python:3.7-alpine

RUN ping host.docker.internal -c 4


sending build context to Docker daemon  562.7kB
Step 1/2 : FROM python:3.7-alpine
 ---&gt; a93594ce93e7
Step 2/2 : RUN ping host.docker.internal -c 4
 ---&gt; Running in 54ce465f7386
PING host.docker.internal (192.168.65.2): 56 data bytes
64 bytes from 192.168.65.2: seq=0 ttl=37 time=0.735 ms
64 bytes from 192.168.65.2: seq=1 ttl=37 time=1.898 ms
64 bytes from 192.168.65.2: seq=2 ttl=37 time=0.720 ms
64 bytes from 192.168.65.2: seq=3 ttl=37 time=0.643 ms

--- host.docker.internal ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.643/0.999/1.898 ms
Removing intermediate container 54ce465f7386
 ---&gt; b2f32c26a135
Successfully built b2f32c26a135