From 8da3de59cae2b9447051739231128e0e283a552b Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Sat, 27 Apr 2024 01:01:06 +0300 Subject: [PATCH] fix docker container ports --- .../Docker/App/Models/DockerContainer.php | 10 ++++ web/Modules/Docker/DockerContainerApi.php | 46 +++++++++++-------- .../Resources/DockerContainerResource.php | 13 +++++- .../shell-scripts/get-available-port.sh | 14 ++++++ 4 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 web/Modules/Docker/shell-scripts/get-available-port.sh diff --git a/web/Modules/Docker/App/Models/DockerContainer.php b/web/Modules/Docker/App/Models/DockerContainer.php index d54872a..e268599 100644 --- a/web/Modules/Docker/App/Models/DockerContainer.php +++ b/web/Modules/Docker/App/Models/DockerContainer.php @@ -4,6 +4,7 @@ namespace Modules\Docker\App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Support\Str; use Modules\Docker\DockerApi; use Modules\Docker\DockerContainerApi; @@ -32,6 +33,7 @@ class DockerContainer extends Model 'memory_limit', 'unlimited_memory', 'automatic_start', + 'port', 'external_port', 'volume_mapping', 'environment_variables', @@ -49,6 +51,12 @@ class DockerContainer extends Model static::creating(function ($model) { + $nextId = 1; + $getLastContainer = DockerContainer::latest()->first(); + if ($getLastContainer) { + $nextId = $getLastContainer->id + 1; + } + $dockerContainerApi = new DockerContainerApi(); $dockerContainerApi->setImage($model->image); $dockerContainerApi->setEnvironmentVariables($model->environment_variables); @@ -57,7 +65,9 @@ class DockerContainer extends Model // $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory); // $dockerContainerApi->setAutomaticStart($model->automatic_start); $dockerContainerApi->setPort($model->port); + $dockerContainerApi->setName(Str::slug($model->name.'-phyre-'.$nextId)); $dockerContainerApi->setExternalPort($model->external_port); + $createContainer = $dockerContainerApi->run(); if (!isset($createContainer['ID'])) { return false; diff --git a/web/Modules/Docker/DockerContainerApi.php b/web/Modules/Docker/DockerContainerApi.php index 19a892c..eb3510b 100644 --- a/web/Modules/Docker/DockerContainerApi.php +++ b/web/Modules/Docker/DockerContainerApi.php @@ -6,6 +6,7 @@ use function AlibabaCloud\Client\json; class DockerContainerApi { + public $name = ''; public $image = ''; public $environmentVariables = []; public $volumeMapping = []; @@ -13,6 +14,12 @@ class DockerContainerApi public $port = ''; public $externalPort = ''; + public function setName($name) + { + $name = trim($name); + $this->name = $name; + } + public function setImage($image) { $this->image = $image; @@ -30,11 +37,13 @@ class DockerContainerApi public function setPort($port) { + $port = trim($port); $this->port = $port; } public function setExternalPort($externalPort) { + $externalPort = trim($externalPort); $this->externalPort = $externalPort; } @@ -49,32 +58,29 @@ class DockerContainerApi public function run() { $commandId = rand(10000, 99999); - $commands = []; - $commands[] = 'docker run -d ' . $this->image; + + $shellFileContent = 'docker run --name ' . $this->name . ' '; if (!empty($this->port)) { - $commands[] = '-p ' . $this->port . ':' . $this->externalPort; + $shellFileContent .= ' -p ' . $this->externalPort . ':' . $this->port . ' '; } + $shellFileContent .= '-d ' . $this->image . ' '; - if (!empty($this->environmentVariables)) { - foreach ($this->environmentVariables as $key => $value) { - $commands[] = '-e ' . $key . '=' . $value; - } - } +// if (!empty($this->environmentVariables)) { +// foreach ($this->environmentVariables as $key => $value) { +// $commands[] = '-e ' . $key . '=' . $value; +// } +// } +// +// if (!empty($this->volumeMapping)) { +// foreach ($this->volumeMapping as $key => $value) { +// $commands[] = '-v ' . $key . ':' . $value; +// } +// } - if (!empty($this->volumeMapping)) { - foreach ($this->volumeMapping as $key => $value) { - $commands[] = '-v ' . $key . ':' . $value; - } - } + $shellFileContent .= PHP_EOL . 'rm -f /tmp/docker-run-container-'.$commandId.'.sh'; - $shellFileContent = ''; - - foreach ($commands as $command) { - $shellFileContent .= $command . PHP_EOL; - } - - $shellFileContent .= 'rm -f /tmp/docker-run-container-'.$commandId.'.sh'; + // dd($shellFileContent); file_put_contents('/tmp/docker-run-container-'.$commandId.'.sh', $shellFileContent); $output = shell_exec('bash /tmp/docker-run-container-'.$commandId.'.sh'); diff --git a/web/Modules/Docker/Filament/Clusters/Docker/Resources/DockerContainerResource.php b/web/Modules/Docker/Filament/Clusters/Docker/Resources/DockerContainerResource.php index 833ef63..084b05d 100644 --- a/web/Modules/Docker/Filament/Clusters/Docker/Resources/DockerContainerResource.php +++ b/web/Modules/Docker/Filament/Clusters/Docker/Resources/DockerContainerResource.php @@ -46,6 +46,15 @@ class DockerContainerResource extends Resource } } $defaultPort = ''; + $defaultExternalPort = 8010; + + // Get available ports from linux server + $getAvailablePortShellFile = module_path('Docker', 'shell-scripts/get-available-port.sh'); + $availablePort = shell_exec("sh $getAvailablePortShellFile"); + if (is_numeric($availablePort)) { + $defaultExternalPort = $availablePort; + } + if (isset($dockerImageInspect['Config']['ExposedPorts'])) { foreach ($dockerImageInspect['Config']['ExposedPorts'] as $port => $value) { $port = str_replace('/tcp', '', $port); @@ -78,12 +87,14 @@ class DockerContainerResource extends Resource Forms\Components\TextInput::make('port') ->label('Port') + // ->disabled() ->default($defaultPort) ->columnSpan(1), Forms\Components\TextInput::make('external_port') ->label('External Port') - ->default($defaultPort) + // ->disabled() + ->default($defaultExternalPort) ->columnSpan(1), diff --git a/web/Modules/Docker/shell-scripts/get-available-port.sh b/web/Modules/Docker/shell-scripts/get-available-port.sh new file mode 100644 index 0000000..c979e08 --- /dev/null +++ b/web/Modules/Docker/shell-scripts/get-available-port.sh @@ -0,0 +1,14 @@ +netstat -aln | awk ' + $6 == "LISTEN" { + if ($4 ~ "[.:][0-9]+$") { + split($4, a, /[:.]/); + port = a[length(a)]; + p[port] = 1 + } + } + END { + for (i = 3000; i < 65000 && p[i]; i++){}; + if (i == 65000) {exit 1}; + print i + } +'