I use docker-php with nginx + php-fpm (docker-compose project). When I try to run an example from the documentation :

<?php use Docker\API\Model\ContainersCreatePostBody; use Docker\Docker; $docker= Docker::create(); $containerConfig = new ContainersCreatePostBody(); $containerConfig->setImage('nginx:latest'); $containerConfig->setCmd(['echo', 'I am running a command']); $containerCreateResult = $docker->containerCreate($containerConfig); var_dump($containerCreateResult); exit; 

then I get the following error:

 Http \ Client \ Socket \ Exception \ ConnectionException - Permission denied 

As I understand it, the problem is in the group of the user who is running php-fpm, this group does not have rw rights to the mounted docker.sock socket file (in the docker-compose project, I see that I am mounting it from host to container)

Configuration:

docker-compose:

The shell directory contains an application on yii2, in which I use docker-php.

 version: '2' services: web: image: 'nginx:latest' container_name: web ports: - '80:80' - '443:443' volumes: - './:/shell' networks: - backend - frontend restart: always php: build: ./docker/php/ container_name: php volumes: - './:/shell' - '/var/run/docker.sock:/var/run/docker.sock' environment: [] networks: - backend restart: always networks: frontend: driver: bridge backend: driver: bridge 

Dockerfile for php-fpm: github gist (file is too big ~ 100 lines, therefore rendered)

In this container, docker is installed for experimentation, it is absolutely not necessary for docker-php to work.

Software versions:

  • Docker version 1.13.1
  • docker-compose version 1.8.0
  • Kubuntu 17.10 x64

I found similar problems on the Internet ( one , two , three ...), their solution was to add the user, from whom the application works, to the www-data group. If php-fpm works on behalf of www-data, then it is not clear why there was no access when I added www-data to the docker group (on the host).

If I assign 777 permissions to docker.sock, then everything will work, but this is not the best solution =)

  • Why do you need to run the container inside the container? for the voiced task (to run three isolated processes) it is completely unnecessary. - aleksandr barakin
  • @alexanderbarakin implement the task of managing containers in php. It is necessary to create a new container using docker-php that would start on the host (and not inside the container). Based on the availability of various services (for example, portainer) it is quite possible (to start the container management service in the container) - Dmitriy K.

0