doujiang6944 2017-09-05 11:31
浏览 114

ReactPHP和docker:无法解析从php到nginx容器的主机

I work with docker and microservices. Imagine we have 3 services:

          user API
        /
gateway API
        \
          hotel API

Requests go through the gateway to services. I have container with php and nginx, where volumes is the same. Next I give list of configs:

server {
    server_name gateway.api.loc;

    root /var/www/der-ibe/ibe-gateway-api/web;
    client_max_body_size 32m;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
       ...
    }
}
server {
    server_name hotel.api.loc;
    root /var/www/der-ibe/ibe-hotel-api/web;
    client_max_body_size 32m;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        ...
    }
}

...

docker-compose.yml

version: "3.1" 
services:

    tphp:
        build: docker/php
        restart: always
        container_name: my_tphp
        extra_hosts:
            - gateway.api.loc:172.14.10.10
            - user.api.loc:172.14.10.10
            - hotel.api.loc:172.14.10.10
        networks:
            - test
        volumes:
            - ../user-api:/var/www/user-api
            - ../hotel-api:/var/www/hotel-api
            - ./:/var/www/gateway-api

    tnginx:
        build: docker/nginx
        restart: always
        container_name: my_tnginx
        ports:
            - 80:80
        networks:
            test:
                 ipv4_address: 172.14.10.10
        volumes:
            - ../user-api:/var/www/user-api
            - ../hotel-api:/var/www/hotel-api
            - ./:/var/www/gateway-api

networks:
    ibe:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 172.14.10.0/24

If I use simple cURL I can reach to hotel and user api, and I can get response from them, but if I try to do it with reactPHP - I can't! My peace of code:

 public function asyncGetRequest(Route $route, $params, $token)
{
    $url = $this->buildURL($route, $params);

    $loop = React\EventLoop\Factory::create();

    $dns = '172.14.10.10';
    $sender = Clue\React\Buzz\Io\Sender::createFromLoopDns($loop, $dns);

    //$url = 'http://172.14.10.10:3000/api/quota/list';

    $client = new Clue\React\Buzz\Browser($loop, $sender);
    //$client = new Clue\React\Buzz\Browser($loop);

    $promise = $client->get($url,
        [
            'Authorization' => $token,
        ]
    );

    /** @var Response $response */
    $response = new Response();

    try {
        $response = Clue\React\Block\await($promise, $loop);
    } catch (ResponseException $e) {
        $this->logger->error($e->getMessage());
    }

    dump($response->getBody()->getContents()); // I work on symfony 3.2
    die;
}

Clue library is just wrapper for react. So the error which I get is: DNS query for hotel.api.loc timed out and the last error An error occurred in the underlying stream.

So I have a question: what I do wrong? Why I have access to nginx container by curl, but can't access by reactPHP? Even If point dns!

But if I use bare ip with port I can reach it. I'm not pro in dns, just basic knowledge.

UPDATE

I use Linux Debian (in container). Work on Mac.

A little about logic: when request URI is gateway.api.loc/api/hotel/list inside gateway we send request to hotel.api.loc to the same URL(/api/hotel/list), accept response and return it.

I repeat that I have access to domains by cURL from, but can't do it with reactPHP and don't understand why...

  • 写回答

2条回答 默认 最新

  • duandu1915 2017-09-05 13:12
    关注

    Your issue is that there is no DNS resolution for gateway.api.loc or hotel.api.loc, they are defined in your Nginx config for routing but no anywhere else for DNS resolution. You need to change your compose to below so they also get resolved in docker DNS resolution

    version: "3.1" 
    services:
    
        tphp:
            build: docker/php
            restart: always
            container_name: my_tphp
            extra_hosts:
                - gateway.api.ibe.lazy-ants.loc:172.14.10.10
                - user.api.ibe.lazy-ants.loc:172.14.10.10
                - hotel.api.ibe.lazy-ants.loc:172.14.10.10
            networks:
                - test
            volumes:
                - ../user-api:/var/www/user-api
                - ../hotel-api:/var/www/hotel-api
                - ./:/var/www/gateway-api
    
        tnginx:
            build: docker/nginx
            restart: always
            container_name: my_tnginx
            ports:
                - 80:80
            networks:
                test:
                     ipv4_address: 172.14.10.10
                     aliases:
                         - hotel.api.loc
                         - gateway.api.loc
            volumes:
                - ../user-api:/var/www/user-api
                - ../hotel-api:/var/www/hotel-api
                - ./:/var/www/gateway-api
    
    networks:
        ibe:
          driver: bridge
          ipam:
            driver: default
            config:
              - subnet: 172.14.10.0/24
    

    This DNS resolution will only work inside the compose and not outside. So if you use it outside then you will have to make host entries

    Edit-1

    It seems your issue may be code only. You are trying to use gateway IP as DNS when it is not a DNS server.

    Change

    $loop = React\EventLoop\Factory::create();
    
    $dns = '172.14.10.10';
    $sender = Clue\React\Buzz\Io\Sender::createFromLoopDns($loop, $dns);
    
    //$url = 'http://172.14.10.10:3000/api/quota/list';
    
    $client = new Clue\React\Buzz\Browser($loop, $sender);
    

    to

    $loop = React\EventLoop\Factory::create();
    
    $dns = '172.14.10.10';
    //$sender = Clue\React\Buzz\Io\Sender::createFromLoopDns($loop, $dns);
    //$url = 'http://172.14.10.10:3000/api/quota/list';
    
    $client = new Clue\React\Buzz\Browser($loop);
    

    And see if it helps

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!