Create Docker Windows Containers

Create Docker Windows Containers from Docker Desktop

Docker containers become unavoidable for infrastructure development as it provides, Isolation, Simplicity, Rapid Continuous Deployment, and Faster Configuration with Security. Earlier, Docker has only used for Linux based applications as it is using the Linux kernel baseline for creating Containers. But Windows applications are widely used in Software development and Hence, windows developers need Docker Containers for Windows. In this article, we will discuss How to Create Docker Windows Containers from Docker Desktop.

Docker Desktop Installation

Requirement

  • Minimum Windows 10 (Home and All other Editions)
  • Hyper-V (In-Built and can be Enabled)
  • Only 64bit Processor Architecture
  • Virtualization Enablement from Bios Level

Installation

NOTE:

Docker Desktop Installed in the Windows Machine can run Linux Based Docker Containers and Windows-based Docker Container. But You cannot run Windows Based Docker Containers on Docker Engine installed in Linux. Refer the below image.

Docker Host Table
Docker Host Table

About Docker on Windows Machine

As we all know, the Docker Engine will run as a daemon that uses the Linux specific kernel features. So, running the Docker Engine on Windows directly is not possible. Hence, we must create a Linux based environment in Windows to run docker. In order to enable Linux environment on the windows, we have two options,

  • Windows Subsystem for Linux (WSL) – is the compatibility layer to run Linux applications
  • Hyper-V – Microsoft solution for virtualization.
Docker on Windows Machine Architecture
Docker on Windows Machine Architecture

Both these features are available from Windows 10. Running docker on windows will be ultimately using the Linux environment. But it is using some of the Host’s features. So, Docker Engine will sit on top of the Linux Kernel created by the Hyper-V/WSL. On top of the Docker Engine, Docker Containers can be created. All this is managed by the Docker Desktop. So, Application Program which will be written by the developers will sit on top of the Containers.

Simple Windows Container with Example

Let’s learn how to create the Docker Windows container using Docker Desktop. For that, first, we are going to create Dockerfile which is the simple text file with the instructions of the application and configurations.

Creating Dockerfile

Let’s run a simple application which will return the “hello world” print output from the Windows Docker Container. For the same, create a file called “Dockerfile” and put the bellow content.

# Base Image
FROM microsoft/nanoserver

# Copy powershell init-script from the host machine (windows) to the docker container.
COPY init-setup.ps1 c:\\workspace\application\init-setup.ps1

# Run the Powershell script in the Docker Container
CMD ["powershell.exe", "c:\\workspace\application\init-setup.ps1"]
  • In this, Line number #2 is setting the container from the base image. Here it is “microsoft/nanoserver”.
  • Inline Number #5 is giving instructions to copy the init-script.ps1 from the host to the docker container.
  • Line Number #8 is to run the script in PowerShell executable.

And Create a script file called “init-setup.ps1” and put the below content inside the file

echo "hello world"

Building Docker Image

Once you have this file in your folder you can start building the Dockerfile as Docker image using the command.

$ docker build -t digitalvarys/print-hello-world .

where digitalvarys/print-hello-world is the tag name of the docker image.

Once the Docker image is been built, you can check the Image by passing the following commad

$ docker images

This will display the created image.

Running the Docker Container

Now, it is time to run the Docker image which we have created. Hence, run the following command

$ docker run digitalvarys/print-hello-world

This will print the string “hello world” as we provided.

If you run it with -it parameter, you can explore the Created Docker Container with Windows CMD.

ASP.net example of the Windows Docker Container.

The above sample application will tell you about the basic container feature. This one will tell you the real-time advantage of the Windows Docker Container.

Sample application  

For this tutorial, we are going to use, cloud foundry’s sample Dotnet core hello world application (https://github.com/cloudfoundry-samples/dotnet-core-hello-world). Just clone it and keep it in your working directory

Dockerfile creation.

Now, we are going to create Dockerfile to create the image of the above application.

# Base Image
FROM microsoft/dotnet:nanoserver-core

# Copy entire application code folder dotnet-application to the working directory in Container.
COPY c:\\workspace\dotnet-application .

# Relax the firewall rule to expose port 5000
EXPOSE 5000

# Run the dotnet application
CMD ["dotnet", "run", "--server.urls", "http://0.0.0.0:5000"]

Just like the PowerShell example, we are going to take the base image and copy the application from the host to the container. Then, we are going to expose the port 5000 to run the dotnet application in this port. Then, we are going to run the application using the dotnet executable.

Building the Docker Image.

Once the Dockerfile is ready, we have to build the Docker container.

$ docker build -t digitalvarys/simple-dotnet-application .

Here, I am giving the image name as “digitalvarys/simple-dotnet-application”.

Running the Docker Container

Now, we have to run the application in background (detached mode),

$ docker run -d -p 5000:5000 digitalvarys/simple-dotnet-application

This will run the container and expose the container port to the host port 5000.

Getting the IP address of the Created Docker Container.

Now, Just inspect the Docker container and see the assigned IP address of the running container,

$ docker inspect [container-id]

This will show you the JSON response. In that, check for “networks” -> “nat” -> “IPAddress”. This will be your container IP address.

Now, Just enter the URL in the browser as https://[container-ip-address]:5000, then you will see the Hello world message in the browser. This means your application is running in a container.

Conclusion

As we already discussed, Docker is unavoidable for the application development or at least in the process of application development. But the Containerization of the Windows application like dotnet application needs extra lookup. Hope this article covers enough concepts and procedures for the Windows Docker Containers running on windows application. In our upcoming article, we will discuss more running a cluster of Microsoft Windows-based applications in Docker Swarm and Kubernetes. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

Leave a Reply