Friday, 24 November 2017

Common & frequent errors in docker setup

Environment: Windows 7, 8.(While using Docker Toolbox)

Action: Running command:- docker run hello-world

Error Message : Unable to find image 'hello-world:latest' locally Pulling repository docker.io/library/hello-world

Solution: Add below code to "C:\Program Files\Docker Toolbox\start.sh" at the end of file.

HTTP_PROXY=http://$proxy_user:$proxy_pass@$ip_server_proxy:$port

-----------------------------------------------------------------------------------------------------------------------

Environment: Windows 7, 8.(While using Docker Toolbox)

Action : Running angular based dockerfile which has npm install

Error Message: npm returned non-zero exit code

Solution: Increase your Ram size in your vagrant VM.


------------------------------------------------------------------------------------------------------------------------
Environment: Windows 7, 8.(While using Docker Toolbox)

Action : Docker-Compose throws below error.


Solution: Check the indentation and tab spacing, roughly the indention should proper key value pair as below.

------------------------------------------------------------------------------------------------------------------------
Environment: Windows 7, 8.(While using Docker Toolbox)

Action : Docker run throws below error when the angular-cli created angular app when run does not show the app in the browser.

Error Message: Invalid Host header

Solution: Change the package.json in your angular 2 app with the below value.






Environment: Windows 10

Action : Docker run throws error when running  "docker run"

Error Message "A firewall is blocking file Sharing between Windows and the containers. See documentation for more info."

Solution: Run the below script in powershell.

Set-NetConnectionProfile -interfacealias "vEthernet (DockerNAT)" -NetworkCategory Private
:

How to run Multiple container using docker compose

If you have not cloned or downloaded the git repo, please run the command below.


  1. Stop all containers:

docker stop $(docker ps -a -q)

  1. Navigate to the folder where you have downloaded/cloned the sample.



  1. Create a file named as “docker-compose.yml”.

  1. Add content as shown below.

  1. Run below command to build docker compose

docker-compose up -d --build --remove-orphans

  1. Run below command to run docker compose.

docker-compose up

In case if you get access denied error please share the drive you have your dockerfiles running.

  1. For windows 10: Navigate to http://localhost:9001



  1. For windows 7,8: Navigate to http://192.168.99.100:9001/



  1. For windows 10: Navigate to http://localhost:8080


  1. For windows 7,8: Navigate to http://192.168.99.100:8080/










How to run an angular app in docker

If you have not cloned or downloaded the git repo, please run the command below.


  1. Navigate to Folder using command prompt or docker prompt(windows 7,8 user) where you copied the  nodelinuximage.tgz

  1. Run below command to load the image to your system.

docker load < nodelinuximage.tgz

  1. Navigate to the angularclient folder.

  1. Create a file named as “DockerFile” without any extension inside the angularclient .

  1. Add content as shown below.


  1. Open command prompt or docker command prompt(windows 7,8 user)

  1. Navigate to the folder in the (docker)command prompt where the dockerfile was created.

  1. Run below command:

docker build -t angularclient .

docker run -it --rm -p 9001:4200 -v ${pwd}/src:/app/src angularclient

  1. For windows 10: Navigate to http://localhost:9001



  1. For windows 7,8: Navigate to http://192.168.99.100:9001/



  1. Stop all containers:

docker stop $(docker ps -a -q)

How to run a .Net core app using docker

For Windows 10 User(see below for windows 7,8 user):

  1. Click on Docker for windows and start docker.

  1. Navigate to Docker on your tray and click “Switch to linux container”

  1. Open command prompt: Type “docker run hello-world”. You  should get below message.



For Windows 7,8 user

  1. Click on and start the docker

  1. On the docker prompt: Type “docker run hello-world”. You  should get below message.

  1. Navigate to Folder using command prompt or docker command prompt(windows 7,8 user) where you copied the  linuxcoreimage.tgz and aspnetcoreimage.tgz

  1. Run below command to load the image to your system.

docker load < linuxcoreimage.tgz
docker load < aspnetcoreimage.tgz

  1. Download the .Net core/Angular project  project from the github(Make sure git is installed)


  1. Create a file named as “DockerFile” without any extension inside the aspnetapp.

  1. Add content as shown below


  1. Open command prompt or docker command prompt(windows 7,8 user)

  1. Navigate to the folder in the (docker)command prompt where the dockerfile was created

