Mat*_*adt 5 networking port-forwarding container docker windows-server-2016
我在Windows 主机 (Windows Server 2016 TP4)上运行Windows 容器。
容器应在内部端口 80 上运行IIS 网络服务器,我还想将端口 80 绑定到主机,以便我可以通过主机 IP/URL 访问它。
我按照 Microsoft 的说明进行操作
我通过 Powershell 和 Docker 尝试了这两种方法,在这两种情况下,绑定到主机的端口都不起作用。
========================== Powershell 方法 ======================== ===
将容器主机部署到现有系统(Windows Server 2016 TP4)
PS C:> wget -uri https://aka.ms/tp4/Install-ContainerHost -OutFile C:\Install-ContainerHost.ps1
PS C:> powershell.exe -NoProfile C:\Install-ContainerHost.ps1
Querying status of Windows feature: Containers...
Feature Containers is already enabled.
Waiting for Hyper-V Management...
Networking is already configured. Confirming configuration...
Getting Container OS image (NanoServer) version 10.0.10586.0 from OneGet (this may take a few minutes)...
Container base image install complete. Querying container images...
OS image (NanoServer) is already installed.
The following images are present on this machine:
ContainerImage (Name = 'NanoServer') [Publisher = 'CN=Microsoft', Version = '10.0.10586.0']
ContainerImage (Name = 'WindowsServerCore') [Publisher = 'CN=Microsoft', Version = '10.0.10586.0']
Docker is already installed.
Stopping Docker...
Starting Docker...
Tagging new base image (8572198a60f1)...
Base image is now tagged:
nanoserver 10.0.10586.0 8572198a60f1 5 months ago 0 B
nanoserver latest 8572198a60f1 5 months ago 0 B
Script complete!
Run Code Online (Sandbox Code Playgroud)
准备运行 IIS 的镜像和容器(基于 WindowsServerCore 镜像)
这些是https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/manage_powershell 上的 Microsoft 文档中描述的确切步骤。我从 WindowsServerCore 创建一个容器,在其上安装 IIS,并从中制作一个新映像,然后我可以重用它。
PS C:> Get-ContainerImage
Name Publisher Version IsOSImage
---- --------- ------- ---------
NanoServer CN=Microsoft 10.0.10586.0 True
WindowsServerCore CN=Microsoft 10.0.10586.0 True
PS C:\> New-Container -Name TP4Demo -ContainerImageName WindowsServerCore -SwitchName "Virtual Switch"
Name State Uptime ParentImageName
---- ----- ------ ---------------
TP4Demo Off 00:00:00 WindowsServerCore
PS C:\> Get-Container
Name State Uptime ParentImageName
---- ----- ------ ---------------
TP4Demo Off 00:00:00 WindowsServerCore
PS C:\> Start-Container -Name TP4Demo
PS C:\> Enter-PSSession -ContainerName TP4Demo -RunAsAdministrator
[TP4Demo]: PS C:\Windows\system32> Install-WindowsFeature web-server
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Common HTTP Features, Default Document, D...
[TP4Demo]: PS C:\Windows\system32> exit
PS C:\> Stop-Container -Name TP4Demo
PS C:\> New-ContainerImage -ContainerName TP4Demo -Name WindowsServerCoreIIS -Publisher Demo -Version 1.0
Name Publisher Version IsOSImage
---- --------- ------- ---------
WindowsServerCoreIIS CN=Demo 1.0.0.0 False
PS C:\> Remove-Container -Name TP4Demo -Force
Run Code Online (Sandbox Code Playgroud)
现在我准备好了一个 IIS 容器,可以绑定到“虚拟交换机”。
PS C:\> New-Container -Name IIS -ContainerImageName WindowsServerCoreIIS -SwitchName "Virtual Switch"
Name State Uptime ParentImageName
---- ----- ------ ---------------
IIS Off 00:00:00 WindowsServerCoreIIS
PS C:\> Start-Container -Name IIS
PS C:\> Invoke-Command -ContainerName IIS {ipconfig}
Windows IP Configuration
Ethernet adapter vEthernet (Virtual Switch-30179F35-A9BD-4231-B264-BDD2994BD956-0):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::24f4:c726:ed9b:e603%28
IPv4 Address. . . . . . . . . . . : 172.16.0.2
Subnet Mask . . . . . . . . . . . : 255.240.0.0
Default Gateway . . . . . . . . . : 172.16.0.1
Run Code Online (Sandbox Code Playgroud)
添加端口映射和防火墙规则:
PS C:\> if (!(Get-NetNatStaticMapping | where {$_.ExternalPort -eq 80})) {Add-NetNatStaticMapping -NatName "ContainerNat" -Protocol TCP -ExternalIPAddress 0.0.0.0 -InternalIPAddress 172.16.0.2 -InternalPort 80 -ExternalPort 80}
PS C:\> if (!(Get-NetFirewallRule | where {$_.Name -eq "TCP80"})) {New-NetFirewallRule -Name "TCP80" -DisplayName "HTTP on TCP/80" -Protocol tcp -LocalPort 80 -Action Allow -Enabled True}
Run Code Online (Sandbox Code Playgroud)
现在我添加了端口映射(和防火墙规则),我应该能够通过我的主机访问 IIS。(可以肯定的是,我完全禁用了主机上的防火墙。)
但是主机端口绑定不起作用。我无法通过主机 IP 和绑定端口通过http://localhost:80/或http://172.16.0.1:80/或http://10.10.0.79:80/访问 IIS
PS C:\> wget http://10.10.0.79:80/
wget : Unable to connect to the remote server
At line:1 char:1
+ wget http://10.10.0.79:80/
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\> wget http://172.16.0.1:80/
wget : Unable to connect to the remote server
At line:1 char:1
+ wget http://172.16.0.1:80/
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)
我只能通过容器 IP ( http://172.16.0.2:80 )访问 IIS 默认页面。
======================== Docker 方法 ======================== ===
这是我使用 Docker管理容器的方法:
C:\> docker run --name iisbase -it windowsservercore cmd
C:\> powershell.exe Install-WindowsFeature web-server
C:\> exit
PS C:\Windows\system32> docker commit iisbase windowsservercoreiis
64271b60a1c4af29ce37ebcee45b00d824883eb67c717d4cee765d9f696867bb
C:\> powershell.exe "if(!(Get-NetFirewallRule | where {$_.Name -eq 'TCP80'})) { New-NetFirewallRule -Name 'TCP80' -DisplayName 'HTTP on TCP/80' -Protocol tcp -LocalPort 80 -Action Allow -Enabled True }"
C:\> docker run --name iisdemo -it -p 80:80 windowsservercoreiis cmd
Run Code Online (Sandbox Code Playgroud)
最后,我只能通过容器 IP 访问 IIS,而不能通过主机 IP。
我正在使用 Docker 版本 1.10.0-dev,构建 18c9fe0。
似乎是 Windows Server TP4 的问题。
Docker 团队的 Stefan Scherer 回复了我报告的问题: https://github.com/docker/docker/issues/21558#issuecomment-202536462
我可以重现@mathiasconradt 的问题。上周使用 TP4 玩了投票应用程序,我有相同的解决方法:打开主机上的防火墙端口,使用容器的 IP 地址打开 Web 服务器 url。迫不及待地想在 TP5 上测试投票应用程序。
等待 TP5...同时我在主机上使用 Apache httpd 来处理端口转发。
归档时间: |
|
查看次数: |
3608 次 |
最近记录: |