Raheel
Raheel

Reputation: 9022

Unable to connect to socket server running inside docker container

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.

Upvotes: 0

Views: 6323

Answers (2)

v.nivuahc
v.nivuahc

Reputation: 862

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.

Upvotes: 2

dnephin
dnephin

Reputation: 28170

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

Upvotes: 0

Related Questions