AZ-204 - Develop Azure Compute Solutions - Azure Container Instances and Docker Compose

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] now in this chapter i want to go through the azure container instance service so in the earlier chapter we had seen how we could deploy a container based on an image we had published onto doc hub so this was a dot net core application over there we went ahead and deployed a container on a linux vm now let's say that you want to go ahead and deploy a container just for testing purposes so yes you can go ahead on to your local machine you can go ahead and see if the containers are working in your development environment but let's say you want to go ahead and see or publish the container from the image on basically another platform and let's see you don't want the burden of going ahead creating a linux vm installing docker and then pulling down the image and seeing if it works so what you can do is that you can actually go ahead and make use of the azure container instance service for this purpose this service provides a fast and simple way to go ahead and deploy your containers here you don't need to provision any vms for hosting your containers here the containers get their own fully qualified domain name and ip address as well the azure container instance can actually go and create containers based on images that are available in docker hub or even in an azure container registry so let's see how we can accomplish this so here we are in zero now i'll go on to all resources and let me go ahead and add a new resource so over here i'll go ahead and search for container instances i'll go ahead and choose that i'll go ahead and hit on create here i'll go ahead and choose the resource group i'll go ahead and give a name for my container instance so it is available i'll go ahead and leave the region as it is now over here in the image source you can go ahead and choose quick start images which are available in the microsoft registry or you could go ahead and choose as your container industry or you could go and choose docker hub so over here let's go ahead and choose docker hub and let's go ahead and specify the name of the image that we have in docker hub over here you can go ahead and specify what should be the underlying os type so this is basically the os it will be used for hosting the docker environment so remember in the background that entire compute that entire virtual machine which is hosting docker the docker host is going to be managed it's going to be a managed service when it comes to the azure container instance service itself over here you can go ahead and choose the size for the underlying vm that is the docker host i'll go onto next for networking so i'll leave everything as it is over here you can see you can assign a dns name label over here you can see the port mapping so i'll leave it as port 80 i'll go on to next advance i'll go on tags i'll go on to review and create and let's go ahead and create the container instance let's come back once we have the instance in place now once you have the container instance in place if i go ahead on to the resource over here i get a public ipad so let me go ahead and take the public ip address and go on to a new tab and over here you can see the home page for your application again so you're going ahead and deploying it onto the azure container instance over here if you go on to containers so over here you can see the state of your container if you go ahead and scroll down let me go ahead and just hide this and over here you can basically see events so if i go ahead and just scroll this on top so you can see all of the events so over here it went and pulled the image it successfully pulled the image and then it started the container if you go on to properties you can see the different properties over here you can see the image that was pulled down from docker hub if you actually go on to the logs over here you can see the actual logs of the container itself so what's happening within the container you can see the logs over here and then you can actually go on to connect and you can also go ahead and connect onto the container itself so over here if i go and hit on connect over here i'm actually connected onto the container if i go ahead and do a ls over here i can see my application files so in this chapter just want to let you know that if you want to go ahead and have an easy deployment service for your containers it's a managed service you can go ahead and make use of the azure container instance now in this chapter just for the benefit of students i just quickly want to go ahead and talk about layers so when an image is pulled down whether it be from docker hub or whether it be from another registry it goes ahead and pulls down various layers for that particular image so actually when you look at how an image is built an image is built based on multiple layers you have multiple layers that actually go ahead and form that actual image for example if you have an nginx image in place over here you might have the base image has a layer which is probably the lightweight os version of let's say ubuntu just giving an example the next layer would involve having nginx now deployed onto the base image and you might have one more layer wherein you have the configuration files that is probably required for nginx itself some additional configuration files so over here your base image is mostly the os and then on top of that when you make any change so let's say you're going in and adding files if you are going ahead and installing something on the base image it will go ahead and create additional layers every change that you make will actually go ahead and create an additional layer but then the docker system actually makes all the layers just same has one seamless file system to you so it will go in and pull the layers and make it has one image to you so you don't have to be bothered about the layers themselves so what you have to ensure is when you go ahead and build a docker image using a docker file then you have to ensure that your image is not that large in size because the entire idea is to have a packaged version of your application has light as possible so even when it came to building our own docker image using our.net core application over there we probably had a base image up again linux then we had one layer for having the asp.net core runtime and then we had our application has another layer now when you go ahead and have an image so all the layers are basically read only when you go ahead and create a container out of that image then the container will have one layer on top of all the other layers so these are all the layers this top layer will actually be a writable layer so if you want to go ahead and let's say write any sort of information that is required by the container so this could be files this could be data that's required by the application you can go ahead and make it available on the writable layer but once the container is stopped once a container is destroyed then this writable layer is not available anymore you can actually go ahead and persist data onto volumes that's something we will see in later on chapters but is to go ahead and explain that concept of layers just want to let students know when it comes to working with docker now in the next set of chapters i actually want to go ahead and explain about the instructions which is available basically in this docker file now in order to go ahead and explain these instructions let's go through the manual way of building a docker image rather than using visual studio so firstly what i'll do i'll go on to my solution so currently it's in the path of c temp docker app so let me go ahead and open the folder in file explorer so over here i've got my docker app in place over here if i go onto docker app i have my docker file in place now let me go ahead and publish my project has an application that can be deployed just on my local machine so i'll go on to my project i'll right click i'll go ahead and hit on publish and over here let me go ahead and create a new publish profile over here i'll just choose folder i'll go on to next and over here in the bin release folder it will go ahead and create a published version of a application i'll go ahead and hit on finish and let me go ahead and hit on publish so this will go ahead and do a build of our project to ensure that we have a deployable version of our dotnet core application please at this point in time we are not using the docker file we are not going ahead and creating an image so far we have gone ahead and created a runnable version of our dotnet core application so now if i go on to the bin folder if i go onto release if i go on to the publish folder so over here i can see the artifacts which can be now deployed onto a web server now let's say i want to go ahead and create an image out of this so let me go ahead and just open up notepad i'll go on to visual studio let me go ahead and copy all these instructions i'll go on to notepad and over here let me go ahead and save this file i'll go on to the temp directory i'll go on to my docker app so i'll go on docker app i'll go on to bin i'll go on release i'll go on to my publish folder and over here let me go ahead and specify docker file so now i'm going to go ahead and have these instructions in place for creating an image i'll go ahead and hit on save now over here we don't need most of these statements so i don't need these set of statements over here over here what is important is to go ahead and expose port 80 i don't even need to go and expose port 443 over here i do need my from instruction so over here the from instruction basically provides a link onto the base image so based on what do you want to create your application image you need to go ahead and base your application image on some sort of base image so over here we are using the base images which are available by microsoft so over here we're looking at asp.net 3.1 this contains or this image contains the required run time for running your application so remember that you will have now the base image of asp net 3.1 this will be used for hosting your application and then you have your application itself your dll and this will become your image your dl will be running in this runtime so we need the from instruction in place the work directory command can be used to go and specify a working directory for your container now let me go ahead and just copy the expo statement a little bit later on so we have better clarity on our statements now we have our working directory in place we now need to go ahead and copy all of the files in this publish folder onto our image so over here i can go ahead i can go ahead and remove this from statement since i already have a from statement over here i can go ahead and remove this working directory and now i can go ahead and use the copy command so now i can go ahead and copy everything from my current folder onto my working directory so over here i said we're copying everything from our current directory because our docker file is basically located over here so we want to go ahead and copy everything right from here from our current directory on to our working directory which is slash app and then i'm going ahead and exposing port 80 and over here i'm mentioning the entry point so i want to go ahead and run dockerapp.dll as soon as the container is launched let me go ahead and click on save so even if you go on to the published version of your dot net core application over here you can see you have a docker app dot dll right so over here we have gone ahead and formulated a docker file now let's mark an end onto this chapter and let's go on to the next chapter wherein we'll go ahead and build our image so now in the last chapter we had gone ahead and create a docker file that will be used for creating an image out of our application now at the moment we have not gone ahead and created the image so now let me go on to command prompt or we can go on to powershell so over here i'm in command chrome so let me go on to docker app cd docker app over here i need to go on to the bin folder right and then next i need to go on to the release folder and next i need to go on to netcode app 3.1 and then i need to go ahead on to the publish folder and over here we have our docker file in place now to go ahead and build an image we have to go ahead and use the docker build command over here we are giving a name of the image so we're going to go ahead and create an image with the name of my app and over here we are saying that please use the current directory has the context for building this image in this current directory we also have our docker file in place so i'll go ahead copy this command and let's paste it over here and let's do a docker build so over here it has gone ahead and now created an image over here on my local system if i do a docker image ls to go ahead and see all the images so over here i have my docker app image i have the one which is stacked to my repository i have one that is tagged onto my dev environment i have nginx and now i have my my app image also in place now i want to go ahead and publish this onto docker hub so over here i need to first of all go ahead and log into docker on docker hub now it is authenticating with existing credentials if this does not work go ahead and do a docker logout and then do a login again now over here i am going to go ahead and tag now my image so i'm using the docker tag command so it's like having different versions for your images you can go ahead and version images with the help of tags so you might have image version 1 image version 2 etc over here let me go ahead and copy this command so currently my app is with the latest tag and if you don't go ahead and mention attack so over here you can go ahead and mention a tag of let's say v1 so let's say this is v1 version of your application you can go ahead and mention that has the tag over here then you can go ahead and push this onto docker hub now since we have gone ahead let me go ahead and modify these commands since we've gone ahead and specified v1 if you didn't specify any tag by default the latest tag will be taken now let's go ahead and push this image and tag on to docker hub so now it's going in and pushing it on to our repository let's wait till this is complete right so this is complete now if you want to go ahead and verify our application is working as it should our image is fine we can actually go ahead on to azure and let's go ahead and create a new azure container instance over here let me go ahead and choose again contain instance please note you can't go ahead and change the containers for your existing container instances you can make your container instances pull down newer versions of your images that you've already specified but you can't go ahead and specify a different image for a container instance so over here i'll go ahead and hit on create i'll go ahead and choose a resource group i'll give a name over here i'll choose docker hub over here let me go ahead and give the name right so it's my app and v1 i'll leave everything as it is networking also has it is advanced tags review and create and let's go ahead and hit on create and let's come back once we have the container in place now once i have the container instance in place i'll go ahead on to the resource over here let me go ahead and copy the public ip address i'll go on to new tab and we can see the home page for our application everything works as it should right so this marks the end of this chapter now in the last chapter i had mentioned that when it comes to azure container instances you can go ahead and make sure that it pulls the latest version of your image right which is available in azure container instances now the way that you can actually do that is to go out and ensure that you have a newer version of your image available so let me go ahead on to visual studio over here let me go on to pages i'll go on to the index page and over here let me go ahead and make a change so over here let me go ahead and say welcome to docker i'll go ahead and save everything now again i'll go ahead and hit on publish i'll publish onto the folder now once this is done if i go ahead on to my local machine now currently i said that the azure container instance can only go ahead and point on to one container at a time and currently we have actually gone ahead and pointed it on to b1 that's the tag so we have to ensure that even now the change that we have is made on to the same image tag what you can do is that you can actually go ahead and have multiple tags for your images so you could have a v1 tag and you could have a latest tag as well and you can go ahead and push both onto docker hub and then you can go ahead and always ensure that the azure container instance takes the latest version of your image since i was trying to explain to you how you can create a tag for your image that's why i'd used an option of v1 or version one but you can have multiple tags for your images currently since my azure contain instance points on to v1 from my app that's my app image let's go ahead and keep it as it is now currently we are in the publish folder now what happens if i go ahead right and do a build of my project again to create an image so if i go ahead and do this so it has gone ahead and create an image over here let me go to uh docker image ls and over here i can see my app that is latest that was created 30 seconds ago now i already have my repository slash my app with the tag of v1 so i have to first go ahead and delete this image so that i can go ahead and tag now my latest image with my repository name so for that i can go ahead and do a docker image rm and over here i can go ahead and give the name of my image basically which is shaken steve slash my app v1 so it's gone ahead and deleted that particular image so if i do a docker image ls i don't have it in place now again i can go ahead and tag my image my latest image then i can go ahead and again push the image on to docker hub and while this is being done let me go on to my container instance and let me go ahead and hit on stop once it is stopped over here let's wait till it is pushed onto docker hub right so it's pushed let's go back on to the container instance and let me go ahead and hit on start so now it should go ahead and pick up that image again pull down the image again from docker hub and remember arm image should be having now the newer version of our application now once my container instance is running it will actually be allocated a new public ip address so let me go ahead and take that public ip address and go on to a new tab and over here you can see our new application in place welcome to docker so in this chapter i want to explain to you how you can make a change onto your application and then publish that image onto docker hub now in this chapter i want to go ahead and now give an explanation on to the dockerfile that gets generated by visual studio now this is known as a multi-stage build or a multi-stage docker file over here the different from statements that we have actually go ahead and define the different bills in this docker file it actually helps to go ahead and create a more leaner image so remember earlier on what we had done from visual studio we had gone ahead and published our application that published would actually be done by building the application first and then doing a publish and then we had a very small docker file and using that docker file we went ahead and created a custom image now this particular dockerfile does everything for you it does the build it does the publish and then it goes ahead and creates an image so we are doing everything has one operation over here instead of manually going ahead building our application publishing our application we are doing everything via this docker file so as you can very well imagine when looking at the statements when it comes to building and publishing your application it is done with these set of statements so in these set of statements it is first going ahead and using the image which has the sdk the software development kit for version 3.1 of dotnet that is used to go ahead and build your application because remember if you want to go ahead and build a net core application you need that sdk in place so over here it is going ahead it's creating an environment to build your application itself by using this particular image then it's going ahead and copying the project files doing a dotnet restore copying all of the files after the restore operation is complete and then it's doing a dotnet build onto the build folder then it is going ahead and using that particular stage which it just created and is then doing a dotnet publish so over here these are all the internals which are done automatically by visual studio whenever you go and build your dot net core application so once these stages are actually complete so then we come on to the final base stage over here so in this final stage it is going ahead and taking the artifacts from the publish stage over here once it has gone ahead and copied the artifacts which are in the publish folder it will go ahead and dispose of these images of these containers it's not required anymore because see the sdk is quite a big image so this image was only being used to go ahead and build your application it's not required anymore once the artifacts have been copied on to the final stage of the docker file it then just goes ahead and ensures that all of the files are in place and then we have the entry point statement the entry point says please go ahead and run our application when the container is started so over here this is the concept when it comes to using a multi-stage build in your docker file over here you're building your application and then you're going ahead and creating a runnable version of your application hi and welcome back now in this chapter i want to go through azure container registry so so far we have been publishing our images on to the public docker hub registry but if you want a private registry only in azure you can go ahead and create an instance based on azure container registry so over here in all resources let me go ahead and first create an instance of the azure container registry over here let me go ahead and search for container registry so i'll go ahead and choose that i'll go ahead and hit on create over here i'll go ahead and choose a resource group i'll go ahead and give a name for the registry so that's not available right i'll leave the other things has there are gone networking i'll gaunt encryption tags review and create and let's go ahead and hit on create now let's come back once we have the container registry in place now once the deployment is complete i can go ahead on to the resource and over here if i go on to repositories so currently i don't have any repositories in place now i'll go on to visual studio so let me go on to my index page over here let me go ahead and make a change so i'll go ahead and say welcome to azure container registry i'll go ahead and save everything now i'll go on to my project i'll right click i'll hit on publish i'll go ahead and create now a new published profile i'll go ahead and choose docker container history i'll go on to next i'll go ahead and choose as your container history i'll go on to next over here i'll go ahead and choose the registry that we just created and let me go ahead and hit on finish and then let me go ahead and hit on publish let's wait till this is complete now once the publish is complete so if i go ahead and hit on refresh now i can see my repository in place so now you can see that it has also gone ahead and attached the latest tag now let's go ahead and create now a container instance based on this repository so i'll just go ahead and open up all resources in a new tab and before i go and create the azure container instance is one thing i need to do for my registry over here i need to go on to the access keys and over here i need to go ahead and enable the admin user so this is required when you want to go ahead and pull out images from the azure container registry so i'll go on to all resources i'll go ahead and click on add i'll go ahead and again search for container instances i'll go ahead and hit on create over here i'll go ahead and choose my resource group i'll go ahead and give a name for the instance now i'll go ahead and choose as your container history and over here we can go ahead and choose our image and the tag i'll go on networking advanced tags review and create right so everything is in place it's port 80 over here let's go ahead and hit on create and let's come back once this is complete now once the deployment is complete i'll go ahead on to the resource i'll take the public ip address i'll go on to a new tab and over here you can see our application is running so the only difference is that we have gone ahead and published our image onto a private container registry in seo now in this chapter i want to go through the mounting of volumes which is available for your containers this can be used for persisting your data so earlier on i explained that when you go ahead and create a container out of an image out of all of the readable layers of the image when it comes to the container you get a very thin writable layer now whatever data you write on this layer will be destroyed once the container is stopped or even when the container is removed in the end so if you want to go ahead and persist data so you want data to be there even after the container is not present then you can actually go ahead and mount a volume onto the container so in docker you can actually go ahead and create volumes those volumes will be persisted on the underlying disk of the system and then you can actually go ahead and write data onto that volume so let's go ahead and see how we can accomplish this so these are the commands we are going to go ahead and execute as part of this chapter so first using the docker commands we are going to go ahead and use a docker volume command to create a volume over here we are going in and giving a name for the volume over here then we can go ahead and list the volumes just to ensure that we have the volume in place then we are going to go ahead and run a container now this container will be based on the image of ubuntu and please know that we are going to go ahead and make use of linux containers right so we're going to go ahead and launch ubuntu on our ubuntu subsystem which is running on our windows system so over here i'm using the image of ubuntu so we'll create a container based out of this now we are going to go ahead and run this both in the daemon mode so it will run in the background at the same time i'll also go ahead and in interactive mode so that we can actually go ahead and interact with the underlying container over here i can go ahead and now use the mount flag to go ahead and mount the volume that we created earlier on and then we can go ahead and say that please go ahead and target amount point of slash app for this particular volume so remember this is a docker volume that will be residing on let's say the hard disk of my workstation and this volume is going to be mapped onto the container within the container there will be a directory called as slash app and then we can go ahead and write our contents over here when we go ahead and write the contents onto this directory remember it is being written on to the hard disk which is attached onto my computer so that's the idea over here you are creating a volume and then you're doing it and assigning it on to amount point on to the container then we'll go ahead and attach on to the running container so over here we have to go ahead and get what is the container id and then we can go ahead and write a file onto the container this time it will be written onto the volume so let's go ahead so firstly let me go ahead and create a volume so here on my ubuntu subsystem i've gone ahead and created the volume then we can go ahead and list the volumes so we have the volume in place now let's go ahead and run a container based on the ubuntu image so since it's not able to go ahead and find the image locally it will go ahead and download the image onto the local system so let's wait till this is complete now once this is complete let me go ahead and list down the containers so over here i can see the container id so now let's go ahead and use a docker attach command to go ahead and attach on to this running container 585 so now i'm logged into that ubuntu container if i do an ls so over here you can see we have the app mount point or the directory over here so let's go on to it so i'll go on to cd app and over here let's go ahead and write some contents onto a sample.txt file right so i have the file in place right so this is a container now remember this is going to be written onto my hard disk on my computer now if you want to go ahead and see where these volumes are created so you can actually go if you're on windows system you can go on to wsl dollar this will actually go ahead and direct you on to that subsystem the windows subsystem for linux over here i can go on to docker desktop data version pack data community docker and the volumes now please note that i've seen in a lot of forums that this is not the exact location for the volume so even for me it took some time to actually figure out where the volumes are stored on my system so i not seen a very consistent location for the storage of the volumes but then when i searched my system this is where i actually found the volumes so if i just go ahead on to windows explorer over here i can see my volume in place and over here i can see my txt file so i said now this is being persisted has a volume on my local system so now what we'll do is that let's go ahead and exit from here so i'm back on to my ubuntu subsystem over here let me go ahead and again see the running container so i don't have the container in place as soon as i exit from the container the container has stopped now let me go ahead and see all of the containers right so i have my ubuntu container in place now let me go ahead and remove the container right so it's removed so if i go ahead and look at the containers i don't have the container in place but remember we still have our volume in place so over here let me go ahead and again run the container command to create a container based on the ubuntu image over here now it should have mapped on to my volume so let's go ahead and see our running containers so it's 25c so let's go ahead and attach on to this container right now here we should be having again our app folder and let's go ahead and see the contents and i can see my sample.txt file so the entire purpose of this chapter is to let you know if you want to go ahead and persist data you can actually go ahead and create volumes in docker now in this chapter i want to go ahead and show you how you can create a container based on the mysql database so the mysql database is a popular sql based database and when it comes to developing workloads on containers so you have your front-end which could be a dotnet code application which could be a java based application and then you have your backend where is your data being stored so your data could be stored in a sql server database in an oracle database in a mongodb database in a mysql database so one of the most popular images or containers when it comes to a data store is my sequel so this is a database that you can actually go ahead and use has a container so over here i'm going to go ahead and use the docker run command over here the name of the image is mysql so if you go on to docker hub there is an image with the name of mysql over here i am giving a name for the container so it will be my sequel instance over here i am just giving an option to restart on any sort of failure i am saying please go ahead and run in the background over here we can actually go ahead and set environment variables so when it comes to this particular docker image you can actually go ahead and specify various environment variables over here i'm just saying that please go ahead and set the root password so you can actually go ahead you know by default there'll be a user known as root that will be created when you go ahead and create a my sequel instance over here you can go ahead and make use of this environment variable which is predefined so it's my sequel underscore root underscore password and over here you can see this should be the password for the root user so let me go ahead i'll execute this command in my ubuntu subsystem so let me go ahead and run this command so i think i'm still in the container house for the last chapter so i'll exit from here now let me go ahead and run the command to run my docker container so since it does not have the mysql image locally it will go ahead and pull the mysql image from docker hub now once this is done so i can see i have a container based on the mysql image so once this is done let me do a docker ps right so i can see my my sequel container in place now i can go ahead and use a docker execute command to go ahead and execute a command to the container over here i'm using the interactive mode i'm giving the name of my container and the command i'm actually executing is my so normally when you go ahead and deploy a mysql database it will also go ahead and have the mysql client in place the mysql client allows you to go ahead and log into the database itself the database server so over here mysql relates to the mysql.exe or the client that can be used to log into the database server over here minus you means to go ahead and specify a username so over here the username is root so please know that if you're not familiar with mysql commands this is the way you can actually go or one of the ways you can actually go ahead and log into the database server remember our database over here is being run in a container now and over here slash b means we need to go ahead and enter the password so let me go ahead and run this command and the password is azir one two three and here we are logged into my sequel and remember this is my sequel not running on the ubuntu machine it's running within a container on my ubuntu machine over here you can see all of the databases which are in place right so in this chapter i just want to go ahead and show you how you can have a mysql database instance running in a container now in the last chapter we had seen how we could create a mysql container based on the image we went and we logged into the mysql container we execute some commands now let's say you want to go ahead and expose the ports of the mysql container so that you can actually go ahead and connect onto that mysql database server from visual studio so for that we are going to go ahead and execute the same command but over here we are going to go ahead and do up port mapping so the reason i have gone ahead and separate these two concepts is because again i want to go ahead and emphasize you know how containers actually work when it comes to the networking aspect in the previous chapter the mysql you know container was running in isolation even over here it's running in isolation but over here we are now exposing the port so that we can actually go ahead and connect onto the mysql instance from let's say visual studio on our local machine later on we are going to be looking at how to deploy multiple containers using docker compose wherein we'll have our container running in a net program and a container running mysql and how do we allow these applications to talk to each other within the containers itself over here in this chapter i just want to go ahead and show you how you can connect from a.net program in visual studio that's running on your local machine onto this mysql instance container now currently i have my mysql container running in place so let me go ahead and stop all of the containers i have currently so let me go ahead and do a direct container removal over here let me go ahead and put the name of my container so saying you cannot go ahead and move a running container so what you can do is that you can go ahead and put the flag of hyphens to go ahead and remove the container and let me go ahead and also remove the other container as well i don't need that cutting actually in place so over here that name of the container is sql app so now i don't or should not be having any containers in place so i have the ubuntu and the engine x that should be fine so let me go ahead and clear the screen so now let me go ahead and start this container now on my ubuntu subsystem for linux not on the container i'm going to go ahead and install the mysql client so over here let me do and app get installed my sql client so right now i've installed my sql client on my ubuntu subsystem now over here i won't run the docker execute command instead let me go ahead and now directly run the mysql command so let me go ahead and copy this command i'll paste it over here now over here the difference is i need to go ahead and mention what is the force name which is basically the localhost ip address right and we are connected on to mysql now over here since this is a new container we don't have again our data in place please know that again you can go ahead and actually mount volumes for your mysql database over here let me go ahead and run all of these commands again just to ensure that i have the database and the table in place and we come back once i have everything in place so once i have all of the data in place so now in visual studio i have a dot net program in place this is a similar program we had seen earlier on i had actually gone ahead and developed a simple program to connect or get a list of courses from a microsoft sql server database over here now i am going ahead and getting it from the my sequel database so over here everything remains the same except for the packages and the classes i'm using so if i go all tools if i go onto you get packet manager and manage nuget packages for the solution over here i'm using the nuget package of mysql.data and over here i'm going in and using the classes that pertain on to my sql over here in the connection string so i'm mentioning what is the server that is my host what is the user what is a password and what is the name of the database so let me go ahead and run this program so over here you can see you're getting the list of services from the courses and it's going out and picking up this information from the mysql database so remember as a developer you might go ahead and deploy a mysql container on your local machine that would be hosting data and before you can actually go ahead and deploy a docker container with your application in this case let's say our.net core application obviously from visual studio we will first go and develop debug and make sure the application is working because remember in the end your container is just a deployment environment it's not your development environment you will still first go ahead and develop your application and then deploy it as a container now in this chapter i just want to go through a use case when it comes to working with blobs and when it comes to working with containers so you've seen earlier on how we could go ahead and use docker to go ahead and run your containers earlier on we had also seen that we could go ahead and persist data on to volume so we could go ahead and attach a volume onto a container and data could be written onto that volume so over here in this chapter i want to go ahead and take a blob that's known as courses.json from one of my storage accounts which is in the data container so if i go on to azure i have a storage account in place if i actually go on to my containers if i go on to the data container over here i have a courses.json file in place if i go on to edit so these are the contents of that particular file so what i'm going in and doing is that i'm going ahead and using the blob classes to go ahead and download the file from the storage account but this particular program is going to go ahead and run in a container so i'm going to go ahead and create an image out of this application using dockerfile so i've already gone ahead and basically added docker support for this particular project over here i've gone ahead and chosen linux has the underlying docker support platform and over here we have the normal docker file in place over here in the program i'm saying that please go ahead and create the blob in the local app folder so that app folder will be basically our target mount point over here we are going to go ahead and create a new docker volume and then we go ahead and run that container using that particular volume now let me go ahead and publish this onto docker hub now over here i already have a published profile in place that will go ahead and publish this as an image onto docker hub so let me go ahead and hit on publish let's wait till this is complete now once a publish is complete if i go ahead and just refresh the page for my repositories so over here i can see blob project in place so over here let me go ahead and first create a new volume so over here in one of my sessions for ubuntu let me go ahead and create the volume and then over here let me go ahead and create now the container out of that particular image using that volume right so that is done now if i go ahead and do a docker ps so over here i can just see the container this is basically on my development environment so i could go ahead and stop this actually once this is done if i go ahead and do a docker ps and see all of my containers so over here you can see the blob project based on my repository in docker hub it ran it also exit 53 seconds ago so what happens that container would just go ahead and download the blob and then it would go ahead and exit but just to confirm that the blob was downloaded so in windows explorer let me go ahead on to the volumes over here let me go on to blob volume i'll go on to data and over here you can see the courses.json file right so everything is in place so in this chapter i want to go through a use case scenario where you can go ahead and download blobs in a container using volumes hi and welcome back now in the next set of chapters i'm gonna go through docker compose so this is a tool that can help you define and run multi-container docker applications over here you can go ahead and make use of a yaml configuration file to define what are the containers that you want in the running state now in large organizations if they want to go ahead and deploy applications has containers they might have many containers that need to be deployed especially if the application is following the microservices architecture when you go ahead and look to following the microservices architecture then you'll go ahead and expose each functionality has a service so let's say that you have a customer service a service that gives you order information a service that gives you information about the products and then you have application that goes and consumes all of these services you then go ahead and deploy each service has a separate container if you try to go ahead and deploy one container after the other it's a nightmare even for me even if i'm just trying to go out and manage two or three containers it's quite difficult if you want to go out and just deploy them one by one it takes time so what you can do is that you can actually go ahead and make use of something known as a docker compose file so this is a deployment mechanism that is available from docker over here you will go ahead and specify a yaml configuration file that yaml configuration file will go ahead and define the services and the containers that need to be deployed now we're going to go ahead and see how to deploy a simple docker compose file for just a database service and then we'll go ahead and see how to up a docker compose file for both an application and a database service now over here in visual studio code i've just gone ahead and opened up one of my local folders over here i have something known has a docker compose yaml file now if you want to go ahead and download and install visual studio code you can go ahead and do that this is a light editor when it comes to developing applications over here you can go ahead and develop your dotnet even your python based applications your java based applications as well so you can go ahead and download this tool for free so over here i have a docker compose yaml file in place now if you want you can go ahead and install the recommended extensions for docker compose as well so if i go ahead and hit on install so in visual studio code you can actually go ahead and install something known as extensions to go ahead and extend the functionality of visual studio code itself over here it has gone ahead and installed the docker extension so that you can actually go ahead and work with docker files with docker compose files etc so going back on to my explorer over here i have my docker compose file in place over here i'm mentioning what is the version of the docker compose that i want to go ahead and use so if you actually go on to the docker documentation over here you can look at the matrix when it comes to the different compose file format versions now over here i'm going ahead and defining a service so i'm saying i want to go ahead and run a container has part of the service so you can go ahead and define various services over here then i'm going in and giving the service a name and then i am saying please go ahead and create a container based on the image of my sequel over here i'm doing a port mapping i'm saying that if there is a failure go ahead and always restart and then i mentioned that if you want to go ahead and specify environment variables we had seen this earlier on when we went and spun up a container based on mysql there we had gone ahead and specified the mysql root password there are other environment variables as well so if you want to go ahead and specify a mysql user its password what is the database etc then over here in the volume section i can actually go ahead and tell my sql the container basically go ahead and run all the scripts that i have in the init folder so over here you can go ahead and pass it on to the default you know file that gets executed the entry point file that gets executed when my sequel container starts over here you can see that please go ahead and execute all the scripts i have in my init folder so over here in the init folder i am saying please go ahead and create a course table and please go ahead and insert data so as soon as my container is up i'll have this data in place then i don't need to go ahead and actually keep on entering the same data so now let's say i want to go ahead and now create my services create my containers based on this docker compose file so firstly i'll go on to my ubuntu subsystem so over here i can see i'm on an existing mysql i'm connected onto an existing mysql container let me go ahead and exit let me go ahead and see all the containers that i have so i do have this running container in place let me go ahead and forcefully remove this container right let me go ahead and see all of the containers so i only have the ubuntu and nginx container that should be fine so now i want to go ahead and run this docker compose file now remember over here i am actually connected on to my ubuntu subsystem which is running on my windows system and over here in visual studio code all of these files are in my c colon temp to directory now if you want to go ahead and browse on to this directory from the ubuntu subsystem it's very easy over here let me go ahead and just clear the screen so over here i can actually go on to mnt folder and over here if i just do an ls so i can go on to my c drive then over here i can go on to my temp 2 folder and over here i have my docker compose file so from your ubuntu subsystem you can actually go on to the contents in your c drive now over here you just go ahead and execute the docker compose file you can go ahead and use the command of docker compose so let's go ahead and do that so now it's going to go ahead and take the commands which are in the docker compose file and it's going to go ahead and now deploy a mysql container right so now it is ready for connections remember over here we are now deploying our container via a yaml configuration file it's making it much more simpler now when it comes to deployment of containers so now in another ubuntu session if i go ahead and just look at my containers so i can see a container is running now remember we have gone ahead and installed the mysql client on this ubuntu subsystem so it should be available so let's go ahead and connect now onto the mysql container now over here i'm going to go ahead and use this my sequel user and the password is ser so over here let me go ahead and mention the username and password and now i am connected and now over here if you go ahead and show databases you can see you have your app db database in place let's go ahead and use appdb show tables we have our course table in place and over here you can see all of the information so now we've gone ahead and showed that in addition to our container being up and running we were also able to go ahead and pass in a script that goes and set up the data in the underlying database now in this chapter i want to go ahead and show you how you can use docker compose to go ahead and deploy both your database has a service and a net webcore application also has a service so over here in my temp folder i have a visual studio project in place so i'll just go over and double click on the project file so again this contains or consists of my asp.net core application which goes ahead and fetches all the details from the course table in a mysql database so we've seen this earlier on as well so there's no changes over here so if i go on to my services if i go on to the course service so over here this goes ahead uses the mysql classes to go ahead and fetch data from a mysql database now over here in this particular project i also have a docker file so i've just gone ahead and created my own version a simpler version of the dockerfile wherein i'm again making use of multi-stage builds over here again i'm going ahead and using the sdk to go ahead and create that build environment copying the files running dot net restore and then going in and doing a publish and then finally going ahead and building the runtime image so this is my dockerfile so dockerfire will go ahead and take my.net code application and bundle it up has a custom image now over here i also have the docker compose file over here the services for the database remain the same so over here again when it comes to the volumes i again have the init folder so as part of my project again i have the init folder and i have the sql file to go ahead and create the table and add the data into the table going back on to my docker compose file so over here i have the db service which will be running and i also have the web service now as well over here i am telling the web servers please go ahead and use the docker file which is in the current context that is in the current directory to go ahead and build the image it will serve a web layer so this docker compose file is going to go ahead and also now build our custom image based on the docker file this will be for the web layer for the my sequel we already have an image in place over here i can go ahead and give a depends on clause saying that the web service depends upon the availability of the db service over here i'm going ahead and doing a port mapping so that i expose port 80 of my web application and over here just has an extra scenario i'm going ahead and creating a new docker network so you can go ahead and create new networks in docker over here i am just going ahead and telling to create a new network known as app network and then over here in the environment for the web service i am saying the db host is db that is basically our db service over here what is the port number 3306 now if i go ahead and scroll on top so my service for the database is named as db so if i go on to my course service over here in the connection string so the very important in the connection string you have to know what you have to place has a server what should be the host name over here in the host name i am now going ahead and giving what is a service that is specified in the docker compose file so this is important so let me go on to my ubuntu sub system let me go ahead and exit let me go ahead so let me go ahead and just see the containers that i have let me go ahead and stop all of my containers so i'll go ahead and remove the container as well right once this is done let me do and see all of the containers so only ubuntu and nginx that should be fine let me go ahead and clear the screen again i'll go on to my mnt folder i'll go on to c drive i'll go on to tem2 over here i have everything in my my sequel project over here i have my docker compose file so let's go ahead and create our services while the docker compose file so over here it's going at and building my web layer first so it'll go ahead and first build the custom image that is required for the deployment of my web layer so let it go ahead and carry out all of the required tasks so i can see that my web layer is now started and now it's going in and initializing my database layer right so everything is working as it should now in chrome let me go on to localhost and over here if i go on to port 5000 over here i can see my application in the running state so my container is basically listening on port 80 and i've gone ahead and mapped it on to port 5000 of my local machine so now i can go ahead and browse to my application via a different port number over here but now you have your web layer running has a container and you have your database layer also running has a container if you actually go on to your docker desktop dashboard if you go on to containers and apps over here you can see an entire project in place based on the docker compose based on the name of my project and over here you can see a separate web layer and you can see the database layer right so in this chat just want to show you how you can use docker compose to go ahead and start two containers at a time
Info
Channel: Alan Rodrigues
Views: 1,323
Rating: undefined out of 5
Keywords: AZ-204, Azure Container Instance
Id: 12rALxPDUEI
Channel Id: undefined
Length: 74min 58sec (4498 seconds)
Published: Thu Oct 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.