Back to Blog

Beginners’ Guide for Microsoft Hyper-V: How to run IIS in Docker Container – Part 38

You can run IIS as a Windows container on Docker by writing a docker file based on the mcr.microsoft.com/windows/servercore/iis image, then building, running, and managing it just like a traditional...
Choose your BDRShield Management Console - Cloud or On-Premise:
Hybrid Storage (Local & Cloud)30-Day Free TrialFull-Feature Access
Not sure which console fits your needs? Request Demo ?
By Brandon Lee | May 23, 2024

TL;DR

You can run IIS as a Windows container on Docker by writing a docker file based on the mcr.microsoft.com/windows/servercore/iis image, then building, running, and managing it just like a traditional IIS site.

  • Write a docker file that sets the base image, enables remote management, and adds an admin user
  • Build the image with docker build, then run it with docker run -d -p 80:80
  • Verify the site loads at the Docker host’s IP or hostname
  • Manage it remotely through IIS Manager using the Docker host and any published management ports, rather than the container’s internal Docker IP
  • Use Hyper-V isolation mode for stronger isolation if needed

We have discussed many aspects of running Docker containers on a Windows Server, including installing Docker. Running an Internet Information Server as a Docker container is a very suitable containerized workload, as many businesses run full virtual machines to host single IIS websites. In this Windows IIS container for beginners’ walkthrough, we will consider pulling down an IIS container and running a simple IIS website using Docker in Windows.

Why run IIS in a Docker container?

For years now, organizations have run IIS websites in full virtual machines. Virtual machines are now considered traditional infrastructure. Businesses are now looking to leverage the benefits of containerized workloads in their environment. The benefits of running applications inside containers are many. Containerized workloads allow applications to be quickly spun up and down as needed.

Containers are scalable, portable, and have all the dependencies included in the container image, making them excellent platforms for running applications like webservers in production.

Creating a docker file to build an IIS container

Let’s step through building a Windows IIS Docker container and see what is involved in this process. First, spinning up a new customized container of any variety is accomplished using a “docker file.” A docker file is a special file that contains instructions for Docker to build your container using the commands specified.
When we build an IIS container, the docker file specifies the base image of the container and any customizations we need to make to the IIS configuration for our Docker image.
To build our IIS container, we will use the following code for the docker file:

# escape=`
FROM mcr.microsoft.com/windows/servercore/iis

# Setup Remote IIS management
RUN powershell Install-WindowsFeature Web-Mgmt-Service; `
New-ItemProperty -Path HKLM:\software\microsoft\WebManagement\Server -Name EnableRemoteManagement -Value 1 -Force; `
Set-Service -Name wmsvc -StartupType automatic;

#Add user for Remote IIS Manager Login

RUN net user iisadmin Password1 /ADD

RUN net localgroup administrators iisadmin /add

RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\*

WORKDIR /inetpub/wwwroot

COPY . /inetpub/wwwroot

RUN dir

In the above docker file directives, we are configuring the container’s base image, mcr.microsoft.com/windows/servercore/iis. Then we configure the container with various configuration commands that do the following:

  • Enabling remote management
  • Setting the WMSVC service to start automatically
  • Adding a remote management user
  • Adding the remote management user to the administrators group
  • Removing any existing files in the inetpub\wwwroot directory
  • Setting the working directory
  • Copying files
  • Running a dir command to list out the files

Building a Windows IIS Docker container

We can build our IIS container image now that we have our docker file. To build the docker image for the IIS docker container, we can issue the following command:

docker build -t .

How to run IIS in Docker Container

Building a new docker IIS container using the customized docker file

The container should build successfully. You can see your new container image in your docker container images using the command:

docker image ls

How to run IIS in Docker Container

Listing docker images to verify the new IIS container is created successfully

Running the new Windows Docker IIS image

Now that we have the new Docker IIS image ready to go, we can run the container. To run a new Docker IIS container, we can use the command:

docker run -d -p 80:80 –name testwebsite testiis/testiis:latest

In the command above, we are telling Docker which port we want to expose externally and where the port translates to on the internal container side. The name parameter allows us to create a friendly name for the new container. Finally, we tell Docker which image we want to use for the container.

How to run IIS in Docker Container

Running the new Docker IIS container

