<p>I'm not sure I can help you, but this might :</p>
<p><a href="https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach">https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach</a></p>
<p>tldr (bridge mode):</p>
<p>to get the ip of the container (in container CLI) :
<code>ip addr show eth0</code></p>
<p>to get the ip of the host's docker interface (host CLI) :
<code>sudo ip addr show docker0</code></p>

CC BY-SA 4.0
2021-12-22T14:30:19.823Z
2021-12-22T14:30:19.823Z
tintin
<p>First of all, please forgive me if any information is missing or if my question does not meet the standards required by the site. I have very basic knowledge of docker, linux and networking and I am lost.</p>
<p>I use Ubuntu version 20.2 and I have docker version 20.10.10.</p>
<p>I have a system (CakePHP) running in a docker container, and an API (LARAVEL) running on my machine's local host (outside of the docker container), and I need to consume that API on the system from inside the docker container.</p>
<p>This is the content of docker-compose.yml</p>
<pre><code>version: '2'
services:
  server-c10-php56:
    build:
      context: .
      dockerfile: Dockerfile
    image: php-c10-56
    volumes:
      - /srv/:/srv/
      - /var/www/html/:/var/www/html/
    ports:
      - &quot;9056:80&quot;
      - &quot;9443:443&quot;
    networks:
      - net30
    container_name: &quot;docker-php56&quot;
networks:
  sdnet:
    driver: &quot;bridge&quot;
  net30:
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/16
</code></pre>
<p>As I understand it, the system (CakePHP) is running inside this 172.30.0.0/16 subnet.
And the system (CakePHP) to connect to PostgreSQL, connects through IP 172.30.0.1 (I think it's the gateway to access the local host of my machine).</p>
<p>The API I can consume normally by Postman.
Here is a screenshot of a test.
<a href="https://i.sstatic.net/fbpBz.png" rel="noreferrer"><img src="https://i.sstatic.net/fbpBz.png" alt="inserir a descrição da imagem aqui" /></a></p>
<p>And on the system (CakePHP) from inside the docker container, I'm trying to consume like this (just testing for now).
<a href="https://i.sstatic.net/ALyOu.png" rel="noreferrer"><img src="https://i.sstatic.net/ALyOu.png" alt="inserir a descrição da imagem aqui" /></a></p>
<p>It takes a while trying to connect, but without success. This is the return obtained ( sorry for the array format).</p>
<pre><code>Array
(
    [response] =&gt; 
    [info] =&gt; Array
        (
            [url] =&gt; http://172.30.0.1:8000/api/queue/integracaoEcommerce
            [content_type] =&gt; 
            [http_code] =&gt; 0
            [header_size] =&gt; 0
            [request_size] =&gt; 0
            [filetime] =&gt; -1
            [ssl_verify_result] =&gt; 0
            [redirect_count] =&gt; 0
            [total_time] =&gt; 130.124754
            [namelookup_time] =&gt; 1.8E-5
            [connect_time] =&gt; 0
            [pretransfer_time] =&gt; 0
            [size_upload] =&gt; 0
            [size_download] =&gt; 0
            [speed_download] =&gt; 0
            [speed_upload] =&gt; 0
            [download_content_length] =&gt; -1
            [upload_content_length] =&gt; -1
            [starttransfer_time] =&gt; 0
            [redirect_time] =&gt; 0
            [redirect_url] =&gt; 
            [primary_ip] =&gt; 172.30.0.1
            [certinfo] =&gt; Array
                (
                )

            [primary_port] =&gt; 8000
            [local_ip] =&gt; 
            [local_port] =&gt; 0
        )
)
</code></pre>
<p>Here is the method used to consume the API (in case that information is important).</p>
<pre><code>private function callAPI($options){
        extract($options);

        $ch = curl_init();

        $header = array(
            &quot;Content-Type: application/json&quot;,
            &quot;Authentication: bearer {$integrador['token']}&quot;
        );

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        if(!empty($data) OR $verb == 'POST'){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, TRUE);    
        }

        if($verb)
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        return array('response' =&gt; $response, 'info' =&gt; $info);
    }
</code></pre>

<docker><docker-compose><api>
From inside a Docker container, how can I consume an API that is on the machine's localhost?
Rafael Arend
# From inside a Docker container, how can I consume an API that is on the machine's localhost?

(Asked by: username_0 on 2021-12-18)

First of all, please forgive me if any information is missing or if my question does not meet the standards required by the site. I have very basic knowledge of docker, linux and networking and I am lost.
I use Ubuntu version 20.2 and I have docker version 20.10.10.
I have a system (CakePHP) running in a docker container, and an API (LARAVEL) running on my machine's local host (outside of the docker container), and I need to consume that API on the system from inside the docker container.
This is the content of docker-compose.yml
version: '2'
services:
  server-c10-php56:
    build:
      context: .
      dockerfile: Dockerfile
    image: php-c10-56
    volumes:
      - /srv/:/srv/
      - /var/www/html/:/var/www/html/
    ports:
      - "9056:80"
      - "9443:443"
    networks:
      - net30
    container_name: "docker-php56"
networks:
  sdnet:
    driver: "bridge"
  net30:
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/16

As I understand it, the system (CakePHP) is running inside this 172.30.0.0/16 subnet.
And the system (CakePHP) to connect to PostgreSQL, connects through IP 172.30.0.1 (I think it's the gateway to access the local host of my machine).
The API I can consume normally by Postman.
Here is a screenshot of a test.

And on the system (CakePHP) from inside the docker container, I'm trying to consume like this (just testing for now).

It takes a while trying to connect, but without success. This is the return obtained ( sorry for the array format).
Array
(
    [response] => 
    [info] => Array
        (
            [url] => http://172.30.0.1:8000/api/queue/integracaoEcommerce
            [content_type] => 
            [http_code] => 0
            [header_size] => 0
            [request_size] => 0
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 130.124754
            [namelookup_time] => 1.8E-5
            [connect_time] => 0
            [pretransfer_time] => 0
            [size_upload] => 0
            [size_download] => 0
            [speed_download] => 0
            [speed_upload] => 0
            [download_content_length] => -1
            [upload_content_length] => -1
            [starttransfer_time] => 0
            [redirect_time] => 0
            [redirect_url] => 
            [primary_ip] => 172.30.0.1
            [certinfo] => Array
                (
                )

            [primary_port] => 8000
            [local_ip] => 
            [local_port] => 0
        )
)

Here is the method used to consume the API (in case that information is important).
private function callAPI($options){
        extract($options);

        $ch = curl_init();

        $header = array(
            "Content-Type: application/json",
            "Authentication: bearer {$integrador['token']}"
        );

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        if(!empty($data) OR $verb == 'POST'){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, TRUE);    
        }

        if($verb)
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        return array('response' => $response, 'info' => $info);
    }

**Tags:** docker, docker-compose, api


---

## 1 answers

### Answer by username_1 on 2021-12-22. Score: 4 | Accepted answer

I'm not sure I can help you, but this might :

<https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach>

tldr (bridge mode):

to get the ip of the container (in container CLI) :
`ip addr show eth0`

to get the ip of the host's docker interface (host CLI) :
`sudo ip addr show docker0`


---

