Skip to content

Commit

Permalink
Add check if Docker is available
Browse files Browse the repository at this point in the history
  • Loading branch information
mischabraam committed Jan 28, 2024
1 parent e34b30b commit 11c29f7
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions cli/ValetPlus/AbstractDockerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,29 @@ public function __construct(
}

/**
* Returns a collection of names of the running docker containers.
* Checks if Docker is available on the system.
*
* @return bool Returns true if Docker is available, false otherwise.
*/
protected function isDockerAvailable(): bool
{
$command = 'command -v docker';
$output = $this->cli->runAsUser($command);

return !empty($output);
}

/**
* Returns a collection of names of all running Docker containers.
*
* @return Collection
*/
public function getAllRunningContainers(): Collection
{
if (!$this->isDockerAvailable()) {
return collect([]);
}

$command = 'docker ps --format=\'{{.Names}}\'';
$onError = function ($exitCode, $errorOutput) {
warning($errorOutput);
Expand All @@ -57,14 +74,18 @@ public function getAllRunningContainers(): Collection
}

/**
* Runs a docker command from the path where the docker-compose.yml is located.
* Runs a Docker command from the path where the docker-compose.yml is located.
*
* @param $command
* @param $dir
* @return $this
*/
public function runCommand($command, $dir)
public function runCommand($command, $dir): self
{
if (!$this->isDockerAvailable()) {
return $this;
}

$onError = function ($exitCode, $errorOutput) {
warning($errorOutput);
};
Expand All @@ -78,13 +99,17 @@ public function runCommand($command, $dir)
}

/**
* Starts the docker container by the service's name. Creates the container first, if it doesn't exist yet.
* Starts the Docker container by the service's name. Creates the container first, if it doesn't exist yet.
*
* @param $name
* @return $this
*/
public function upContainer($name)
public function upContainer($name): self
{
if (!$this->isDockerAvailable()) {
return $this;
}

info("Docker up version {$name}...");
$installPath = $this->getComposeInstallPath($name);
$installDir = $this->getComposeInstallDir($name);
Expand All @@ -108,13 +133,17 @@ public function upContainer($name)
}

/**
* Stops the docker container by the service's name.
* Stops the Docker container by the service's name.
*
* @param $name
* @return $this
*/
public function stopContainer($name)
public function stopContainer($name): self
{
if (!$this->isDockerAvailable()) {
return $this;
}

info("Docker stop version {$name}...");
$this->runCommand(
'docker compose stop',
Expand All @@ -130,8 +159,12 @@ public function stopContainer($name)
* @param $name
* @return $this
*/
public function downContainer($name)
public function downContainer($name): self
{
if (!$this->isDockerAvailable()) {
return $this;
}

info("Docker down version {$name}...");
$this->runCommand(
'docker compose down --volumes --rmi all',
Expand Down

0 comments on commit 11c29f7

Please sign in to comment.