docker build -t netcorewebapi .
docker run -t -p 8080:80 netcorewebapi:latest

  1. For windows 10: Navigate to http://localhost:8080


  1. For windows 7,8: Navigate to http://192.168.99.100:8080/


Sunday, 2 July 2017

Async in C#


    All started when i asked the question about difference between Node.Js and C# .Net. It ended that the answer is how they handle request in asynchronous operation.

Asynchronous programming has always been a grey box for me, i understand how it works but not sure how thread process requests and how it works inside C#. I really did have lots of questions before, that has fuzzy answers.

Now i have better understanding about asynchronous programming. Hope you have these questions too, because if you have answer for the below questions, you should better understand the asynchronous programming and how node.js server and IIS works.  I write the blog in assumption you know the syntax of async pattern and have programmed a bit using async and TPL libraries, if you want more about the syntax of the async pattern, you can find it here

  • What is Asynchronous programming ?
  • Difference between Node Js vs C# asynchronous programming model ?
  • Asynchronous program runs on multiple threads ?
  • Why there is Task Keyword ?
  • When should i use await ?
  • When should i use Task.Run()

What is Asynchronous programming ?


    Before jumping into async pattern, let us look into asynchronous programming, asynchronous programming is nothing but, not blocking the thread, it started on.

Whenever there is request, a thread start processing the request, then IF it finds the request needs a  I/O or database or network operation, the current thread does not wait for the thread to complete, it just says “tell me when you’re done”. 

 

Difference Node Js vs C# asynchronous programming model ?


The main difference between Node Js and C# is their programming model. (Basically comparing Node Js and IIS server)

In case of Node Js, it offers only asynchronous programming model, meaning node js is built on assumptions that all your web application will use I/O or database or network resources etc to satisfy  their request. So the asynchronous (callback) is by default.

Whereas in C# we have an option of either using asynchronous programming model or synchronous (blocking thread).

So whenever there is a request to Node.Js server, the thread takes the request and then delegates to the other resource threads through event queue. Main thread is released to process other request that comes in, so the main thread is never blocked.

But in case of C#, Synchronous programming model, thread will process the request, whenever there is a database call or I/O call then  the thread waits itself for the external requested to be processed. So when the other request that comes to our IIS server has to wait. 

Await and async makes it asynchronous and makes it work like node js.Whereas in Node js, by default follows asynchronous model.

    

Asynchronous program runs on multiple threads ?

 

     Asynchronous does not mean it run on multiple thread, it only means the thread is not blocked or does not waits on any operation to complete. If we have multiple core system then the process is carried out by the number of threads available. Do not confuse multi threading with asynchronous programming model, async programming model can work in single thread environment too.



Why there is Task Keyword ?


    Task keyword is used in .Net technologies to denote the asynchronous programming model, it goes with async keyword denoting that the method is of asynchronous type and it returns the Task in future.

When should i use await ?


    Await keyword goes in synonymous with async keyword. We will look into how the compiler works when it reaches await keyword.  Below is the example we will use for  explanation.

In this example we are trying to download images from different website.

 private async Task AddFavicon(string domain)
       {
           WebClient webClient = new WebClient();
           byte[] bytes = await webClient.DownloadDataTaskAsync("http://" + domain + "/favicon.ico");
           Image imageControl = MakeImageControl(bytes);
           m_WrapPanel.Children.Add(imageControl);
       }

In the above example, until the compiler finds the await keyword, the execution is synchronous, the magic of asynchronous happens only when the await keyword is processed.

byte[] bytes = await webClient.DownloadDataTaskAsync("http://" + domain + "/favicon.ico");

When the compiler finds the await keyword, the current thread is released. The code below the await keyword is moved to separate part of execution, and the separate part is executed once the download is completed by the same thread or different thread. 

 When should i use Task.Run() ?


    Task.Run is used to start a separate new thread. It is mostly used to free up the UI thread to avoid freezing the UI.

I will share my experience why i needed a Task.run in the first place, i have old web api application which does not have async programming model implemented, but i need to incorporate the Active directory B2C into our application which only supports async methods. Now when i use await on the B2C get method which is async, the process suspends and does not return to the main thread of the Web api application, this happened intermittently, so i want run this async method and return to my main thread. So i have to start a new thread and make it run using Task.Run and i had control of the return and it solved the problem. Short answer is “You need a separate new thread, you can use Task.Run()”.

Leave a comment to share what you learned, and hit the “like” button to let others know about it (make sure you’re logged into Facebook to like and comment).



Build Bot using LUIS