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

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改