1

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. inserir a descrição da imagem aqui

And on the system (CakePHP) from inside the docker container, I'm trying to consume like this (just testing for now). inserir a descrição da imagem aqui

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);
    }

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.