无法连接到在docker容器内运行的套接字服务器

无法连接到在docker容器内运行的套接字服务器

问题描述:

I have a docker compose file which combines nginx and php like this:

nginx:
  image: nginx
  ports:
    - "80:80"
    - "2443:2443"
  links:
    - phpfpm
  volumes:
    - ./nginx/anonymous.conf:/etc/nginx/conf.d/anonymous.conf
    - ./logs/nginx-error.log:/var/log/nginx/error.log
    - ./logs/nginx-access.log:/var/log/nginx/access.log
    - ./public:/usr/share/nginx/html

phpfpm:
  image: php:fpm
  expose:
   - "2443"
  volumes:
    - ./public:/usr/share/nginx/html

I can see my website i.e index.php page on browser with the virtual host i have already made lets say anonymous.com

Now inside my phpfpm container i started a socket server based on Ratchet which is listening to port 2443

// bin/server.php
$webSocketServer = new WsServer(new Chat());
$server = IoServer::factory(
        new HttpServer($webSocketServer), 2443);
$server->run();

This is how i run my server inside phpfpm container

php /usr/share/nginx/html/bin/server.php 

My understanding is, since i have already exposed 2443 and my ngnix and phpfpm containers are linked. I would be able to connect to my socket server running on phpfpm container by going to telnet anonymous.com 2443

But its not getting connected. Here is the output

$ telnet anonymous.com 2443
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Please note that previously when i had both nginx and php-fpm were on single container, every thing was working fine. So i am sure that there is nothing wrong with PHP. It just i cannot figure out how to access the socket server from outside world.

Thanks

Update If i use my phpfpm container ip and use it with port 2443 through browser, its working fine. but the thing is i cannot rely on container ip as its all dynamic.

我有一个docker compose文件,它结合了nginx和php,如下所示: p>

  nginx:
 image:nginx 
 ports:
  - “80:80”
  - “2443:2443”
 links:
  -  phpfpm 
卷:
  -  ./nginx/  anonymous.conf:/etc/nginx/conf.d/anonymous.conf 
  -  ./logs/nginx-error.log:/var/log/nginx/error.log
  -  ./logs/nginx-access。  log:/var/log/nginx/access.log 
  -  ./public:/usr/share/nginx/html

phpfpm:
 image:php:fpm 
 expose:
  - “2443”\  n卷:
  -  ./public:/usr/share/nginx/html
nn

我可以看到我的网站,即 index.php code >我已经制作了虚拟主机的浏览器页面让我们说 anonymous.com code> p>

现在在我的phpfpm容器中我启动了一个基于的套接字服务器 Ratchet code>正在侦听端口 2443 code> p>

  // bin / server.php 
 $ webSocketServer = new WsServer(new Chat(  )); 
 $ server = IoServer :: factory(
新的HttpServer($ we  bSocketServer),2443); 
 $ server-> run(); 
  code>  pre> 
 
 

这就是我在 phpfpm code>中运行服务器的方法 容器 p>

  php /usr/share/nginx/html/bin/server.php 
  code>  pre> 
 
 

我的理解是 因为我已经公开了 2443 code>,并且我的 ngnix code>和 phpfpm code>容器已被链接。 我可以通过转到 telnet anonymous.com 2443 code> p>

连接到运行在 phpfpm code>容器上的套接字服务器但是它不是 连接。 这是输出 p>

  $ telnet anonymous.com 2443 
Trying 127.0.0.1 ... 
连接到localhost。
Escape字符是'^]'。
关闭 外国主机。
  code>  pre> 
 
 

请注意,以前当我同时使用nginx和php-fpm在单个容器上时,每件事都运行良好。 所以我确信PHP没有任何问题。 它只是我无法弄清楚如何从外部世界访问套接字服务器。 p>

谢谢 p>

更新 strong> 如果我 使用我的 phpfpm code>容器ip并通过浏览器端口2443使用它,它工作正常。 但事情是我不能依赖容器ip作为它的全部动态。 p> div>

You should use the name of the service as the hostname. So to connect from nginx to phpfpm use phpfpm:2443

I had a similar issue (same output) using a socket server based on Ratchet. Mine was only a PHP container (= no nginx/apache/...). It was because of the 3rd parameter given to the factory :

$server = IoServer::factory(
  new HttpServer($webSocketServer), 2443, '127.0.0.1'
);

Removing 127.0.0.1 (and then use the default 0.0.0.0 value) solved the problem.