PHP socket connect socket_connect(): unable to connect [111 - php

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.

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)

Related

The problem Docker PHP-Redis and healthcheck

I have two services. Redis and PHP. How to make php wait for the launch of redis and write about it.I tried using healthcheck-s but apparently I am not setting it up correctly.
docker-compose.yml
version: '2'
services:
php:
build:
context: docker/web
dockerfile: Dockerfile-php-7.0
container_name: php
ports:
- "8280:80"
links:
- redis:redis
redis:
build: docker/cache
container_name: redis
Dockerfile-php-7.0
FROM php:7.0
RUN pecl install redis \
&& docker-php-ext-enable redis
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php"]
EXPOSE 80
index.php
<?php
echo 'Starting';
$redis = new Redis();
$redis->connect(getenv('host'), getenv('6379'));
var_dump($redis->incr('foo'));
?>
Dockerfile
FROM redis:3.2-alpine
COPY conf/redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
EXPOSE 6379
Don't be afraid to scold me. I am just starting to learn docker.
I would be very grateful for any help !!!
Here is the docker-compose.yml file that you can use:
version: '2.1'
services:
php:
build:
context: docker/web
dockerfile: Dockerfile-php-7.0
container_name: php
ports:
- "8280:80"
depends_on:
redis:
condition: service_healthy
redis:
build: docker/cache
container_name: redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
I also change the PHP script (index.php) as follows:
<?php
echo 'Starting';
$redis = new Redis();
$redis->connect('redis', '6379');
var_dump($redis->incr('foo'));
Now the whole stack works in my machine. I can connect redis container from php.
The php container log shows the following
docker logs -f php
Startingint(1)

Can't connect to Postgres database using PHP PDO and Docker

