Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Monday 5 June 2017

Dotnet Core Web Api, Docker, Consul & Registrator

Introduction

       In this article I am going to provide step by step tutorial for creating docker image for docker web api, consul for service registry and discovery, registrator for registering the docker service.

Prerequisites

  • Sample Web api application is ready and available in working condition in dotnet core.
  • Docker installed on the system.

Steps:

  #1: Create a docker file Dockerfile. in bin /Release folder parallel to PublishOutput folder.
  

#2: Put the below text inside Dockerfile.
FROM microsoft/dotnet:onbuild
WORKDIR /app
Add PublishOutput /app/
ENV ASPNETCORE_URLS http://*:5001
EXPOSE 5001
RUN dotnet restore
ENTRYPOINT ["dotnet",".dll"]
#3: Open command prompt pointing to the same path as Dockerfile and then run the below command
 >docker build -t <servicename> .
 docker images will be created.
>docker images
will list your image

#4: run the docker run command to execute the service
>docker run -d -p 80:5001 servicename
Now browse the service 

#5: now stop the docker container

#6: pull the consul & registrator images from hub
>docker pull gliderlabs/consul-server:latest
>docker pull gliderlabs/registrator:latest

#7: run the container for consul
>docker run -d --name=consul -p 8300:8300 -p 8500:8500  gliderlabs/consul-server -bootstrap -advertise=127.0.0.1

#8:run the container for the registrator
>docker run -d --name=registrator  --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://localhost:8500

#9: Now run the docker container for the web api
>docker start <containerId for webapi>

#10: Browse localhost:8500/ui and click on the services, new service will be listed there.

#11. Now client application of web api service can use the consul nuget package to discover the service.

var _services =new List<Uri>();
            var consulClient = new ConsulClient(c => c.Address = new Uri("http://127.0.0.1:8500"));
            var services = consulClient.Agent.Services().Result.Response;
            foreach (var service in services)
            {
                
                var isServiceAvailable = service.Value.Service(
                if (isSchoolApi)
                {
                    var serviceUri = new Uri($"{service.Value.Address}:{service.Value.Port}");
                    _serverUrls.Add(serviceUri);
                }
            } 


Sunday 26 March 2017

Creating DotNet Console application container with Docker

Introduction
In this article I am going to provide step by step tutorial how to create docker image for sample dotnet core console sample application.

Prerequisites
        Below framework needs to be installed on the dev machine
         Dotnet core & docker
   
Steps
   1. Create sample console application  in c# core as below

 
using System;
namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Test Program");
        }
    }
}
round-color: black; color: white; padding: 20pxdsfjfaskdjf

2. Compile and publish the application in a folder named PublishOutput.

3. Run Command Prompt Windows from PublishOutput directory/folder (cmd should point to PublishOutput folder) and type notepad Dockerfile.

4. Save the file.
5. Make the below entry in the file and save again
FROM microsoft/dotnet:onbuild
ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]
6. Execute >docker build -t testapp .
7. >docker run testapp