Docker Containers and Docker installation
Containers like Dockers and others slice the operating system resources, for e.g. the network stack, processes namespace, file system hierarchy and the storage stack. Dockers are more like virtualizing the operating system. Learn more about dockers here. Open this URL and click on Download from Docker hub. Once downloaded, login to the Docker and follow instructions to install Docker for Windows.
Dockerize ASP.net core Micorservic
In my previous posts I have created a microservice ASP.net core and implemented Swagger for it you can find the code in GitHub. In this post I am going dockerize this Microservice.
Running the service could be done via docker commands to be run in docker command prompt and using visual studio as well. Since we added the docker support when we create the project, it is easy to run the service in docker container using visual studio.
- This will ask for the orchestrator. Select Docker Compose and press OK.
Once added to the solution, the solution will look like shown below having docker-compose with dockerignore and docker-compose.yml and its override file.
As soon as the solution is saved, it builds the project under the container and creates a docker image. All the commands execution can be seen in the output window when the solution is saved.
- Open the command prompt in admin mode and navigate to the same folder where the project files are.
- Run the command docker images to see all the created images. We see the ProductMicroserviceimage the latest one.
- Now, run the command docker ps to see the running containers. It shows the container is running on 32773:80 port.
Since the container is in running state, it is good to test the service now running under the container. To test the service, replace ”values” with “product” in the address as shown below. Ideally, it should get the product details. But it gives exception as shown below:
Running the same thing under IIS Express works fine i.e. on port 44312. Replace “values” with the product to get the product details,
Since in IIS Express application runs fine and not in docker container, the error clearly shows that something is wrong with the SQL server that it does not understand our docker container or it is not running under docker container. In this scenario, the docker container is running as a separate machine inside the host computer. It is like that docker container running another machine an SqlServer (local) is installed in a specific machine. So, to connect to the SQL database in the host machine, remote connections to SQL needs to be enabled. We can fix this.
Configure SQL Server for remote connection
- Open the SQL Server Configuration Manager (in windows 10 you can open this by executing: SQLServerManagerxx.msc where xx is the version number of SqlServer. eg. for Sql server 14 it is SQLServerManager12.msc)
- Now select Protocols for MSSQLSERVER and get the IPAll port number under TCP/IP section.
3. Open port: 1433 on your Windows Firewall
Setup your firewall to accept inbound connection to 1433. you can start Windows Defender Firewall with Advance Security from this select Inbound Rules: New Rule: Ports: Protocols Ports and the select TCP and write 1433 in the Specific local prots as shown in the following figure:
press Next and then select: Allow All connection and press to Next and Next, Next, Next and write: under Name: 1433 and press to Finnish button.
Then it shall show the following image:
4. In the Dockerfile add EXPOSE 1433 under EXPOSE 80
5. Change the connection string mentioned in the appsettings.json
In appsettings.jsonfile points to the data source as local which the docker container does not understand. It needs proper IP addresses with port and SQL authentication. So, provide the relevant details i.e. Data Source as Ip address, port number and SQL authentication details as shown below.
where IP address can be found from the >ipconfig command for IPv4 address in your local machine (depends on your machine is connected via cable:Ethernet adapter, or Wireless LAN adapter Wi-Fi
Now again run the application with Docker as an option like done earlier .
This time the response is received.
Important: this solution can solve the problem Show in browser in running docker container on the Docker desktop (opens containers application on browser)
Test again with IIS Express URL.
This proves that the microservice is running on two endpoints and on two operating systems independently locally deployed.
Conclusion
A microservice is a service built around a specific business capability, which can be independently deployed which is called bounded context. This article on microservices focused on what microservices are and their advantages over monolithic services architecture. The article in detail described to develop a microservice using ASP.NET Core and run it via IIS and Docker container. Likewise, the service can have multiple images and could be run on multiple containers at the same point in time.
Source code can be find in my GitHub
In my next post I will explain Building image from the command line
This post is part of “Microservices-Step by step”.