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"]
WORKDIR /app
Add PublishOutput /app/
ENV ASPNETCORE_URLS http://*:5001
EXPOSE 5001
RUN dotnet restore
ENTRYPOINT ["dotnet","
#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);
}
}
No comments:
Post a Comment