Do you want to run your Angular and Dotnet Core application inside Docker container and also connect it with a database like SQL server?
In this article I will show you exactly how to do that .
I have created an application using the Angular template of Dotnet Core. This application also communicates with the Microsoft SQL Server database for saving and retrieving data.
The code for this tutorial is available on Github at https://github.com/juzer-hakimji/DockerDemo.
You can download the SQL Server database from here.
Requirements : you need to have Docker desktop installed in your system.
What is Docker?
Before we start with the tutorial I would like to explain what are Docker containers and why are they used.
Earlier virtual machines were used to deploy applications. We can create multiple virtual machines on a single computer. But the problem with virtual machines is the space and resources they require to run.
Containers. Containers allow us to do the same work we do with virtual machines but with much lesser space and resources required.
So now with the same computer, you can host many more applications with containers than you can with virtual machines.
How to create a Docker container
After you have created your application, you need to create a Dockerfile in the root of your application.
A Dockerfile contains commands that tell what are the things which need to be installed and copied inside the container. Visual Studio provides an option to add Docker support to the application in which it creates the Dockerfile for you.
An image is created using Dockerfile. This image can be shared with anyone or can be uploaded to a repository. Containers are built using this image. You can build as many containers as you want using this image.
The point to remember is that we do not share containers. We share images and containers are built from those images.
Let’s say you are developing in a windows machine and your server is on Linux. You do not have to worry about compatibility now. Your image is cross-platform and will work irrespective of the Operating system.
Running Dotnet Core and Angular application inside Docker container
After you have completed any Dotnet Core application and you want to run it in a Docker container the steps are as follows
1) Add docker support to your Dotnet Core application in visual studio.
a) Right-click on your web project in solution explorer, go to Add, and click Docker Support.
It will ask you to choose the Operating system which will be used inside your docker container. Choose the OS which you had chosen while installing Docker Desktop. Click Ok.
The rest of the work will be done by visual studio. It will add Dockerfile and will check for all the requirements and let you know if anything is missing. It will download all the images required for your application to run, for example, the base Dotnet Core image.
b) Change the run option from IIS Express to Docker inside your standard toolbar. Click Docker.
You should see new container running inside Docker desktop dashboard. A new tab will open in your browser. If your application is not running in the browser, wait for some time and then refresh it. It will work.
2) How to run Angular application inside Docker
Let’s say you have used Angular template given with Dotnet Core. If you want to run this application in Docker container you need to add additional layers inside your Docker file.
The Docker file which was created with Dotnet Core, in that you need to add another layer where you install Node.js in your container to run Angular application. The final Dockerfile looks like this.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 # Prevent 'Warning: apt-key output should not be parsed (stdout is not a terminal)' ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1 # install NodeJS 13.x # see https://github.com/nodesource/distributions/blob/master/README.md#deb RUN apt-get update -yq RUN apt-get install curl gnupg -yq RUN curl -sL https://deb.nodesource.com/setup_13.x | bash - RUN apt-get install -y nodejs FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR /src COPY ["SamplePOC.csproj", ""] COPY ["../BusinessLogicLayer/BusinessLogicLayer.csproj", "../BusinessLogicLayer/"] COPY ["../ViewModels/ViewModels.csproj", "../ViewModels/"] COPY ["../DataAccessLayer/DataAccessLayer.csproj", "../DataAccessLayer/"] RUN dotnet restore "./SamplePOC.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "SamplePOC.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "SamplePOC.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "SamplePOC.dll"]
In this Docker file after installing the base image we are installing Node.js in our container so that our container will be able to run Angular application.
The rest of the steps are copying files from our PC to container and mentioning entry point for our application. You can learn more about Dockerfile from here.
Connecting SQL Server from Docker container
If you are also using a SQL Server database with your application, you will not be able to connect to your database from your application running in a Docker container with regular connection string. Because your local SQL Server instance is running in your PC which is outside of the container.
To connect to our SQL Server instance from container we need to enable TCP/IP protocol for SQL Server. Follow the steps as shown below to enable it.
1) search for SQL Server 2019 configuration manager in windows start.
2) go to SQL Server network configuration in the menu
3) Enable TCP/IP protocol as shown in the image. Also, check the TCP port number in IP Addresses tab. We will need it later.
4) Click Apply.
5) After that , go to SQL Server Service in the same menu and restart SQL Server.
now our SQL Server instance can be connected from the container. We need to change the connection string as well.
change the server value as above and port number should be the one in your IP Addresses tab.
“host.docker.internal” is an alias for the IP address of your machine. After changing the connection string you will be able to connect your database from application inside the container.
That’s all. This how you can run your application in Docker container and connect SQL Server database to it.
Thank you for reading this article. Have a good day.
Hope you liked the post, Thanks for reading 🙂