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:

  • Accessing an Apache server in a Docker container using a hostname
  • Docker push fails, too many redirects
  • Docker compose “--no-build was passed”
  • docker 1.7.0 under ubuntu14.04,can not run a container,error message “device or resourcebusy ”
  • Switching off AWS instance automatically once a job is done
  • Following the Get Started: Stack server stuck loading page endlessly
  • 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";
       $response = stream_get_contents($socket);
    else {

    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?

  • Do docker images have access to the tmp dir?
  • Docker Wordpress Container Exits after ~30 seconds
  • Vagrant or Docker for environment development
  • Does AWS support Kubernetes?
  • Docker: Not a valid repository:
  • How to handle PHP project code in docker container
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.