I have issue with stream_socket_client()
function, but only under some circumstances.
I'm using docker-compose to build environment containing nginx, php-fpm (7.1.3), and schickling/mailcatcher (and a couple of other not important containers).
My problem relates to SwiftMailer, but I've isolated the issue to the following code:
// /var/www/web/app_dev.php
$streamContext = stream_context_create();
$timeout = 5;
$_stream = stream_socket_client('172.21.0.106:1025', $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
var_dump($_stream);
172.21.0.106:1025
is correct and static IP:port for Mailcatcher.
Now, if I request this file via browser, I get:
/var/www/web/app_dev.php:8:resource(4, stream)
which is correct.
When I do docker exec php_container /var/www/web/app_dev.php
, I get:
/var/www/web/app_dev.php:9: resource(20) of type (stream)
which is also correct.
But when I run this script via PhpStorm (option 'Run >Run...') using configured Docker remote interpreter (working fine in general), I get output:
docker://php_container:latest/php /var/www/web/app_dev.php
Warning: stream_socket_client(): unable to connect to 172.21.0.106:1025 (Connection timed out) in /var/www/web/app_dev.php on line 5
Call Stack: 0.0001 349080 1. {main}() /var/www/web/app_dev.php:0 0.0032 360960 2. stream_socket_client() /var/www/web/app_dev.php:5
Process finished with exit code 0
I did some tests and it looks like the problem exists when trying to use any other container's host:port, but it's working when using anything outside of docker network.
For example using '172.21.0.103:3306'
(MySQL container) the issue remains, but with 'google.com:80'
, it works also via PhpStorm Run feature.
Important part of docker-compose.yml:
services:
php:
build: ./docker/php
container_name: php_container
volumes:
- .:/var/www
networks:
mynet:
ipv4_address: 172.21.0.102
mailcatcher:
image: schickling/mailcatcher
container_name: mailcatcher
ports:
- "1080:1080"
networks:
mynet:
ipv4_address: 172.21.0.106
networks:
mynet:
ipam:
config:
- subnet: 172.21.0.0/24
PHP Dockerfile:
FROM php:7-fpm
RUN apt-get update && apt-get install -y git zlib1g-dev zip unzip libgd-dev
RUN docker-php-ext-install pdo pdo_mysql zip gd
RUN docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache
RUN pecl install xdebug-2.5.0 && docker-php-ext-enable xdebug
WORKDIR /var/www
It would be pretty much of content to put docker-compose.yml
, all Dockerfile
s etc here, so if any other specific part of configuration would be required, just let me now in the comment.