Lat*_*san 5 php docker docker-compose reactphp
我正在尝试使用以下技术构建需要处理许多请求/秒的轻量级 api 服务器:
这是我将所有内容放在一起的方式。PS 这个项目是代号flying-pony
文件夹结构:https : //i.stack.imgur.com/a2TPB.png
docker-compose.yml:
flying_pony_php_service:
container_name: flying_pony_php_service
build:
context: ./service
dockerfile: Dockerfile
ports:
- "9195:8080"
volumes:
- ./service:/app
Run Code Online (Sandbox Code Playgroud)
服务/Dockerfile:
FROM php:7.1-cli-alpine
ADD . /app
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT /entrypoint.sh
Run Code Online (Sandbox Code Playgroud)
服务/入口点.sh
#!/bin/sh
/app/service.php
Run Code Online (Sandbox Code Playgroud)
服务/service.php
#!/usr/local/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) {
$path = $request->getUri()->getPath();
$method = $request->getMethod();
if ($path === '/') {
if ($method === 'GET') {
return new React\Http\Response(200, array('Content-Type' => 'text/plain'), "Welcome to react-php version of flying pony api :)\n");
}
}
return new React\Http\Response(404, ['Content-Type' => 'text/plain'], 'Not found');
});
$socket = new React\Socket\Server(8080, $loop);
$server->listen($socket);
$loop->run();
Run Code Online (Sandbox Code Playgroud)
当项目构建并运行时,我确认使用docker-compose ps并得到以下信息:
? flying_pony_php git:(reactphp) docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------
flying_pony_php_service /bin/sh -c /entrypoint.sh Up 0.0.0.0:9195->8080/tcp
flying_pony_php_worker /bin/sh -c /entrypoint.sh Up
flying_pony_redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
Run Code Online (Sandbox Code Playgroud)
由于一切都已构建并正在运行;我在我的主机上访问了:http://localhost:9195并且页面没有加载(空响应错误)。但是,如果我通过 ssh 进入我的flying_pony_php_service容器并运行此命令:curl http://localhost:8080- 它正在工作(即 ReactPHP http 服务器正在响应,我收到了上面定义的欢迎消息)。
所以,我的问题是,为什么端口映射没有按预期工作?或者这与端口映射无关,不知何故容器的响应没有通过?
如您所见,一切都已正确连接,并且 ReactPHP 中的 Web 服务器在内部工作,但在外部无法访问/正常工作?
如果我使用诸如 apache/nginx 之类的东西,我在端口映射方面没有任何问题。有任何想法吗?PS 很抱歉这篇很长的帖子;试图在人们一一要求之前提供所有细节。
这是因为127.0.0.1如果没有明确提供接口(source),ReactPHP 的 TCP 套接字会侦听(localhost )。127.0.0.1不是从容器外部访问的 - 您应该监听0.0.0.0(这意味着“所有接口”)。
代替
$socket = new React\Socket\Server(8080, $loop);
Run Code Online (Sandbox Code Playgroud)
用
$socket = new React\Socket\Server('0.0.0.0:8080', $loop);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1111 次 |
| 最近记录: |