0

I have created below docker-composer yml file to create environment for connect php to socket server.

services:
  websocket-server:
    build: .
    command: php -S 127.0.0.1:1000 -t /var/www/html/websocket
    volumes:
      - ./src:/var/www/html
    ports:
      - 1000:1000
  
  php-tutorial:
    build: .
    volumes: 
      - ./src:/var/www/html
    ports:
      - 7000:80

My Dockerfile looks like

FROM php:8.0-apache
WORKDIR /var/www/html
RUN apt-get update -y && apt-get install -y telnet libmariadb-dev
RUN docker-php-ext-install mysqli sockets

After buid docker container, both container running well , in websocket-server I'm getting below log

[Fri Feb 3 05:12:43 2023] PHP 8.0.27 Development Server (http://127.0.0.1:1000) started

Now I have written a php code to connect socket

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0);

if (socket_connect($socket, '127.0.0.1', 1000) === false) {
    echo "Unable to connect to server\n";
    exit;
}

socket_write($socket, "Hello, server!");

$response = socket_read($socket, 1024);

echo "Response from server: $response\n";

socket_close($socket);

After run in browser , I'm getting below Warning

Warning: socket_connect(): unable to connect [111]: Connection refused in /var/www/html/index.php on line 4
Unable to connect to server

I am unable to find the solution.

Niloy Rony
  • 602
  • 1
  • 8
  • 23
  • You don't run that code in the browser but rather in a webserver, can you clarify what exactly you're doing? Why do you use multiple containers when you're using 127.0.0.1, which can only end up in the same system? – Ulrich Eckhardt Feb 03 '23 at 07:07
  • @UlrichEckhardt It may I'm wrong, but everything I'm trying to do in docker container. – Niloy Rony Feb 03 '23 at 08:34
  • So why do you mention two containers in your Q? – Ulrich Eckhardt Feb 03 '23 at 08:43
  • Does the `php -S` option tell the server where to listen? That _must_ have a `0.0.0.0` address and not `127.0.0.1`, or else the server won't be reachable from outside its own container, which sounds like your symptom. [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip) describes this more (I can't immediately find a PHP-specific example but the mechanics are the same across all languages). – David Maze Feb 03 '23 at 12:09

1 Answers1

0

You need to connect two containers to same network, forward port is just connect container port your host machine, it won't help you connect each containers, so your php container can touch socket container.

Read document: https://docs.docker.com/network/

I'm not sure if this example work, but you can try it:

services:
  websocket-server:
    build: .
    command: php -S 127.0.0.1:1000 -t /var/www/html/websocket
    volumes:
      - ./src:/var/www/html
    ports:
      - 1000:1000
    networks:
      - dev
  php-tutorial:
    build: .
    volumes: 
      - ./src:/var/www/html
    ports:
      - 7000:80
    networks:
      - dev
networks:
  dev:
    driver: bridge

And even if you connect to same network, you still need to change your code:

if (socket_connect($socket, '127.0.0.1', 1000) === false)

// change to below

if (socket_connect($socket, 'websocket-server', 1000) === false)
Charlie
  • 287
  • 11