1
votes

I use Docker Windows Toolbox

I created the docker container with PHP-FPM:

docker run -d -v /var/run/docker.sock:/var/run/docker.sock php:7.0-fpm-alpine

If I use curl directly from container shell:

curl --unix-socket /var/run/docker.sock http://containers/json

I got correct answer.

But if use PHP code:

$socket = stream_socket_client('unix:///var/run/docker.sock', $errno, $errstr);
if ($socket){
   $request = 'GET /version HTTP/1.1'."\r\n";
   $request.= 'Connection: Close'."\r\n\r\n";
   fwrite($socket,$request);
   $response = stream_get_contents($socket);
   fclose($socket);
   print_r($response);
}
else {
   print_r($errstr);
}

then I got the error below:

Warning: stream_socket_client(): unable to connect to unix:///var/run/docker.sock (Permission denied)

PHP works under www-data user. I tried to run PHP-FPM under root but got PHP-FPM error that I can't run php under root.

I tried to create "docker" user/group inside the container then ran PHP-FPM under "docker" user/group but it not helped.

How can I fix it?

2
Could this could be a curl version issue. Support for unix socket in curl has been added relatively recently. Is the application code using the same curl as bash?Sergei Rodionov
Mentioned PHP code doesn't use curl. I am unable to find approach using PHP curl with socket.Ivan Shibkih

2 Answers

2
votes

Make sure /var/run has r and x privilege for all users and docker.sock and mode of docker.sock is 0777

0
votes

change permission of docker.sock to 0755

chmod 755 /var/run/docker.sock

it's also work for all