remove-netnatstaticmapping:不支持请求的操作。尝试删除 windows docker 容器端口映射时

remove-netnatstaticmapping : The requested operation is not supported. When trying to remove windows docker container port mappings

我一直在努力解决这个问题。在某处,我的 windows 服务器 2016 docker 服务器中似乎遗留了一些旧的端口映射,并且无法将其删除。这是我正在尝试的命令 运行 和错误:

PS C:\Users\...\Desktop> Get-NetNatStaticMapping | Remove-NetNatStaticMapping

Confirm
Are you sure you want to perform this action?
Performing operation Delete on Target H54d664a6-523c-4452-b137-d66701623488;20;0 PolicyStore Local
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a
Remove-NetNatStaticMapping : The requested operation is not supported.
At line:1 char:27
+ Get-NetNatStaticMapping | Remove-NetNatStaticMapping
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (MSFT_NetNatStat...b4d01327e;6...):root/StandardCi...atStaticMapping)
   [Remove-NetNatStaticMapping], CimException
    + FullyQualifiedErrorId : Windows System Error 50,Remove-NetNatStaticMapping

Remove-NetNatStaticMapping : The requested operation is not supported.
At line:1 char:27
+ Get-NetNatStaticMapping | Remove-NetNatStaticMapping
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (MSFT_NetNatStat...701623488;2...):root/StandardCi...atStaticMapping)
   [Remove-NetNatStaticMapping], CimException
    + FullyQualifiedErrorId : Windows System Error 50,Remove-NetNatStaticMapping

下面的命令显示了两个端口映射...

PS C:\Users\...\Desktop> Get-NetNatStaticMapping


StaticMappingID               : 20
NatName                       : H54d664a6-523c-4452-b137-d66701623488
Protocol                      : TCP
RemoteExternalIPAddressPrefix : 0.0.0.0/0
ExternalIPAddress             : 0.0.0.0
ExternalPort                  : 443
InternalIPAddress             : 172.20.95.205
InternalPort                  : 443
InternalRoutingDomainId       : {00000000-0000-0000-0000-000000000000}
Active                        : False

StaticMappingID               : 6
NatName                       : H68dfe202-0efd-480b-b78a-53ab4d01327e
Protocol                      : TCP
RemoteExternalIPAddressPrefix : 0.0.0.0/0
ExternalIPAddress             : 0.0.0.0
ExternalPort                  : 443
InternalIPAddress             : 172.31.142.210
InternalPort                  : 443
InternalRoutingDomainId       : {00000000-0000-0000-0000-000000000000}
Active                        : False

我在 windows 服务器 2016 上自动卸载并重新安装了 docker,并尝试了所有常用技巧。

以下是我用来在气隙服务器上卸载 docker 的命令:

docker rm -f $(docker ps --all --quiet)
docker system prune --all --volumes -f

Stop-Service docker -Verbose
Stop-Service hns -Verbose

Get-ContainerNetwork | Remove-ContainerNetwork
Get-NetNat | Remove-NetNat -Confirm:$false
Get-VMSwitch -name nat | Remove-VMSwitch -Confirm:$false

Remove-Item -Path "C:\ProgramData\Docker" -Recurse -Force
Remove-WindowsFeature -Name Containers -Verbose
Remove-WindowsFeature -Name Hyper-V -Verbose

Remove-Item C:\ProgramData\Microsoft\Windows\HNS\hns.data -Force

即使我仍然无法删除端口映射,稍后当我尝试 运行 另一个 docker 将端口 443 转发到容器的图像时,我收到错误消息:

failed to create endpoint 
container_name on network nat: HNS failed with error : The object already exists. 

我开始认为我唯一的选择是将服务器恢复到本周早些时候的某个时间点。

非常感谢任何帮助!

为了解决这个问题...删除以下注册表项:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nsi\{eb004a20-9b1a-11d4-9123-0050047759bc}

删除密钥后,我会从命令中得到空结果:

Get-NetNat
Get-NetNatStaticMapping

作为参考,答案来自博客 post 上的讨论:

https://www.thomasmaurer.ch/2016/05/set-up-a-hyper-v-virtual-switch-using-a-nat-network/

在此评论中:

i was able to solve the “Invalid Operation”-Error. Actually, the Mircosoft Consultant that i share my office with, was able to track this down ;-) To remove the NetNat, that i couldn´t remove with the powershell-command, i had to delete the following registry-key:

HKLM\System\CurrentControlSet\Control\NSI{eb004a20-…..7759bc}\….

The key was the same on two different machines, so this might be always the same GUID. After deletion, the NetNat was gone and i was finally able to create a new one. This then worked as expected.

Best regards,

Sebastian