<p>From your container's perspective <code>localhost</code> 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.</p>

<p><strong>edit</strong> <code>host.docker.internal</code> is recommended from 18.03 onward. </p>

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

<pre><code># Dockerfile

FROM python:3.7-alpine

RUN ping host.docker.internal -c 4
</code></pre>

<pre><code>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
</code></pre>

CC BY-SA 4.0
2023-10-02T09:56:23.893Z
What's about Docker on Linux? I'm having this error with Docker on Debian 12
`> [backend-apitest-client 5/9] RUN ping host.docker.internal:4873 -c 4:
0.383 ping: host.docker.internal:4873: Name or service not known
------
failed to solve: process "/bin/sh -c ping host.docker.internal:4873 -c 4" did not complete successfully: exit code: 2`
Lewis
CC BY-SA 4.0
2019-04-06T13:05:16.287Z
2019-04-06T13:16:51.583Z
2019-04-06T13:16:51.583Z
swysocki
<p>I have a Dockerfile which I want to run some tests inside of as part of a multistage build. The tests attempt to make a connection to localhost, however it appears that they're not able to connect to the specific port (which is ready on the host machine) inside the RUN command of the Dockerfile. Is such a thing possible, and if so how do I achieve it? Obviously external connections can be made at build time (to download dependencies and such) and loopbacks at run time</p>

<pre><code>FROM python:3.7-alpine as tester
COPY . .
RUN python -m unittest discover
</code></pre>

<p>And for reference, the Python code checking the connection</p>

<pre><code>def setUp():
    timeout = 120.0
    port = 15903
    host = 'localhost'
    start_time = time.perf_counter()
    while True:
        try:
            with socket.create_connection((host, port), timeout=timeout):
                break
        except OSError as ex:
            time.sleep(1.0)
            if time.perf_counter() - start_time &gt;= timeout:
                raise TimeoutError('Waited too long for the port {} on host {} to start accepting connections.'.format(port, host)) from ex 
</code></pre>

<docker><dockerfile><python>
How to connect to host during a Docker build?
Madden
# How to connect to host during a Docker build?

(Asked by: username_0 on 2019-04-04)

I have a Dockerfile which I want to run some tests inside of as part of a multistage build. The tests attempt to make a connection to localhost, however it appears that they're not able to connect to the specific port (which is ready on the host machine) inside the RUN command of the Dockerfile. Is such a thing possible, and if so how do I achieve it? Obviously external connections can be made at build time (to download dependencies and such) and loopbacks at run time
FROM python:3.7-alpine as tester
COPY . .
RUN python -m unittest discover

And for reference, the Python code checking the connection
def setUp():
    timeout = 120.0
    port = 15903
    host = 'localhost'
    start_time = time.perf_counter()
    while True:
        try:
            with socket.create_connection((host, port), timeout=timeout):
                break
        except OSError as ex:
            time.sleep(1.0)
            if time.perf_counter() - start_time >= timeout:
                raise TimeoutError('Waited too long for the port {} on host {} to start accepting connections.'.format(port, host)) from ex

**Tags:** docker, dockerfile, python


---

## 1 answers

### Answer by username_1 on 2019-04-06. Score: 5 | Accepted answer

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
 ---> a93594ce93e7
Step 2/2 : RUN ping host.docker.internal -c 4
 ---> 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
 ---> b2f32c26a135
Successfully built b2f32c26a135

> **Comments:**
> **username_2**: What's about Docker on Linux? I'm having this error with Docker on Debian 12 `> [backend-apitest-client 5/9] RUN ping host.docker.internal:4873 -c 4: 0.383 ping: host.docker.internal:4873: Name or service not known ------ failed to solve: process "/bin/sh -c ping host.docker.internal:4873 -c 4" did not complete successfully: exit code: 2`  

---