I am running a Docker app that is built on three images: php:7.4-fpm, nginx:stable-alpine and postgres:alpine.
I was previously attempting to use the image php:7.4-fpm-alpine, but apparently it does not come with the Postgres PDO driver, which was causing a could not find driver error. This post explains more about that, and how to fix it. I followed the steps in that post, including creating a Dockerfile for my PHP FPM service (though I did not create a PHP CLI service), and that remedied my driver issues. However, I am now getting the following error when I try to connect to my PSQL database using PDO:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 6432?
I know my database is running (on port 6432) because I can access it from my terminal and from Positco, and have created tables after my Docker app was created. I also tried to connect to a database on port 5432 (through which my Postgres desktop app is running), but that did not work either. I also tried to restart my Postgres container, but that did not help.
Here is my PHP:
class Database {
private $connection;
private static $options = array(
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
public function __construct() {
try {
$dsn = 'pgsql:host=localhost;port=6432;dbname=db_my_test_app';
$username = 'root';
$password = 'secret';
$connection = new PDO($dsn, $username, $password, self::$options);
$this->connection = $connection;
return $connection;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
$database = new Database();
...and my docker-compose.yml file:
version: '3'
networks:
my_test_app:
services:
# nginx
nginx-service:
image: nginx:stable-alpine
container_name: nginx-container
ports:
- "8080:80"
volumes:
- ./app:/var/www/project
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php-fpm-service
- postgres-service
networks:
- my_test_app
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
# postgres
postgres-service:
image: postgres:alpine
container_name: postgres-container
ports:
- "6432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: secret
POSTGRES_DB: db_my_test_app
networks:
- my_test_app
...and the PHP service Dockerfile (from the aforementioned site):
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y libpq-dev
RUN docker-php-ext-install pdo pdo_pgsql pgsql
RUN ln -s /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pgsql/extension=pgsql/' /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pdo_pgsql/extension=pdo_pgsql/' /usr/local/etc/php/php.ini
You are configuring docker under the default network mode: bridge. In order for the php-fpm-service to be able to access the postgres-service you need to make the following modifications:
In the docker-compose.yml file you'll need to add depends_on to php-fpm-service
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
depends_on:
- postgres-service
In your PHP config, you need modify localhost to postgres-service and using port 5432:
$dsn = 'pgsql:host=postgres-service;port=5432;dbname=db_my_test_app';

docker php-fpm: the connection between services by port 9000 fails

I'm setting up a config to run my environment. I've a mysql, redis, nginx and php-fpm.
The services mysql and redis are ok, but when I start nginx and php my environment don't run.
The nginx config call to fastcgi_pass: php:9000
The error is:
[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.224.1, server: ~.*, request: "POST /sellers HTTP/1.1", upstream: "fastcgi://192.168.224.4:9000", host: "127.0.0.1:8080"
My Dockerfile is:
FROM php:7.2-fpm-alpine
WORKDIR /app
# Install composer
RUN apk --update add curl && rm /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
# Copy files
COPY . /app/
## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
RUN composer up
CMD /wait && bin/console doctrine:migrations:migrate
and my docker-compose is:
version: '3'
services:
mysql:
image: mysql:5.7
restart: unless-stopped
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: database
MYSQL_DATABASE: cart
redis:
image: redis:alpine
ports:
- '6379:6379'
web:
image: nginx:latest
depends_on:
- php
ports:
- '8080:80'
volumes:
- .:/app
- ./docker/site.conf:/etc/nginx/conf.d/site.conf
php:
build:
context: .
depends_on:
- mysql
volumes:
- .:/app
environment:
- APP_ENV=prod
- APP_SECRET=aaaa
- DATABASE_URL=mysql://root:database#mysql:3306/cart
- REDIS_HOST=redis
- REDIS_PORT=6379
- WAIT_HOSTS= mysql:3306

docker mysql connection refused while connecting from PHP script

Basically I want to run the LAMP stack in an Docker container.
All services are so far running but as soon I try to connect to an mysql from within an PHP script I get following error:
Warning: mysqli::__construct(): (HY000/2002): Connection refused
docker-compose.yml
version: '3.2'
services:
php:
build: ./php/
networks:
- backend
volumes:
- './public/:/var/www/html'
apache:
build: ./apache/
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- '8080:80'
volumes:
- './public/:/var/www/html'
mysql:
image: 'mysql:5.6.40'
ports:
- "3306:3306"
networks:
- backend
environment:
- MYSQL_DATABASES=test
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=127.0.0.1
- MYSQL_PORT=3306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=test
networks:
frontend: null
backend: null
Dockerfile for PHP:
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; \
apk upgrade;
RUN docker-php-ext-install mysqli
I had tested it with the following approach:
docker exec -ti <mysql_docker> bash
and used the same crendatial for entering the mysql mode:
mysql -uroot -h127.0.0.1 -P3306 -p
and there is not problem with it.
So what may cause the above connection refused?

PhpStorm + Docker + Xdebug + DB SSH tunnel

Locally I have following docker-compose configuration:
nginx:
build:
context: ./nginx
ports:
- "80:80"
volumes:
- ./../logs:/home/web/logs/
- ./../:/home/web/my-website.com/
depends_on:
- php
php:
build:
context: ./php
volumes:
- ./../:/home/web/my-website.com/
working_dir: /home/web/my-website.com/
expose:
- "8123"
php container has Xdebug installed into it, I can easily connect to it from PhpStorm.
I have remote ClickHouse database which is connected via SSH Tunnel. When I start my container I just go into my container and execute:
ssh -4 login#host.com -p 2211 -L 8123:localhost:8123 -oStrictHostKeyChecking=no -Nf
After this, my site is able to use this connection, but when I execute console command
./yii analysis/start-charts 003b56fe-db47-11e8-bcc0-52540010e5bc 205
from PhpStorm, I'm getting an exception:
Failed to connect to 127.0.0.1 port 8123: Connection refused
If I jump into the container and launch the same command, everything works fine.
What's wrong? Why PhpStorm doesn't see my SSH tunnel?
I've got an answer on "superuser" site: https://superuser.com/questions/1374463/phpstorm-docker-xdebug-db-ssh-tunnel/1375961#1375961
Besides, I've added ports node to my php container definition, now it's the following:
php:
build:
context: ./php
volumes:
- ./../:/home/web/my-website.com/
working_dir: /home/web/my-website.com/
expose:
- "8123"
ports:
- "8123:8123"
depends_on:
- redis
- mysql

Categories