Verifying connectivity to the new IIS website

At this point, we should be able to verify that we have connectivity to the new IIS website in Docker. We can browse to the localhost address, IP address, or hostname of the IIS container and it should pull up the website.

How to run IIS in Docker Container

Verifying the new Docker IIS website

Managing the IIS running in Docker

Now that we have a new IIS website running in Docker, can we manage it with the traditional IIS Manager console? Yes, as we recall, we enabled remote management and created a user for management. We can connect to the IIS site using the IIS Manager console with that information.

First, we need to get the internal Docker IP address for managing the container. To get your IP address for the running IIS container, we need to issue the command:

docker inspect

We should see the network information when we scroll down toward the bottom of the JSON output.

How to run IIS in Docker Container

The IP address of an IIS Docker container in Docker inspect output

With this internal IP address from the Docker host, we can manage the IIS website in Docker. Add a new connection in IIS Manager. Enter the IP address.

How to run IIS in Docker Container

Add a new connection using the Docker IP address in IIS Manager

Use the username and password configured in the Docker file to add the connection.

How to run IIS in Docker Container

Enter the username and password for the Docker IIS container

Name the connection to the Docker IIS container.

How to run IIS in Docker Container

Name the connection for the IIS Docker container

The connection is successfully added to the Docker IIS container.

How to run IIS in Docker Container

The Docker IIS connection is added successfully

Docker IIS container FAQs

What is a Docker file?

A Docker file is a special file that allows building a custom Docker container with the commands, components, and configurations you need in the environment.

Why run IIS websites in Docker?

Running simple IIS websites in full virtual machines is inefficient and not the best use of resources. Instead, you can more efficiently run IIS sites in Docker and have all the prerequisites required bundled in the container without worrying about the VM environment. Having IIS in Docker allows easily moving the website to a new Docker host and scaling the solution if needed.

What does Docker build do?

Docker build takes a Docker file and builds the Docker image specified by the file contents.

Wrapping Up

Running IIS websites in Docker containers is a great way to make the best use of resources and have all the underlying requirements for the website contained in the container. Furthermore, moving the container to another Docker host is easy if needed. In addition, each container is isolated from another. If you want further isolation, you can use Hyper-V isolation mode for your containers for even more robust security.

Read More:
Beginners Guide for Microsoft Hyper-V: Top 8 Basic Docker Commands You Should Know – Part 36
Beginners’ Guide for Microsoft Hyper-V: Windows Docker Container Networking in Hyper-V – Part 37
Hyper-V Mastery: A Step-by-Step Guide for Beginners to Elevate Your IT Skills and Boost Your Career

FAQ

1. What base image does the article use to build the IIS Docker container?

The docker file is based on mcr.microsoft.com/windows/servercore/iis, the Microsoft-maintained Windows Server Core image with IIS pre-installed. This gives the container the IIS role without a full Windows Server VM footprint.

2. How do you enable remote management for an IIS container built this way?

The docker file installs the Web-Mgmt-Service feature, sets a registry value to enable remote management, and configures the WMSVC service to start automatically. It also adds a dedicated Windows user and puts that user in the administrators group so IIS Manager can authenticate remotely.

3. What command exposes the IIS container on port 80?

docker run -d -p 80:80 –name testwebsite testiis/testiis:latest runs the container in detached mode, maps host port 80 to container port 80, names the container, and specifies which image to run.

4. How do you find the internal IP address of a running IIS Docker container?

Run docker inspect against the container and check the network section near the bottom of the JSON output to find the container’s internal IP address.

5. Can you connect to a containerized IIS site with the standard IIS Manager console?

Yes. Using the remote management user and password set in the Dockerfile, you can add a new connection in IIS Manager and manage the containerized site the same way you’d manage a traditional IIS installation, provided IIS Remote Management is enabled and accessible.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Rate this post
Avatar for Brandon Lee

Brandon Lee

Brandon Lee is a guest blogger for Vembu. He has been in the IT industry for over 15+ years now and has worked in various IT industries spanning education, manufacturing, hospitality, and consulting for various technology companies including Fortune 500 companies. Brandon is a prolific blogger and contributes to the community through various blog posts and technical documentation primarily at Virtualizationhowto.com

Go to Top