fix docker container ports

This commit is contained in:
Bozhidar 2024-04-27 01:01:06 +03:00
parent a8c309dac8
commit 8da3de59ca
4 changed files with 62 additions and 21 deletions

View file

@ -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;

View file

@ -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');

View file

@ -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),

View file

@ -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
}
'