【问题标题】:docker connect to windowsservercore via PowerShell remotingdocker 通过 PowerShell 远程连接到 windowsservercore
【发布时间】:2022-03-29 14:05:18
【问题描述】:

我想测试两个 docker 容器之间的 PS 远程处理。我有以下 DockerFile:

FROM microsoft/windowsservercore:latest

# Set trusted hosts for PS remoting
RUN winrm s winrm/config/client @{TrustedHosts="*"}
# Set password -> just for testing!
RUN net user Administrator 1234!password5678

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Enable PS remoting
RUN Enable-PSRemoting -force; if ($?) {Start-Service winrm}

# Keep container alive if started via docker-compose
CMD start-sleep -seconds 3600

还有下面的docker-compose.yml:

version: '3.1'

services:
   testserver:
      image: 172.23.86.48/myPowerShellImage:latest
      ports:
        - 6985:5985 
        - 6986:5986   

   startpowershelltests:
      image: 172.23.86.48/myPowerShellImage:latest
      ports:
       - 7985:5985 
       - 7986:5986   
      depends_on: 
       - testserver

我通过docker-compose up -d 启动容器并通过docker container exec -it powershelltoolsdocker_startpowershelltests_1 powershell 将我附加到一个容器。

在我执行的附加容器中:

 PS C:\> $pw = ConvertTo-SecureString "1234!password5678" -AsPlainText -Force
 PS C:\> $cred = new-object -typename System.Management.Automation.PSCredential  -argumentlist "testserver\Administrator", $pw
 PS C:\> $session = new-pssession -computername testserver -credential $cred

$session = new-pssession -computername testserver -credential $cred 给我以下错误:

new-pssession : [testserver] 连接到远程服务器 testserver 失败 带有以下错误消息:访问被拒绝。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。SSessionOpenFailed 在行:1 字符:12 + $session = new-pssession -computername testserver -credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:Re moteRunspace) [New-PSSession], PSRemotingTransportException + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

因此我检查了目标是否可ping:

 PS C:\> ping testserver

 Pinging testserver [172.21.162.141] with 32 bytes of data:
 Reply from 172.21.162.141: bytes=32 time<1ms TTL=128
 Reply from 172.21.162.141: bytes=32 time=2ms TTL=128
 Reply from 172.21.162.141: bytes=32 time=3ms TTL=128
 Reply from 172.21.162.141: bytes=32 time=1ms TTL=128

 Ping statistics for 172.21.162.141:
     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
 Approximate round trip times in milli-seconds:
     Minimum = 0ms, Maximum = 3ms, Average = 1ms

谁能告诉我我错过了什么。

谢谢

【问题讨论】:

  • 不确定您是否已经解决了这个问题,因为这是一篇较旧的帖子。但是由于您没有在 New-PSSession 中定义端口并且您没有为容器提供不同的网络,您可能正在连接到 5985/5986,这将是您的 Docker 主机而不是 7985/7986 处的容器是否 @987654332 @工作?
  • 这在 docker 主机上对你有用吗?看看这个thread,他们解释了如何测试这个。
  • 您很可能会遇到臭名昭著的“第二跳”问题。见docs.microsoft.com/en-us/powershell/scripting/learn/remoting/…

标签: powershell docker docker-compose


【解决方案1】:

使用

测试它们之间的连接性
Test-WSMan -ComputerName testserver -Port 7986 -credential $cred

要检查的其他事项包括在您的映像中运行 winrm quickconfig,检查 winrm 服务是否在映像中运行,并检查以确保容器的受信任主机列表中的值正确。

Get-Item WSMan:\localhost\Client\TrustedHosts

在 Windows 域中,您通常不需要担心这种信任,但我想您的容器是独立的机器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2015-05-05
    相关资源
    最近更新 更多