.NET Docker Tutorial - SQL Server Docker [.NET Docker]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi. Welcome to CodingDroplets. In this video we  are going to see how to containerize a .net core application with a sql server database. So we need  to create two containers. One for the dotnet core application and another one for sql server. We'll  be adding the container orchestrator support for our project and make use of docker compose to  deploy the application with all the docker images needed. Before starting you should know few things. This video is part of a series named microservice architecture. You can find the playlist link in  the video description to watch the other videos in this series. In the last video we have seen how to  create and deploy a single container application. The main intention of this video is to make  you understand how to create and deploy multi-container applications. It is very  important to understand the concept as in the upcoming videos we will be  creating multiple microservice applications which we need to containerize and deploy  in the same way. So let's get started. This is a simple project which we  are going to containerize and deploy. The process is same for any dotnet core  application. For this example I have created a .Net Core blazor project. If  you are interested in learning blazor development you can find the playlist named  blazor tutorial in our channel. I'll just show you the application first by running it. It is a simple  application in which we can create multiple users. As you can see there are two tabs. In the first tab, we'll be listing the added user details and in the second tab we are having the option to add a new  user. Let me add a user by providing the values. Now that user is listed under the first tab. I'm  adding one more user. Now the new user is also listed here. We also have a trash icon here which  will delete the user record from the database. This application saves data in a sql server  database. In this project I have used entity framework core to read and write data  to the database. In order to use entity framework core in the project, we have to install  'microsoft.entityframeworkcore.design'. Then 'microsoft.entityframeworkcore.tools' and 'microsoft.entityframeworkcore.sqlserver' libraries from the nuget packages. Here you  can see the packages installed for this project. This is the database context class which inherits  from db context class of entity framework. In the constructor you can see that I have created  an object of database creator and using that object I am creating database if it doesn't exist. Also the same object can create tables in the database if no tables exist. This method will do  the database and table creation while we run the application for the first time. Below that method, I  have declared dbset for user entity. Now moving to the program.cs class. You can see the dependency  injection made for the database context class here. Here you can see the connection string. In the  connection string DbHost DbName and DbPassword are fetched from other variables. It is because  while containerizing will be using environment variables here. I will show you soon how  to do that. Now in the 'users.razor' file, you can see a method named fetch data  which will fetch users from the database. Also here you can see a delete user method  which will delete the user from the database. In the add user razor file, we have the method  to save the user to the database. So that's it in our application. It's a simple one. Now let's  see how we can add container orchestrator support for our project. For that right click on the  project in solution explorer. Then go to add option. There you can find a menu option named, container  orchestrator support. Once you select that, visual studio will ask for the container orchestrator. We can choose docker compose here and click ok. Then visual studio will ask for the container  operating system we need. I prefer to use linux. Now this will add a docker file in our project  and it will also create a docker compose project in our solution. Visual studio allows us to  debug the application using docker compose. As we have seen in the last video, now visual  studio will download all the needed images for creating the debug docker container. Now let's  have a look on the newly created docker file. It is the same kind of docker file which we  have seen in the last video. Here is the 'EXPOSE' instruction to expose the port number 80. Now  let's open the docker-compose yaml file. Under the services section, there is only one  service with the name demo blazer server app. Docker compose will create only a single container  with this docker compose yaml file. But we need to create a sql server container as well. So we  must modify this yaml file as per our need. First let me provide a container name for this  demo blazor server app. Let it be demo blazor app. Then let's provide a port mapping to map the  port number 8001 of the host machine with port number 80 of the container. So that we can access  the application using 8001 from the host machine. We have already seen that the docker  file is exposing port number 80. Now let me create one more service for sql server. Let the service name be demo app db. I am providing a friendly name for this container as well. Let  it be app-db. Next we can provide the image details. Let's have a look on the sql server image  in docker hub. You can just search for sql server in docker hub and you can find the docker image by  just scrolling down. Here it is mentioned that this is the official image for microsoft sql server  on linux for docker engine. Let me click on that. Here you can see different tags for different  versions of sql server. 2019-latest, then 2017=latest etc. In the section below  you can find the image name mcr.microsoft.com/mssql/server. So let us provide the image details now . I am copying the image name from the documentation. After that put a colon and we need to  provide the tag which we need to use. Let's use 2019-latest which is the latest  version available while I am recording this video. Next I can assign a port mapping. But this is  optional. You can do this if you need to access the database from the host machine. I am assigning port  number 8002 for port 1433 which is the default port number for sql server. So  that I can open the database using 8002 from the host machine. Next we can assign  some environment variables for the sql server docker container. In the documentation itself you  can find the environment variables that we can use. First one is accept end user license  agreement equals 'Y'. So let me provide that. Then the second one is SA_PASSWORD  to assign the system administrator password. Here you can provide the password you need. Let  me provide a password which I need to assign. Now let us add the demo blazor server app depends on the demo app db. So that docker will start the database  container before the application container. Next let's provide some environment variables for  our application container. I need the application to generate a connection string based on  the environment variable values we provide. First let me create a variable named DB_HOST . So demoappdb is the database host. Then DB_NAME. I need the application  to create and use the database with the name what I provide here. So let the name be demo blazor  app. Next DB_SA_PASSWORD. Here we need to provide the same password what  we have assigned in the database container. Now we need to make changes in our application  to read the values from the environment variables and use them in the connection string. So in  the program.cs class, i can call the 'Environment.GetEnvironmentVariable' method to fetch the value.  DB_HOST is the environment variable name for the database host. Now I can copy the same  method and change the environment variable names. DB_NAME for database name. DB_SA_PASSWORD  for the password. So now the connection string will use the values from the environment  variables. Next important thing what I need to discuss is about networks. In docker we can  place multiple containers in the same network if they need to communicate each other. So let  me show you some basics of docker networking. While creating a docker network, we can  specify the network driver we need to use. Let's see the available network drivers in docker. Bridge driver. This is the default network driver.  If you don't specify a driver, this is the type of network you are creating. Bridge networks  are usually used when your application run in standalone containers that need to communicate. Host driver. If you use the host network driver for a container that containers network stack is not  isolated from the docker host. The container shares the host's networking namespace and the container  does not get its own ip address allocated. Overlay driver. The overlay network driver creates  a distributed network among multiple docker hosts. IPVlan driver. The ipvlan driver gives users  total control over both ipv4 and ipv6 addressing. MACVlan driver. MACVlan networks  allow you to assign a mac address to a container making it appear as a  physical device on your network. The docker will route traffic to  containers by using their mac address. None. Used if you want to completely disable the  networking stack on a container. Just assume that we have a solution with one web application  and one web api. The web application should not communicate directly to the database. We need  the communication to happen through web api. Here web api is the only application  that need direct access to the database. So for this solution we should create two  networks. Let's name it as frontend and backend. In frontend network we can add web application and  web api. So that they can communicate each other. In the backend network we can add web api and the  database. So they can also communicate each other. Here the web api container is having two networks  both frontend and backend. If we establish the network connectivity like this, the web application  docker container cannot even access the database docker container because they don't have any  network connectivity. Hope you got a brief idea about docker networking. We'll be using different  types of networks in our upcoming videos. So coming back to our docker compose file. Let me  create a new network under networks section. I'll name the network as demo blazer app. You  can provide whatever name you need. Here I am not providing any network driver. So by default  docker will use bridge driver for this network. Now let us add the database docker  container under the new network. Also we can add the application docker  container under the same network. Now let's run the application using docker compose. So visual studio will create containers according to our docker compose file and run it. Let's  see the application is working as expected. So the application is loaded in  the browser. Let's try to add a user. The user got successfully created. You can see the  user is in the list. That means the application can access the database container and do the read and  write operations. Let me minimize the browser and let's have a look on the environment variables  of each containers in the container window. So you can see the app-db container is having  the environment variables which we have assigned. The application container is also showing the  values we assigned for the environment variables. In my docker desktop you can see the containers  created by visual studio for debugging. The docker images are also listed here. Next I  need to show you how to deploy this application in a different machine. Just before that let  me publish this application to docker hub. So that we can use the docker image directly  from the docker hub while deployment. So choosing the docker container registry as the  target and docker hub as the specific target. Then providing the credentials  to access docker hub repository. The process got started. It will build our  application and push the image to dockerhub repository. I am skipping these steps as we have  already discussed this process in detail in the last video. The process got completed. Let me open  docker repositories in browser and you can see the newly created public repository. Let me click  on that and you can see the tag assigned for docker image is the latest tag. We can see more  information of the docker image in the tags tab. So now let's deploy this application using docker  compose. For that you must have docker compose installed in your machine. Docker desktop for  windows includes docker compose along with it. So you don't need to install it separately. To install  docker compose you can visit docs.docker.com/compose/install page and you can  find the options to install docker compose. Here you can find different tabs for mac, windows,  windows server, linux etc. The project is uploaded in our github repository. You can access it  using the url github.com/codingdroplets. In the repositories tab you can find a project  named demo blazer server app with docker compose. Inside that we have project files including  the docker compose file. So let me open that. The content is same as we have seen before. Let  me copy the file content and paste it in notepad. You can use any text editor. Just follow the same  procedure if you need to test by deploying this application in your machine. You can also access  the docker image as it is a public image. The demo app db service section or the database section  is fine. We don't need to make any changes there. Let's move to the application service section. Here we can replace with the name of the image in docker hub. Also we don't need the build  section as the docker image in docker hub is already compiled and ready to use image. That's  it. Now let me save the file. I have a folder named demo-blazer-app in my C  drive. I am saving this file to that folder. Let me name the file as docker-compose.yml. Next I'm opening command prompt to run the docker compose command. Just before that  let me execute docker ps -a command and you can see there are no docker containers  in this machine. Also there are no docker images. So let me move to the folder named demo blazer  app. Now here i can provide docker-compose up -d. Up is the docker compose parameter  or keyword to pull the needed images and start the container. Now you can see the docker  compose is pulling two images. One is our application's image and the second one is the  sql server image. Here the important point what you need to understand is, if you have published  the application to a public or private repository, you only need this docker compose file to deploy  the entire application in a new machine. In the new machine we don't need to install dotnet runtime  or sql server database or any other dependencies and libraries. The only thing we need is the docker  and docker compose to be installed in that machine. Once the images are downloaded docker compose  will create the network and the containers. Then start the containers. Now let's see the running  containers using docker ps command and you can see both the containers are running. Also let me list  the images using docker images command and you can see the downloaded images. I'll show you in docker  desktop as well. Here you can see the containers. In the images section you can see the images as  well. Now let's run the application in browser. So let me open localhost:8001 in a new tab. The application has been loaded successfully. Let's check whether  the database communication is happening. We can try by creating a user and listing it. Also I'm able to  delete the user. So the application is working fine. Now I will show you one more thing. We can use  docker compose to stop and remove the containers and related networks. From the same folder which  is having the docker-compose.yml file, we can just provide docker-compose down  command. Now you can see that the containers and the network got removed. There are no  containers now in my docker desktop. But the images will not get deleted. Hope you are clear  with docker compose. So that's it for this video. Hope you liked it. Please subscribe, like and share  this video. See you all in the next video. Thank You!
Info
Channel: Coding Droplets
Views: 17,651
Rating: undefined out of 5
Keywords: docker asp.net core 3, docker asp.net core tutorial, docker .net core api, docker .net core microservices, docker in .net core, docker secrets .net core, docker dotnet core api, install .net core api in docker, .net core docker, .net core docker debuging, docker blazor webassembly, docker .net core debug, docker asp .net core, docker for .net core, docker .net core, deploy .net core to docker, sql server docker, .net docker tutorial, .net docker
Id: hpLvXNASyTI
Channel Id: undefined
Length: 20min 18sec (1218 seconds)
Published: Wed Apr 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.