docker通过PowerShell远程连接到windowsservercore

Moe*_*ald 5 powershell docker docker-compose

我想测试两个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
Run Code Online (Sandbox Code Playgroud)

以下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
Run Code Online (Sandbox Code Playgroud)

我启动容器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
Run Code Online (Sandbox Code Playgroud)

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

new-pssession:[testserver]连接到远程服务器testserver失败,并显示以下错误消息:拒绝访问.有关详细信息,请参阅about_Remote_Troubleshooting帮助主题.SessionOpenFailed在行:1 char: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
Run Code Online (Sandbox Code Playgroud)

有人能给我一个暗示我错过了什么.

谢谢

小智 1

使用以下命令测试它们之间的连接性

Test-WSMan -ComputerName testserver -Port 7986 -credential $cred
Run Code Online (Sandbox Code Playgroud)

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

Get-Item WSMan:\localhost\Client\TrustedHosts
Run Code Online (Sandbox Code Playgroud)

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