<p>If the container needs to connect to a host or a remote service, you need his ip or domain. <strong>Any other workaround only will work on your laptop or is just for academic purposes</strong>, not valid in the real scenarios with real users. For example: a container in aws needs to connect to an azure mysql.</p>
<p>In your case, you need the local ip. As I explained here <a href="https://stackoverflow.com/a/52213178/3957754">https://stackoverflow.com/a/52213178/3957754</a> you could get the ip before the <code>docker-compose up</code>:</p>
<pre><code>export MACHINE_HOST_IP=$(hostname -I | awk '{print $1}')
docker-compose up ...
</code></pre>
<p>And in your compose file use this value. For example of you have a mysql on your host and your container need it, you could use:</p>
<pre><code>version: '3.2'
services:
  wordpress:
    environment:
      DB_HOST : ${MACHINE_HOST_IP}:3306
      DB_USER : usr_wordpress
</code></pre>
<p>Review:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/52173352/what-is-the-difference-between-publishing-808080-and-80808080-in-a-docker-run/52213178#52213178">https://stackoverflow.com/questions/52173352/what-is-the-difference-between-publishing-808080-and-80808080-in-a-docker-run/52213178#52213178</a></li>
<li><a href="https://stackoverflow.com/questions/63136530/how-to-connect-api-with-web-spa-through-docker/63207679#63207679">https://stackoverflow.com/questions/63136530/how-to-connect-api-with-web-spa-through-docker/63207679#63207679</a></li>
</ul>

CC BY-SA 4.0
2022-04-12T00:39:48.583Z
2022-04-12T00:39:48.583Z
JRichardsz
<p>The problem is that you've got an extra colon in <code>requests.get(&quot;http//:host.docker.internal:8000&quot;)</code>. It should be <code>requests.get(&quot;http//host.docker.internal:8000&quot;)</code>.</p>
<p>You might have hit a bug in requests' URL parsing; from the exception message you can see it's trying to connect to <code>localhost:8000</code> when it would make more sense to complain about the malformed URL.</p>

CC BY-SA 4.0
2022-10-25T15:56:32.203Z
2022-10-25T15:56:32.203Z
Steve McKay
<p>My problem is that im trying to connect to a Docker host(Im running on Ubuntu) from a container. Ive tried several solutions including adding extra_hosts: host.docker.internal:host-gateway, but still im not able to establish the connection. Im running docker with docker-compose up.</p>
<p>So for example when i run script the <code>requests.get(&quot;http//:host.docker.internal:8000&quot;)</code> in the container then receive an error</p>
<pre><code>requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: / (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x7f18ac13dd90&gt;: Failed to establish a new connection: [Errno 111] Connection refused'))
</code></pre>
<p>.</p>
<p>This is my docker-compose.yml</p>
<pre><code> version: 3.7

services:
  web:
    container_name: schedule_model_service-api
    build: .
    ports:
      - &quot;8001:8001&quot;
    command: startserver
    volumes:
      - ./:/app
    env_file:
      - .env
    depends_on:
      - postgres
      - redis
    extra_hosts:
    - &quot;host.docker.internal:host-gateway&quot;


  worker:
    container_name: schedule_model_service-worker
    build: .
    command: celeryworker
    volumes:
      - ./:/app
    env_file:
      - .env
    depends_on:
      - web
      - postgres
      - redis

  redis:
    container_name: schedule_model_service-redis
    image: redis:6.2.6
    volumes:
      - ./docker-data/redis:/var/lib/redis
    env_file:
      - .env
    restart: on-failure

  postgres:
    container_name: schedule_model_service-db
    image: postgres:14.1
    volumes:
      - ./docker-data/postgresql:/var/lib/postgresql/data
    env_file:
      - .env
    restart: on-failure```


</code></pre>

CC BY-SA 4.0
2022-04-11T07:39:09.260Z
I want to add that this https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach solution did not work for me.
Joel
CC BY-SA 4.0
2022-04-12T00:30:10.597Z
I think your docker-compose is wrong. host.internal work very well. Compare it with https://stackoverflow.com/questions/63136530/how-to-connect-api-with-web-spa-through-docker/63207679#63207679
JRichardsz
CC BY-SA 4.0
2022-10-26T05:46:14.740Z
Well, whatever else is wrong, this `requests.get("http//:host.docker.internal:8000")`  is going to fail until the extra `:` after `http://` is removed.
Chris Becke
<docker><docker-compose><ubuntu>
How to connect from Docker container to the host?
Joel
# How to connect from Docker container to the host?

(Asked by: username_0 on 2022-04-11)

My problem is that im trying to connect to a Docker host(Im running on Ubuntu) from a container. Ive tried several solutions including adding extra_hosts: host.docker.internal:host-gateway, but still im not able to establish the connection. Im running docker with docker-compose up.
So for example when i run script the requests.get("http//:host.docker.internal:8000") in the container then receive an error
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))

