PHP code example of alvar01o / docker

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

    

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

$dockerComposeDirectory = './scripts/laravel-commands';
$dockerCompose = new DockerCompose($dockerComposeDirectory);
$dockerCompose->up();
$dockerCompose->start();

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

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

$containerInstance = DockerContainer::create($imageName)
    ->privileged()
    ->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)
    ->stopOnDestruct()
    ->start();

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

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

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

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

$instance->addPublicKey($pathToPublicKey);

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

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

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


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

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

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