How to Run IIS Server inside Windows Container

What is a Container ?

Containers are an isolated, resource controlled, and portable operating environment.

A container is an isolated place where an application can run without affecting the rest of the system and without the system affecting the application. Containers are the next evolution in virtualization.

When we enter to a container it feels the same as the freshly installed physical computer or a virtual machine.we can use Docker to manage the windows server containers too.

Windows Container Types

  • Windows Server Containers
    provide application isolation through process and namespace isolation technology. A Windows Server container shares the same kernel with the container host and all containers running on the host.
  • Hyper-V Containers
    expand on the isolation provided by Windows Server Containers by running each container in a highly optimized virtual machine. When using this configuration kernel of the container host is not shared with the Hyper-V Containers.
containerfund
From MSDN Site

Let’s walk through the creation of the windows container in windows server 2016 TP4

In this I use Azure VMs to test this in azure we can deploy a virtual machine with windows server 2016 TP4 image in azure marketplace.If your using Hyper-V or any other platform to test this you have to create a windows server 2016 TP4 VM and configure that VM as this link.

  1. Get the container image
    Get-ContainerImage
    
    containerimage
    Container Images

     

  2. Create a new container from existing container image
     New-Container -Name mycontainer -ContainerImage $containerImage -SwitchName "Virtual Switch"
    
    newcontainer
    New container

     

  3. Start the created container
    Start-Container -Name mycontainer
    
    startcontainer
    Start container

     

  4. Remote connect to container install IIS Web Role
    Enter-PSSession -ContainerName mycontainer -RunAsAdministrator
    Install-WindowsFeature web-server
    
    IIS Install
    IIS Role Installation

     

  5. Stop the container for container image creation
    Stop-Container -Name mycontainer
    
  6. Create new container image include IIS Web Role
    New-ContainerImage -Container $container -Name IISImage -Publisher Demo -Version 1.0.0
    
    iis container image
    Create IIS Container Image

    images.PNG
    Container Images
  7. Create new container using IIS container image
    New-Container -Name IIS -ContainerImage $containerImage -SwitchName 'Virtual Switch' -Verbose
    

    newiiscontainer
    IIS enable container
  8. Start the IIS enable container
    Start-Container IIS
    
  9. Create NAT port mapping to access the IIS server enable container through container host
    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
    }
    
    PortMaptoContainer.PNG
    NAT Port Mapping

     

  10. Add a firewall rule at allow HTTP traffic to container from the container host
    if (!(Get-NetFirewallRule | where {$_.Name -eq "TCP80"})) {
     New-NetFirewallRule -Name 'TCP80' -DisplayName 'HTTP on TCP/80' -Protocol tcp -LocalPort 80 -Action Allow -Enabled True
    }
    
    firewallrule.PNG
    Firewall Rule

     

  11. Remote access to the container and modify the default IIS page
    Enter-PSSession -ContainerName IIS -RunAsAdministrator
    del C:inetpubwwwrootiisstart.htm
    'Hello World From other side to Windows Server Container' >C:inetpubwwwrootindex.html
    
    iispage
    Edit the default page

     

  12. I used another Azure VM connect to same VNet to test the IIS server enter the IP address of the container host and you will see the edited default IIS page.
    Note-If your using Azure VM make sure to allow inbound and outbound traffic for HTTP TCP Port 80 in NSG (Network Security Group)

    iis page
    Default IIS Page

     

Additional Resources 

Windows Containers Documentation
Windows Containers
Container Host Deployment – Windows Server