PHP code example of spatie / docker

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

    

spatie / 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 = 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();

$containerInstance = DockerContainer::create($imageName)
    ->dockerBin('/usr/local/bin/docker')
    ->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)
    ->mapPort($portOnHost, $portOnContainer, 'udp')
    ->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]);

$process = $instance->execute($command, 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

Spatie\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