Skip to content

Commit ae0b899

Browse files
update
1 parent 09a28a4 commit ae0b899

File tree

2 files changed

+441
-0
lines changed

2 files changed

+441
-0
lines changed

src/Docker.php

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
namespace idrislab\DockerServices;
4+
5+
use Docker\DockerClient;
6+
use Symfony\Component\Process\Exception\ProcessFailedException;
7+
use Symfony\Component\Process\Process;
8+
9+
/**
10+
* Class Docker
11+
*
12+
* @package idrislab\DockerServices
13+
*/
14+
class Docker
15+
{
16+
/**
17+
* @var bool
18+
*/
19+
private $sudo = false;
20+
/**
21+
* @var \Docker\Docker
22+
*/
23+
private $client;
24+
25+
/**
26+
* Docker constructor.
27+
*
28+
* @param \Docker\Docker $docker
29+
*/
30+
public function __construct(\Docker\Docker $docker)
31+
{
32+
$client = new DockerClient([
33+
'remote_socket' => getenv('DOCKER_SOCKET') ? : 'unix:///var/run/docker.sock',
34+
]);
35+
36+
$this->client= new $docker($client);
37+
}
38+
39+
/**
40+
* @param $image
41+
* @param bool $verbose
42+
*
43+
* @return string
44+
*/
45+
public function pull($image, $verbose = true)
46+
{
47+
return $this->docker("pull $image", $verbose);
48+
}
49+
50+
/**
51+
* @param $options
52+
* @param bool $verbose
53+
*
54+
* @return string
55+
*/
56+
public function run($options, $verbose = false)
57+
{
58+
return $this->docker("run $options", $verbose);
59+
}
60+
61+
/**
62+
* @param $name
63+
*
64+
* @return bool
65+
*/
66+
public function isNamedContainerRunning($name)
67+
{
68+
$containerManager = $this->client->getContainerManager();
69+
$containers = $containerManager->findAll();
70+
71+
foreach ($containers as $container) {
72+
if ($container->getNames()[0] == "/".$name && $container->getState() == "running") {
73+
return true;
74+
}
75+
}
76+
return false;
77+
}
78+
79+
/**
80+
* @param $name
81+
*
82+
* @return string
83+
*/
84+
public function getNamedContainerIp($name)
85+
{
86+
$containerManager = $this->client->getContainerManager();
87+
$containers = $containerManager->findAll();
88+
89+
foreach ($containers as $container) {
90+
if ($container->getNames()[0] == "/".$name) {
91+
return $container->getNetworkSettings()->getNetworks()['bridge']->getIPAddress();
92+
}
93+
}
94+
95+
return '';
96+
}
97+
98+
/**
99+
* @param $name
100+
*
101+
* @return bool
102+
*/
103+
public function getNamedContainerPorts($name)
104+
{
105+
$containerManager = $this->client->getContainerManager();
106+
$containers = $containerManager->findAll();
107+
108+
$ports = [];
109+
110+
foreach ($containers as $container) {
111+
if ($container->getNames()[0] == "/".$name) {
112+
$containerPorts = $container->getPorts();
113+
foreach ($containerPorts as $port) {
114+
$ports[] = $port->getPrivatePort();
115+
}
116+
}
117+
}
118+
return $ports;
119+
}
120+
121+
/**
122+
* @param $name
123+
*
124+
* @return bool
125+
*/
126+
public function stopNamedContainer($name)
127+
{
128+
$this->docker("stop $name", false);
129+
}
130+
131+
/**
132+
* @param $name
133+
*
134+
* @return bool
135+
*/
136+
public function removeNamedContainer($name)
137+
{
138+
$this->docker("rm $name", false);
139+
}
140+
141+
/**
142+
* @param $tag
143+
*
144+
* @return bool
145+
*/
146+
public function imageExists($tag)
147+
{
148+
$imageManager = $this->client->getImageManager();
149+
$images = $imageManager->findAll();
150+
151+
foreach ($images as $image) {
152+
if ($image->getRepoTags()[0] == $tag) {
153+
return true;
154+
}
155+
}
156+
return false;
157+
}
158+
159+
/**
160+
* @param $options
161+
* @param bool $verbose
162+
*
163+
* @return string
164+
*/
165+
private function docker($options, $verbose = true)
166+
{
167+
$sudo = $this->sudo ? 'sudo' : '';
168+
169+
$process = new Process("$sudo docker $options");
170+
$process->run(function ($type, $buffer) use ($verbose) {
171+
if (!$verbose) {
172+
return;
173+
}
174+
175+
if (Process::ERR === $type) {
176+
print $buffer;
177+
} else {
178+
print $buffer;
179+
}
180+
});
181+
182+
if (!$process->isSuccessful()) {
183+
throw new ProcessFailedException($process);
184+
}
185+
186+
return $process->getOutput();
187+
}
188+
189+
/**
190+
* @param bool $sudo
191+
*
192+
* @return $this
193+
*/
194+
public function sudo($sudo = true)
195+
{
196+
$this->sudo = $sudo;
197+
198+
return $this;
199+
}
200+
}

0 commit comments

Comments
 (0)