Skip to content

Instantly share code, notes, and snippets.

@takali
Created March 25, 2021 14:40
Show Gist options
  • Save takali/63ace9fd56c3d8ff6a7b5da096bf457e to your computer and use it in GitHub Desktop.
Save takali/63ace9fd56c3d8ff6a7b5da096bf457e to your computer and use it in GitHub Desktop.
Laravel command - list all docker container and display command to connect
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
// package symfony/yaml is require
class ContainerConnect extends Command
{
/**
* @var Parser
*/
protected $yamlParser;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'container:connect';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Liste all containers and command to connect';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->yamlParser = new Parser();
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$services = [];
$parser = $this->yamlParser->parseFile('./docker-compose.yml');
foreach ($parser['services'] as $key => $value) {
$containerName = $value['container_name'] ?? $key;
$services[$key] = $containerName;
}
$container = $this->choice('Select container to connect', $services);
$this->info(sprintf('docker exec -ti %s /bin/bash', $services[$container]));
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment