mirror of
https://github.com/PhyreApps/PhyrePanel.git
synced 2024-11-22 07:30:25 +00:00
fix docker container ports
This commit is contained in:
parent
a8c309dac8
commit
8da3de59ca
4 changed files with 62 additions and 21 deletions
|
@ -4,6 +4,7 @@ namespace Modules\Docker\App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Modules\Docker\DockerApi;
|
use Modules\Docker\DockerApi;
|
||||||
use Modules\Docker\DockerContainerApi;
|
use Modules\Docker\DockerContainerApi;
|
||||||
|
|
||||||
|
@ -32,6 +33,7 @@ class DockerContainer extends Model
|
||||||
'memory_limit',
|
'memory_limit',
|
||||||
'unlimited_memory',
|
'unlimited_memory',
|
||||||
'automatic_start',
|
'automatic_start',
|
||||||
|
'port',
|
||||||
'external_port',
|
'external_port',
|
||||||
'volume_mapping',
|
'volume_mapping',
|
||||||
'environment_variables',
|
'environment_variables',
|
||||||
|
@ -49,6 +51,12 @@ class DockerContainer extends Model
|
||||||
|
|
||||||
static::creating(function ($model) {
|
static::creating(function ($model) {
|
||||||
|
|
||||||
|
$nextId = 1;
|
||||||
|
$getLastContainer = DockerContainer::latest()->first();
|
||||||
|
if ($getLastContainer) {
|
||||||
|
$nextId = $getLastContainer->id + 1;
|
||||||
|
}
|
||||||
|
|
||||||
$dockerContainerApi = new DockerContainerApi();
|
$dockerContainerApi = new DockerContainerApi();
|
||||||
$dockerContainerApi->setImage($model->image);
|
$dockerContainerApi->setImage($model->image);
|
||||||
$dockerContainerApi->setEnvironmentVariables($model->environment_variables);
|
$dockerContainerApi->setEnvironmentVariables($model->environment_variables);
|
||||||
|
@ -57,7 +65,9 @@ class DockerContainer extends Model
|
||||||
// $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory);
|
// $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory);
|
||||||
// $dockerContainerApi->setAutomaticStart($model->automatic_start);
|
// $dockerContainerApi->setAutomaticStart($model->automatic_start);
|
||||||
$dockerContainerApi->setPort($model->port);
|
$dockerContainerApi->setPort($model->port);
|
||||||
|
$dockerContainerApi->setName(Str::slug($model->name.'-phyre-'.$nextId));
|
||||||
$dockerContainerApi->setExternalPort($model->external_port);
|
$dockerContainerApi->setExternalPort($model->external_port);
|
||||||
|
|
||||||
$createContainer = $dockerContainerApi->run();
|
$createContainer = $dockerContainerApi->run();
|
||||||
if (!isset($createContainer['ID'])) {
|
if (!isset($createContainer['ID'])) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -6,6 +6,7 @@ use function AlibabaCloud\Client\json;
|
||||||
|
|
||||||
class DockerContainerApi
|
class DockerContainerApi
|
||||||
{
|
{
|
||||||
|
public $name = '';
|
||||||
public $image = '';
|
public $image = '';
|
||||||
public $environmentVariables = [];
|
public $environmentVariables = [];
|
||||||
public $volumeMapping = [];
|
public $volumeMapping = [];
|
||||||
|
@ -13,6 +14,12 @@ class DockerContainerApi
|
||||||
public $port = '';
|
public $port = '';
|
||||||
public $externalPort = '';
|
public $externalPort = '';
|
||||||
|
|
||||||
|
public function setName($name)
|
||||||
|
{
|
||||||
|
$name = trim($name);
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
public function setImage($image)
|
public function setImage($image)
|
||||||
{
|
{
|
||||||
$this->image = $image;
|
$this->image = $image;
|
||||||
|
@ -30,11 +37,13 @@ class DockerContainerApi
|
||||||
|
|
||||||
public function setPort($port)
|
public function setPort($port)
|
||||||
{
|
{
|
||||||
|
$port = trim($port);
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setExternalPort($externalPort)
|
public function setExternalPort($externalPort)
|
||||||
{
|
{
|
||||||
|
$externalPort = trim($externalPort);
|
||||||
$this->externalPort = $externalPort;
|
$this->externalPort = $externalPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,32 +58,29 @@ class DockerContainerApi
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$commandId = rand(10000, 99999);
|
$commandId = rand(10000, 99999);
|
||||||
$commands = [];
|
|
||||||
$commands[] = 'docker run -d ' . $this->image;
|
$shellFileContent = 'docker run --name ' . $this->name . ' ';
|
||||||
|
|
||||||
if (!empty($this->port)) {
|
if (!empty($this->port)) {
|
||||||
$commands[] = '-p ' . $this->port . ':' . $this->externalPort;
|
$shellFileContent .= ' -p ' . $this->externalPort . ':' . $this->port . ' ';
|
||||||
}
|
}
|
||||||
|
$shellFileContent .= '-d ' . $this->image . ' ';
|
||||||
|
|
||||||
if (!empty($this->environmentVariables)) {
|
// if (!empty($this->environmentVariables)) {
|
||||||
foreach ($this->environmentVariables as $key => $value) {
|
// foreach ($this->environmentVariables as $key => $value) {
|
||||||
$commands[] = '-e ' . $key . '=' . $value;
|
// $commands[] = '-e ' . $key . '=' . $value;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// if (!empty($this->volumeMapping)) {
|
||||||
|
// foreach ($this->volumeMapping as $key => $value) {
|
||||||
|
// $commands[] = '-v ' . $key . ':' . $value;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
if (!empty($this->volumeMapping)) {
|
$shellFileContent .= PHP_EOL . 'rm -f /tmp/docker-run-container-'.$commandId.'.sh';
|
||||||
foreach ($this->volumeMapping as $key => $value) {
|
|
||||||
$commands[] = '-v ' . $key . ':' . $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$shellFileContent = '';
|
// dd($shellFileContent);
|
||||||
|
|
||||||
foreach ($commands as $command) {
|
|
||||||
$shellFileContent .= $command . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
$shellFileContent .= 'rm -f /tmp/docker-run-container-'.$commandId.'.sh';
|
|
||||||
|
|
||||||
file_put_contents('/tmp/docker-run-container-'.$commandId.'.sh', $shellFileContent);
|
file_put_contents('/tmp/docker-run-container-'.$commandId.'.sh', $shellFileContent);
|
||||||
$output = shell_exec('bash /tmp/docker-run-container-'.$commandId.'.sh');
|
$output = shell_exec('bash /tmp/docker-run-container-'.$commandId.'.sh');
|
||||||
|
|
|
@ -46,6 +46,15 @@ class DockerContainerResource extends Resource
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$defaultPort = '';
|
$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'])) {
|
if (isset($dockerImageInspect['Config']['ExposedPorts'])) {
|
||||||
foreach ($dockerImageInspect['Config']['ExposedPorts'] as $port => $value) {
|
foreach ($dockerImageInspect['Config']['ExposedPorts'] as $port => $value) {
|
||||||
$port = str_replace('/tcp', '', $port);
|
$port = str_replace('/tcp', '', $port);
|
||||||
|
@ -78,12 +87,14 @@ class DockerContainerResource extends Resource
|
||||||
|
|
||||||
Forms\Components\TextInput::make('port')
|
Forms\Components\TextInput::make('port')
|
||||||
->label('Port')
|
->label('Port')
|
||||||
|
// ->disabled()
|
||||||
->default($defaultPort)
|
->default($defaultPort)
|
||||||
->columnSpan(1),
|
->columnSpan(1),
|
||||||
|
|
||||||
Forms\Components\TextInput::make('external_port')
|
Forms\Components\TextInput::make('external_port')
|
||||||
->label('External Port')
|
->label('External Port')
|
||||||
->default($defaultPort)
|
// ->disabled()
|
||||||
|
->default($defaultExternalPort)
|
||||||
->columnSpan(1),
|
->columnSpan(1),
|
||||||
|
|
||||||
|
|
||||||
|
|
14
web/Modules/Docker/shell-scripts/get-available-port.sh
Normal file
14
web/Modules/Docker/shell-scripts/get-available-port.sh
Normal 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
|
||||||
|
}
|
||||||
|
'
|
Loading…
Reference in a new issue