-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09a28a4
commit ae0b899
Showing
2 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
namespace idrislab\DockerServices; | ||
|
||
use Docker\DockerClient; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Class Docker | ||
* | ||
* @package idrislab\DockerServices | ||
*/ | ||
class Docker | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $sudo = false; | ||
/** | ||
* @var \Docker\Docker | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* Docker constructor. | ||
* | ||
* @param \Docker\Docker $docker | ||
*/ | ||
public function __construct(\Docker\Docker $docker) | ||
{ | ||
$client = new DockerClient([ | ||
'remote_socket' => getenv('DOCKER_SOCKET') ? : 'unix:///var/run/docker.sock', | ||
]); | ||
|
||
$this->client= new $docker($client); | ||
} | ||
|
||
/** | ||
* @param $image | ||
* @param bool $verbose | ||
* | ||
* @return string | ||
*/ | ||
public function pull($image, $verbose = true) | ||
{ | ||
return $this->docker("pull $image", $verbose); | ||
} | ||
|
||
/** | ||
* @param $options | ||
* @param bool $verbose | ||
* | ||
* @return string | ||
*/ | ||
public function run($options, $verbose = false) | ||
{ | ||
return $this->docker("run $options", $verbose); | ||
} | ||
|
||
/** | ||
* @param $name | ||
* | ||
* @return bool | ||
*/ | ||
public function isNamedContainerRunning($name) | ||
{ | ||
$containerManager = $this->client->getContainerManager(); | ||
$containers = $containerManager->findAll(); | ||
|
||
foreach ($containers as $container) { | ||
if ($container->getNames()[0] == "/".$name && $container->getState() == "running") { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param $name | ||
* | ||
* @return string | ||
*/ | ||
public function getNamedContainerIp($name) | ||
{ | ||
$containerManager = $this->client->getContainerManager(); | ||
$containers = $containerManager->findAll(); | ||
|
||
foreach ($containers as $container) { | ||
if ($container->getNames()[0] == "/".$name) { | ||
return $container->getNetworkSettings()->getNetworks()['bridge']->getIPAddress(); | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* @param $name | ||
* | ||
* @return bool | ||
*/ | ||
public function getNamedContainerPorts($name) | ||
{ | ||
$containerManager = $this->client->getContainerManager(); | ||
$containers = $containerManager->findAll(); | ||
|
||
$ports = []; | ||
|
||
foreach ($containers as $container) { | ||
if ($container->getNames()[0] == "/".$name) { | ||
$containerPorts = $container->getPorts(); | ||
foreach ($containerPorts as $port) { | ||
$ports[] = $port->getPrivatePort(); | ||
} | ||
} | ||
} | ||
return $ports; | ||
} | ||
|
||
/** | ||
* @param $name | ||
* | ||
* @return bool | ||
*/ | ||
public function stopNamedContainer($name) | ||
{ | ||
$this->docker("stop $name", false); | ||
} | ||
|
||
/** | ||
* @param $name | ||
* | ||
* @return bool | ||
*/ | ||
public function removeNamedContainer($name) | ||
{ | ||
$this->docker("rm $name", false); | ||
} | ||
|
||
/** | ||
* @param $tag | ||
* | ||
* @return bool | ||
*/ | ||
public function imageExists($tag) | ||
{ | ||
$imageManager = $this->client->getImageManager(); | ||
$images = $imageManager->findAll(); | ||
|
||
foreach ($images as $image) { | ||
if ($image->getRepoTags()[0] == $tag) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param $options | ||
* @param bool $verbose | ||
* | ||
* @return string | ||
*/ | ||
private function docker($options, $verbose = true) | ||
{ | ||
$sudo = $this->sudo ? 'sudo' : ''; | ||
|
||
$process = new Process("$sudo docker $options"); | ||
$process->run(function ($type, $buffer) use ($verbose) { | ||
if (!$verbose) { | ||
return; | ||
} | ||
|
||
if (Process::ERR === $type) { | ||
print $buffer; | ||
} else { | ||
print $buffer; | ||
} | ||
}); | ||
|
||
if (!$process->isSuccessful()) { | ||
throw new ProcessFailedException($process); | ||
} | ||
|
||
return $process->getOutput(); | ||
} | ||
|
||
/** | ||
* @param bool $sudo | ||
* | ||
* @return $this | ||
*/ | ||
public function sudo($sudo = true) | ||
{ | ||
$this->sudo = $sudo; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.