Skip to content

Commit 4c7d838

Browse files
committed
feat(devops): 添加容器服务访问信息显示功能
新增 Get-LanIpAddress 函数获取本地局域网 IP 地址 新增 Show-ServiceAccessInfo 函数显示运行中容器的端口映射和访问地址 在容器启动后自动显示本地和局域网访问 URL,提升开发调试体验
1 parent 1b4e9c4 commit 4c7d838

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

scripts/pwsh/devops/start-container.ps1

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,101 @@ function Show-RustDeskInfo {
432432
}
433433

434434

435+
function Get-LanIpAddress {
436+
try {
437+
$hostEntry = [System.Net.Dns]::GetHostEntry([System.Net.Dns]::GetHostName())
438+
$ip = $hostEntry.AddressList |
439+
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
440+
Select-Object -ExpandProperty IPAddressToString -First 1
441+
return $ip
442+
}
443+
catch {
444+
return $null
445+
}
446+
}
447+
448+
function Show-ServiceAccessInfo {
449+
param(
450+
[string[]]$Services,
451+
[string]$ProjectName
452+
)
453+
if (-not $Services) { return }
454+
455+
$lanIp = Get-LanIpAddress
456+
$hasPrintedHeader = $false
457+
458+
foreach ($svc in $Services) {
459+
try {
460+
$psArgs = @('ps', '-q', '--filter', "label=com.docker.compose.service=$svc")
461+
if ($ProjectName) { $psArgs += @('--filter', "label=com.docker.compose.project=$ProjectName") }
462+
463+
$containerIds = & docker @psArgs
464+
if ([string]::IsNullOrWhiteSpace($containerIds)) { continue }
465+
466+
foreach ($cid in ($containerIds -split '\s+')) {
467+
if ([string]::IsNullOrWhiteSpace($cid)) { continue }
468+
469+
$portOutput = & docker port $cid 2>$null
470+
if ([string]::IsNullOrWhiteSpace($portOutput)) { continue }
471+
472+
if (-not $hasPrintedHeader) {
473+
Write-Host "`n=== 服务访问信息 ($ProjectName) ===" -ForegroundColor Cyan
474+
$hasPrintedHeader = $true
475+
}
476+
477+
Write-Host "Service: $svc" -ForegroundColor Green
478+
479+
$lines = $portOutput -split '\r?\n'
480+
foreach ($line in $lines) {
481+
if ($line -match '^(\d+)/(\w+)\s*->\s*(.+):(\d+)$') {
482+
$containerPort = $Matches[1]
483+
$proto = $Matches[2]
484+
$hostIp = $Matches[3]
485+
$hostPort = $Matches[4]
486+
487+
$isBindAll = ($hostIp -eq '0.0.0.0' -or $hostIp -eq '::')
488+
if ($isBindAll) { $hostIp = 'localhost' }
489+
490+
# Helper closure for formatting URL
491+
$formatUrl = {
492+
param($ip, $port, $cPort, $proto)
493+
$u = "${ip}:${port}"
494+
if ($proto -eq 'tcp') {
495+
if ($cPort -eq '80' -or $cPort -match '^(3000|5000|8000|8080|8888|9000|9999)$') {
496+
return "http://$u"
497+
}
498+
elseif ($cPort -eq '443') {
499+
return "https://$u"
500+
}
501+
}
502+
return $u
503+
}
504+
505+
$localUrl = & $formatUrl -ip $hostIp -port $hostPort -cPort $containerPort -proto $proto
506+
Write-Host " - Port $containerPort/$proto"
507+
Write-Host " Local: $localUrl"
508+
509+
if ($isBindAll -and -not [string]::IsNullOrWhiteSpace($lanIp) -and $lanIp -ne '127.0.0.1') {
510+
$lanUrl = & $formatUrl -ip $lanIp -port $hostPort -cPort $containerPort -proto $proto
511+
Write-Host " LAN: $lanUrl"
512+
}
513+
}
514+
else {
515+
Write-Host " - $line"
516+
}
517+
}
518+
}
519+
}
520+
catch {
521+
Write-Verbose "Error getting info for ${svc}: $_"
522+
}
523+
}
524+
if ($hasPrintedHeader) {
525+
Write-Host "================================`n" -ForegroundColor Cyan
526+
}
527+
}
528+
529+
435530
function Get-ComposeServiceNames {
436531
param(
437532
[string]$ComposePath,
@@ -552,6 +647,7 @@ try {
552647
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
553648
}
554649
Show-RustDeskInfo -ServiceName $ServiceName -DataPath $DataPath
650+
Show-ServiceAccessInfo -Services $targetProfiles -ProjectName $projectName
555651
}
556652
return
557653
}
@@ -564,6 +660,7 @@ try {
564660
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
565661
}
566662
Show-RustDeskInfo -ServiceName $ServiceName -DataPath $DataPath
663+
Show-ServiceAccessInfo -Services $targetProfiles -ProjectName $projectName
567664
}
568665
}
569666
else {
@@ -574,6 +671,7 @@ try {
574671
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
575672
}
576673
Show-RustDeskInfo -ServiceName $ServiceName -DataPath $DataPath
674+
Show-ServiceAccessInfo -Services $targetProfiles -ProjectName $projectName
577675
}
578676
}
579677
return
@@ -610,6 +708,7 @@ try {
610708
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
611709
}
612710
Show-RustDeskInfo -ServiceName $ServiceName -DataPath $DataPath
711+
Show-ServiceAccessInfo -Services $targetProfiles -ProjectName $projectName
613712
}
614713
return
615714
}

0 commit comments

Comments
 (0)