Deploying IIS in a container

In the Deploying a hello-world sample recipe, you downloaded and ran multiple container images. One of those images was the microsoft/iis image. This image contains IIS with only the default website set up and working. When you run this image, IIS is loaded in the container, and with port mapping in place, you can easily see the website in the container, even though IIS is not loaded on the container host.

For this recipe to work as written, the container has to have the same base OS image as your container host. This recipe, therefore, assumes that the host you are using for the recipes in this chapter is 1809 (for both Windows 10 and Server 2019). If you run this recipe on an older OS, for example, 1709 or Server 2016, Docker would not run the container and you get an error, so ensure that the kernel versions on the container host and the container itself are the same (or use Hyper-V isolation).

Getting ready

This recipe uses the CH1 host, which you configured in the Configuring a container host recipe.

How to do it...

  1. Create the C:Reskitapp folder:
    $EA = @{ErrorAction='SilentlyContinue'}
    New-Item -Path C:ReskitApp -ItemType Directory @EA
  2. Create a web page:
    $Fn = 'C:ReskitappIndex.htm'
    $Index = @"
    <!DOCTYPE html>
    <html><head><title>
    ReskitApp Container Application</title></head>
    <body><p><center><b>
    HOME PAGE FOR RESKITAPP APPLICATION</b></p>
    Running in a container in Windows Server 2019<p>
    </center><br><hr></body></html>
    "@
    $Index | Out-File -FilePath $Fn
  3. Get a server core with a server core image (with IIS loaded) from the Docker registry:
    docker pull mcr.microsoft.com/windows/servercore/iis |
      Out-Null
  4. Run the image as a container named rkwebc:
    $image = 'mcr.microsoft.com/windows/servercore/iis'
    docker run -d -p 80:80 --name rkwebc "$image"
  5. Copy our file into the container:
    Set-Location -Path C:Reskitapp
    docker cp .index.htm rkwebc:c:inetpubwwwrootindex.htm
  6. View the page:
    Start-Process "Http://CH1.Reskit.Org/Index.htm"
  7. Clean up:
    docker rm rkwebc -f | Out-Null
    docker image rm  mcr.microsoft.com/windows/servercore/iis | 
      Out-Null

How it works...

In step 1, you create a folder on CH1 to hold a web page. In step 2, you create a very simple home HTML page. In step 3, you download a server core image that contains IIS. These first three steps produce no output.

In step 4, you run the image as a container named rkwebc and bridge the local port 80 to the container's port 80, which looks like this:

How it works...

In step 5, you copy the web page HTML file from CH1 into the container, which produces no output. In step 6, you view the container's web page, which looks like this:

How it works...

Finally, in step 7, you forcibly stop the container and remove the container image, which produces no output.

There's more...

This recipe creates a new web page (in step 1 and step 2) on the CH1 host, then copies that file into the running container (step 5). When you run the container, you use port forwarding to instruct Docker to forward port 80 on the container host to port 80 in the container. This means that although you do not have IIS loaded on CH1, it is loaded, active, and runs a website inside the container. In this recipe, you are making use of the existing network address/name (that is, of the CH1 host) to access the container's website. You can see another method to push data into a container in the Using a Dockerfile to create and use a container recipe.

In step 5, you use the docker cp command to copy files from the container host into the container. In this recipe, you only add (and in step 6, view) a single page to the existing default website that was loaded by installing IIS. You can use the docker exec command to create a new website inside the container and run that, much like you did in the recipes in the IIS chapter. You could also copy all the files and other resources that are necessary for a rich website, set up SSL, and make use of host headers to support multiple containers.

In this recipe, you forwarded traffic inbound to port 80 on the container host to port 80 in the container. This is a very simple way to use containers and container networking. You could also create a Docker network and give your container unique IP settings. For more on Docker networking, see the following: http://rafalgolarz.com/blog/2017/04/10/networking_golang_app_with_docker_containers/ and https://docs.docker.com/v17.09/engine/userguide/networking/. You can, as ever, use your search engine to discover more about containers and networking. One thing to keep in mind as you search is that much of the search results relate to running containers on Linux, where the networking stack is quite different, and differently managed.

See also

This recipe uses the docker cp command to copy a file into the container. There are other ways to transfer information between your container and other hosts in your environment. See https://markheath.net/post/transfer-files-docker-windows-containers to take a look at some methods you can use to transfer data into and out of your containers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.42