PHP code example of diego-ninja / docker

1. Go to this page and download the library: Download diego-ninja/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/ */

    

diego-ninja / docker example snippets


$containerInstance = DockerContainer::create($imageName)->start();
$process = $containerInstance->execute('whoami');
$process->getOutput(); // returns the name of the user inside the docker container

$containerInstance = DockerContainerInstance::fromExisting("test-runner"); // uses a running container

$spinner = new Spinner("Running unit tests...") // starts a console spinner

$process = $containerInstance->execute('php vendor/bin/phpunit', true); // starts process asynchronous

while($process->isRunning()) {
    $spinner->advance(); // updates spinner
    usleep(2000);    
}

$process->isSuccessful() ? $spinner->success() : $spinner->error(); // finishes spinner based on result

$containerInstance = DockerContainer::create($imageName)->start();

$containerInstance = DockerContainer::create($imageName)
    ->doNotDaemonize()
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->doNotCleanUpAfterExit()
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->privileged()
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->shell('sh')
    ->start();

new DockerContainer($imageName, $nameOfContainer);

$containerInstance = DockerContainer::create($imageName)
    ->name($yourName)
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->mapPort($portOnHost, $portOnContainer)
    ->mapPort($anotherPortOnHost, $anotherPortOnContainer)
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->setEnvironmentVariable($variableKey, $variableValue)
    ->setEnvironmentVariable($anotherVariableKey, $anotherVariableValue)
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->setVolume($pathOnHost, $pathOnDocker)
    ->setVolume($anotherPathOnHost, $anotherPathOnDocker)
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->setLabel($labelName, $labelValue)
    ->setLabel($anotherLabelName, $anotherLabelValue)
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->setCommands('--api.insecure=true', '--providers.docker=true')
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->setOptionalArgs('-it', '-a')
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->stopOnDestruct()
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->network('my-network')
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->remoteHost('ssh://username@hostname')
    ->start();

$containerInstance = DockerContainer::create($imageName)
    ->command('ls -l /etc')
    ->start();

// returns "docker run -d --rm spatie/docker"
DockerContainer::create($imageName)->getStartCommand();

$containerInstance = DockerContainer::create($imageName)
    ->setStartCommandTimeout(120)
    ->start();

$process = $instance->execute($command);

$process = $instance->execute([$command, $anotherCommand]);
$asyncProcess = $instance->execute([$command, $anotherCommand], true);


$process = $instance->execute([$command, $anotherCommand], false, 3600);

$process->isSuccessful(); // returns a boolean

$instance->addPublicKey($pathToPublicKey);

$instance->addPublicKey($pathToPublicKey, $pathToAuthorizedKeys);

$instance->addFiles($fileOrDirectoryOnHost, $pathInContainer);

$inspectArray = $instance->inspect();
$inspectArray[0]['State']['Status']; // Running, Starting etc.
$inspectArray[0]['RestartCount']; // Integer
$inspectArray[0]['NetworkSettings']['IPAddress']; // 172.17.0.2

Ninja\Docker\DockerContainerInstance::macro('whoAmI', function () {
    $process = $containerInstance->run('whoami');


    return $process->getOutput();
});

$containerInstance = DockerContainer::create($imageName)->start();

$containerInstance->whoAmI(); // returns of name of user in the docker container