PHP code example of khs1994 / docker

1. Go to this page and download the library: Download khs1994/docker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

khs1994 / docker example snippets




ocker\Docker;

$option = Docker::createOptionArray('127.0.0.1:2375');

// Connect TLS Docker Daemon

// $option = Docker::createOptionArray('123.123.123.133:2376',true,'/etc/docker/cert');

$docker = Docker::docker($option);

$docker_container = $docker->container;

$docker_image = $docker->image;

/*
 * $ docker run -it -d -v lnmp-data:/app php:7.2.8-fpm-alpine3.7 /bin/sh
 */

$image = 'php:7.2.8-fpm-alpine3.7';

$docker_image->pull($image);

$container_id = $docker_container
  ->setImage($image)
  ->setCmd(['/bin/sh'])
  ->setBinds(['lnmp-data:/app'])
  ->create(true);

$docker_container->start($container_id);

var_dump($docker_container->logs($container_id));

use Docker;

// call by facade
Docker::container()->list();

// call by helper function
docker()->container()->list();

// call by DI

class MyController
{
    public $docker;

    public function __construct(\Docker\Docker $docker)
    {
        $this->docker = $docker;
    }

    public function demo()
    {
        $this->docker->container()->list();
    }
}
bash
$ php artisan vendor:publish --tag=config