Articles / Cloud

Run your first Docker container in Windows 10

Containerization or also known as containers involves bundling an application and all necessary configuration files, libraries and dependencies required to make it run. The bundle is compiled into an image file and can be moved across different computing environments. We will explore how-to install the required applications/features to support containers in Windows 10.

What is containerization?

Containerization or also known as containers involves bundling an application and all necessary configuration files, libraries and dependencies required to make it run. The bundle is compiled into an image file and can be moved across different computing environments.

You can almost deploy any non-interactive applications into containers. When I say interactive applications, I mean applications that presents a graphical user interface on a host through technologies such as WinForms, with the non-interactive I mean services such as a homepage or a API.

The difference between a virtual machine and a container is that containers are an abstraction of the application layer. Though each container runs isolated processes, multiple containers share a common operating system. Virtual machines are an abstraction of the hardware layer (CPU, RAM, HDD etc.). The virtual machine technology can be used to run multiple full-blown servers on a physical host.

The most popular containerization systems are Docker and Kubernetes.

In this article we are going install all prerequisites running a container in Windows 10 and deploy a simple web application. This will run without installing a web server in the operating system and just relying on containers and Docker.

Prerequisites

Before you can follow the article, please make sure that your machine has the following prerequisites meet:

  • Windows 10 Enterprise, Pro, or Education running at least build 1607 or higher.
  • 64-bit Processor with Second Level Address Translation (SLAT).
  • CPU (virtualization) support for VM Monitor Mode Extension (VT-c on Intel CPUs).
  • Your machine must have an internet connection.
  • Make sure that your machine has more than 4 GB of ram available.

Installing Hyper-V

Hyper-V is virtualization software that, well, virtualizes software. It can not only virtualize operating systems but also entire hardware components, such as hard drives and network switches. Docker Desktop requires either to have Hyper-V or WSL2 installed on your Windows 10 machine. We are going to use Hyper-V in this article.

  • Step 1

    Run the cmdlet in an elevated PowerShell session.

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

    Please note that you need to reboot the machine after you have installed Hyper-V.

Installing Docker Desktop

Docker Desktop is an easy-to-install application for your Mac or Windows environment that enables you to build and share containerized applications and microservices. Docker Desktop includes Docker Engine, Docker CLI client, Docker Compose and a few more components.

  • Step 1

    Download the latest version of Docker Desktop (version 3.1.0 time of writing). Go to the following URL for download “hub.docker.com/editions/community/docker-ce-desktop-windows/

  • Step 2

    Run the installation executable “Docker Desktop Installer.exe”.

  • Step 3

    Click “Yes” to the UAC prompt.

  • Step 4

    It will now download the required packages from the internet it may take a few minutes.

  • Step 5

    Uncheck “Installed required Windows components for WSL2”, this is not necessary if you are running Windows 10 Pro or higher. Click “OK”.

  • Step 6

    The installations are now complete, we need to logout and login in Windows 10 before it becomes available. Note that this will sign you out.

  • Step 7

    After you have logged in Windows 10 with your user again, click “User Hyper-V”.

  • Step 8

    Click on “Not now”.

  • Step 9

    Docker will now start. It will create an VM in Hyper-V, this might take a minute or so. You can verify if it is finished by hover the Docker icon in the tray area.


Build and run container the first container

Now that we have fully operational container ecosystem, we are ready to deploy containers through Docker. In this example we are going to deploy an IIS web server which normally runs on Windows Server, but because of the container technology can run in our Windows 10 machine.

  • Step 1

    Right click on the Docker Tray and click on “Switch to Windows Containers”.

  • Step 2

    Now click on “Switch” to switch from Linux to Windows based containers. It may take some time to finish.

  • Step 3

    Open a command prompt with elevated permissions and run the following command. The command downloads the IIS nano image from Microsoft. This may take up to 5-10 minutes depending on your internet connection and computer resources.

    docker container run --detach --publish 8080:80 --name web microsoft/iis:nanoserver

    The command creates container called “web”.

  • Step 4

    Now open your browser and navigate to “localhost:8080”.

    This shows the power of container technology. Currently we are running a local IIS on Windows 10, without the need to install a full-blown Windows Server.

Modify the content in the container app

You may be wondering on how to change the content in the web server, as there is no visible file structure that you can interact with?

We need to make use of the Docker CLI interface which is installed through the Docker Desktop application. In this next part, we are going to change the frontpage of the IIS webserver to something else.

  • Step 1

    Create a new file called “index.html” (remember the path to the file) and paste the following content to the file.

    <html>
    <body>
    <h1>This is pretty cool!</h1>
    </body>
    </html>

  • Step 2

    We are going to copy the HTML file you just created into the container. But first we need to stop the container, run the following command in an elevated command prompt.

    docker container stop web

  • Step 3

    Now copy the file into the container by running the command in an elevated command prompt.

    docker container cp C:\Path\To\File\index.html web:C:\inetpub\wwwroot\index.html

  • Step 4

    Start the container again. Run the following command.

    docker container start web

  • Step 5

    Again, open a browser and navigate to “http://localhost:8080”, you should get a new page saying, “This is pretty cool!”.

Clean up your containers

Despite all the different moving parts, Docker Desktop makes it easy to run containers in Windows 10. Now that we are done with our example container, it’s time to clean-up our resources.

  • Step 1

    Open an elevated command prompt and run the following command.

    This will stop your container and remove it.

    docker container stop web
    docker container rm web

Final thoughts

Containerization is a major trend in software development and is its adoption will likely grow in both magnitude and speed. I suspect that the next 5-10 years almost every webpage and micro-service will run in a container. In traditional software development, code developed in one computing environment often runs with bugs and errors when deployed in another environment. Software developers solve this problem by running software in ‘containers’ in the cloud.