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:

  • Does Kubernetes download Docker image automatically?
  • Google Cloud Container Registry Issues while pushing docker images
  • Tomcat datasource Properties with Docker and Jenkins
  • Connect to WordPress Docker Container
  • How to cache the RUN npm install instruction when docker build a Dockerfile
  • How to figure out if docker master or agent running
  • 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?

  • Docker, How to replace capistrano tasks in docker
  • SSL handshake failure with node.js https
  • how to use docker and salt provisioning [closed]
  • Docker not adding existing file
  • Locating data volumes in Docker
  • Docker container sshd logs
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.