Codementor Events

Enable Docker Support to ASP .NET Core Application

Published Aug 10, 2019Last updated Aug 11, 2019
Enable Docker Support to ASP .NET Core Application

We will start with the most confusing questions around Container Service -

  • What is difference between VM and Container?
  • How Docker for windows is different than Docker on Windows?

Container vs VM

In a nutshell, a VM provides an abstract machine that uses device drivers targeting the abstract machine, while a container provides an abstract OS. A para-virtualized VM environment provides an abstract hardware abstraction layer (HAL) that requires HAL-specific device drivers. Applications running in a container environment share an underlying operating system, while VM systems can run different operating systems. Typically a VM will host multiple applications whose mix may change over time versus a container that will normally have a single application. However, it’s possible to have a fixed set of applications in a single container.

1.JPG

Containers provide a number of advantages over VMs and the most important is the low overhead of containers and, therefore, the ability to start new containers quickly. This is because starting the underlying OS in a VM takes time, memory, and the space needed for the VM disk storage.

Docker for Windows vs Docker on Windows

Docker FOR Windows has been around for some time – ever since the advent of Docker Toolbox and its predecessor, Boot 2 Docker. Docker for Windows is a product meant for running both Linux and Windows containers on Windows. It's not meant for a production environment, and instead is the best way to get started with Docker on Windows systems.

2.JPG

Docker ON Windows entered general availability on September 26, 2016. Docker ON Windows server is an implementation of Docker that will run on a Windows Server without any Linux components. It is made to run only Windows-based containers.

3.JPG

Getting Started – Install Docker ToolBox

Docker Toolbox is an installer for quick setup and launch of a Docker environment on older Mac and Windows systems that do not meet the requirements of the new Docker for Mac and Docker for Windows apps. Docker Toolbox includes the following Docker tools:

  • Docker CLI client for running Docker Engine to create images and containers

  • Docker Machine so you can run Docker Engine commands from terminals

  • Docker Compose for running the docker-compose command

  • Kitematic, the Docker GUI

  • Oracle VM VirtualBox

Because the Docker Engine daemon uses Linux-specific kernel features, you can’t run Docker Engine natively on Windows. Instead, you must use the Docker Machine command, docker-machine, to create and attach to a small Linux VM(Boot2Docker) on your machine.

https://docs.docker.com/toolbox/overview/

4.png

You can start the virtual machine either from VM VirtualBox or launch it from Docker QuickStart terminal. From Terminal you can remote login securely to machine using SSH protocol as below -

$ docker-machine ssh

If you have multiple machines, you need to find your "machine name" first:

$ docker-machine ls

NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS

default * virtualbox Running tcp://192.168.99.101:2376 v1.10.2

You can see that default is the name of your machine and you can ssh as follows:

$ docker-machine ssh default

When you're in, it's a simple case of sudo-ing to root

docker@default:~$ sudo -i

Boot2Docker version 1.10.2, build master : 611be10 - Mon Feb 22 22:47:06 UTC
2016

Docker version 1.10.2, build c3959b1

root@default:~#

Add Dockerfile to root directory to .NET Core Application

Now let's create an ASP .NET Core Application. In my example I have writen a controller which accept LoanKey and GlobalPropertyId and returns Required Coverage Period. Next, add below Dockerfile to the root directory of application. You can also have docker file added by selecting Docker for Windows checkbox in ASP .NET Core Application template as below -

template.JPG

FROM microsoft/aspnetcore:1.0.1
WORKDIR /app
COPY published ./
ENTRYPOINT ["dotnet", "requiredcoverage.dll"]

Instructions for running App

Run Docker Terminal and navigate to root of application location:

$ cd “F:\\requiredcoverage”

Follow these steps to build, publish and run this sample in a Linux container:

$ docker build -t requiredcoverage .
$ dotnet publish -o published
$ docker run -d -p 80:80 requiredcoverage

Validate Docker container

5.png

6.png

7.png

View web page running from your container

If you are using a linux container you can simply browse to http://localhost:80 to access your app in a web browser.

8.png

Running docker container with a new volume.

$ docker run -d -P --name rslog1 -v /rsvlog1 rsvlog1

Command to stop all containers.

docker stop $(docker ps -a -q)

Discover and read more posts from DhananjayKumar
get started