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:

  • How to set-up a docker registry acting as a Proxy?
  • Configure docker images as executable
  • docker: having trouble running npm install after creating a new user
  • Share docker images between hosts with NFS
  • Docker: Unable to execute RUN command after ADD when starting from scratch
  • Run shell script inside container and delete it?
  • 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?

  • dial tcp: lookup xxx.xxx.xxx.xxx: no such host
  • Windows Server 2016 Docker - cant install webfarm components to microsoft/iis image
  • Can't delete mysql database, table or even alter table inside docker
  • Docker image build as AWS CodePipeline step
  • Seeing protocol error with ln for mounted volume inside docker
  • DNS issue with docker image
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.