1

My Code fails to reach the Docker Socket

    $client = new GuzzleHttp\Client();
    $test = $client->request('GET','http://v1.40/containers/json',[
        'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
    ]);

I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.

4
  • do you have the module curl installed ? Commented Mar 22, 2022 at 10:47
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 22, 2022 at 12:27
  • yes curl is installed and a request to google is successful Commented Mar 22, 2022 at 12:32
  • working inside the Container with the cURL command from the cmd For clarity and future readers please add that command and output to the question. Commented Mar 28, 2022 at 12:54

2 Answers 2

1

So just in case someone stumbles upon this in the future and has the same or a similar problem.

Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)

user = root
group = root

you will have to append the -R flag to the run command to allow running the workers as root.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not a good idea to work around permission problems by escalating priviledges, and rarely, a good idea to run php processes as root - here's a reference. If this is in a production system it's worth finding a different solution, it's not clear from the question or answer what specifically running as root fixes/avoids (please edit the question/answer to clarify).
0

/var/run/docker.sock belongs to the docker group. In order to interact with it, simply add the user that the PHP process runs as, e.g. www-data, to the group.

sudo usermod -aG docker www-data

This avoids escalating to root directly, but comes with its own set of dangers. See the Docker documentaion on Linux post-install and security.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.