.
This is my docker-compose.yml
 version: 3.7

services:
  web:
    container_name: schedule_model_service-api
    build: .
    ports:
      - "8001:8001"
    command: startserver
    volumes:
      - ./:/app
    env_file:
      - .env
    depends_on:
      - postgres
      - redis
    extra_hosts:
    - "host.docker.internal:host-gateway"


  worker:
    container_name: schedule_model_service-worker
    build: .
    command: celeryworker
    volumes:
      - ./:/app
    env_file:
      - .env
    depends_on:
      - web
      - postgres
      - redis

  redis:
    container_name: schedule_model_service-redis
    image: redis:6.2.6
    volumes:
      - ./docker-data/redis:/var/lib/redis
    env_file:
      - .env
    restart: on-failure

  postgres:
    container_name: schedule_model_service-db
    image: postgres:14.1
    volumes:
      - ./docker-data/postgresql:/var/lib/postgresql/data
    env_file:
      - .env
    restart: on-failure```

**Tags:** docker, docker-compose, ubuntu

> **Comments:**
> **username_0**: I want to add that this https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach solution did not work for me.  
> **username_1**: I think your docker-compose is wrong. host.internal work very well. Compare it with https://stackoverflow.com/questions/63136530/how-to-connect-api-with-web-spa-through-docker/63207679#63207679  
> **username_2**: Well, whatever else is wrong, this `requests.get("http//:host.docker.internal:8000")`  is going to fail until the extra `:` after `http://` is removed.  

---

## 2 answers

### Answer by username_1 on 2022-04-12. Score: 0 | Accepted answer

If the container needs to connect to a host or a remote service, you need his ip or domain. Any other workaround only will work on your laptop or is just for academic purposes, not valid in the real scenarios with real users. For example: a container in aws needs to connect to an azure mysql.
In your case, you need the local ip. As I explained here https://stackoverflow.com/a/52213178/3957754 you could get the ip before the docker-compose up:
export MACHINE_HOST_IP=$(hostname -I | awk '{print $1}')
docker-compose up ...

And in your compose file use this value. For example of you have a mysql on your host and your container need it, you could use:
version: '3.2'
services:
  wordpress:
    environment:
      DB_HOST : ${MACHINE_HOST_IP}:3306
      DB_USER : usr_wordpress

Review:

https://stackoverflow.com/questions/52173352/what-is-the-difference-between-publishing-808080-and-80808080-in-a-docker-run/52213178#52213178
https://stackoverflow.com/questions/63136530/how-to-connect-api-with-web-spa-through-docker/63207679#63207679


---

### Answer by username_3 on 2022-10-25. Score: 0

The problem is that you've got an extra colon in `requests.get("http//:host.docker.internal:8000")`. It should be `requests.get("http//host.docker.internal:8000")`.

You might have hit a bug in requests' URL parsing; from the exception message you can see it's trying to connect to `localhost:8000` when it would make more sense to complain about the malformed URL.


---

