doukuai3822 2016-10-11 15:57
浏览 512
已采纳

无法连接到在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.

  • 写回答

2条回答 默认 最新

  • douyu1656 2016-10-11 17:44
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题