1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| function Manage-DockerContainer { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateSet('Create', 'Start', 'Stop', 'Remove', 'Restart', 'Pause', 'Resume')] [string]$Action, [Parameter(Mandatory = $true)] [string]$ContainerName, [Parameter()] [string]$Image, [Parameter()] [string[]]$PortMappings, [Parameter()] [string[]]$EnvironmentVariables, [Parameter()] [string[]]$Volumes, [Parameter()] [string]$Network, [Parameter()] [hashtable]$ResourceLimits, [Parameter()] [switch]$AutoRemove, [Parameter()] [switch]$Detached ) try { $containerConfig = [PSCustomObject]@{ Name = $ContainerName Action = $Action Timestamp = Get-Date Status = "Pending" Details = @{} } switch ($Action) { 'Create' { if (-not $Image) { throw "创建容器时必须指定镜像" } $dockerArgs = @( "create" "--name", $ContainerName ) if ($PortMappings) { foreach ($mapping in $PortMappings) { $dockerArgs += "-p", $mapping } } if ($EnvironmentVariables) { foreach ($env in $EnvironmentVariables) { $dockerArgs += "-e", $env } } if ($Volumes) { foreach ($volume in $Volumes) { $dockerArgs += "-v", $volume } } if ($Network) { $dockerArgs += "--network", $Network } if ($ResourceLimits) { if ($ResourceLimits.ContainsKey('Memory')) { $dockerArgs += "--memory", $ResourceLimits.Memory } if ($ResourceLimits.ContainsKey('CpuShares')) { $dockerArgs += "--cpu-shares", $ResourceLimits.CpuShares } } if ($AutoRemove) { $dockerArgs += "--rm" } if ($Detached) { $dockerArgs += "-d" } $dockerArgs += $Image $result = docker $dockerArgs $containerConfig.Details.Result = $result $containerConfig.Status = "Created" } 'Start' { $result = docker start $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Started" } 'Stop' { $result = docker stop $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Stopped" } 'Remove' { $result = docker rm -f $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Removed" } 'Restart' { $result = docker restart $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Restarted" } 'Pause' { $result = docker pause $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Paused" } 'Resume' { $result = docker unpause $ContainerName $containerConfig.Details.Result = $result $containerConfig.Status = "Resumed" } } $logEntry = [PSCustomObject]@{ Timestamp = Get-Date Action = "Docker容器操作" Container = $ContainerName Operation = $Action Config = $containerConfig } Write-Host "容器操作完成:$($logEntry | ConvertTo-Json)" return $containerConfig } catch { Write-Error "Docker容器操作失败:$_" return $null } }
|