Ivan Shibkih
Ivan Shibkih

Reputation: 41

Unable to connect to unix:///var/run/docker.sock (Permission denied) from PHP code

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?

Upvotes: 1

Views: 3557

Answers (2)

Salah
Salah

Reputation: 64

change permission of docker.sock to 0755

chmod 755 /var/run/docker.sock

it's also work for all

Upvotes: 0

Toby
Toby

Reputation: 21

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

Upvotes: 2

Related Questions