Deploy A Python Flask App With Docker On Azure

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys so today we are going to go ahead and learn how to essentially take a flask application put it in a container put that container on measure and deploy it okay so we're gonna walk through this step-by-step there are four main steps step one is going to be create the flask application step two is going to be create the docker file step three is going to be create the docker image there's going to be more than three steps I four steps step four is going to be build the docker image or a you know build and create the docker image build the docker image step five we're going to create an azure container registry which is actually just like the docker container registry it's a repository for containers and that we're going to go ahead and then push our newly created container up to that repository for containers after that we're gonna go ahead and deploy it on a platform a managed service known as Azure app services and it's gonna host our containerized flask application and we're gonna do this all in hopefully twenty minutes so let's go ahead and do it alright so first thing we're doing we're creating a Python application so we're gonna go ahead and create our app py file and the next thing we're gonna do is go ahead and create a virtual environment and we're going to be using Python 3.8 for this so I've gone ahead and basically what I've done I installed Python 3.8 and then I did a pip or a pip 3 install virtual env so I already have this this this package here so it's virtually envy so I already have this package here but essentially what it is is it allows us to isolate our Python packages in this specific directory so they don't affect our global directory in our computer and that allows us so when we're developing multiple different Python programs we don't actually mix up any packages in there so we don't want to do that so we're going to go ahead and create a virtual environment to isolate our Python packages that we download in this directory here so in order to do that we say a virtual E&V ve and V and it's going to go ahead and start to create that for us so we're only going to need one package and that is going to be the flask package so we're going to go ahead and install the flask package as soon as this virtual environment is created I says we noticed a new virtual environment has been created do you want to select it for the workspace folder doesn't really matter but we'll say yes for the for the heck of it so what it's in do what it's doing here is installing the tools for the in our virtual environment that are necessary for essentially downloading and building Python packages so when this is done we're gonna go ahead and now say pip install flask and that will install flask now we can say from flask import flask and then we can create our API server object so we can say app equals flask using the using the function that we call imported from our flask library and we're gonna pass the the the name and it would pass the name just a parameter there and that's just the name of the file here so now what we're gonna go ahead and do is is now we're gonna go ahead and add an endpoint so we're gonna say after route and we're just going to have the base base endpoint so essentially there's not going to be any additional routes thrown on there this is going to be the index page the home page essentially and then we're gonna define a function under this decorator that essentially is going to return the text hello world so we're going to keep this super simple here and it should be a function so it should look like that all right so now that we've got that we're gonna go ahead and go so if if name equals main so is if this is the full if this is the I misspoke earlier when I said name would be the name of the file so name is the is the width in this case is main is to file that we're running it's the main file or running style Python interpreter so if name equals main app dot run and we're going to put specify a host is going to be 0.0.0.0 and our port is going to be 80 all right so again the host is the IP address of where the the the application is going to be with the IP address we assigned to this running web server and then the port is where it's going to be receiving traffic in this case it's H HTTP traffic so it's going to be port 80 and that'll take care of that so we're gonna go ahead now and this right here should now run so in order to activate all I didn't activate the virtual environment okay so we're gonna retrace if you installed flask there google pip uninstall flask because that installed it globally so we'll get rid of that and now we're gonna say so if we do an LS here to see what we have we have a dot vs code which are just some metadata about DBS code editor that's relevant to us so we're gonna say dots a dot slash VNV scripts activate and then I'll go ahead and activate our virtual environment this is for a Windows environment so if you're off you're on a Windows and a linux-based a UNIX based environment and the command is going to be source V and V bin activate and that'll be that'll take care of that for you okay so now we can go ahead and install flask alright so once we install flask what we're gonna need to do is create our docker file file and in our docker file what we're gonna do is we're gonna go ahead and pull the latest Python docker file image so we can say is from Python three point eight point zero and what this is gonna do is essentially pull down what again what a docker container does is they're virtualizes a kernel and what that means is it basically allows us to use the very bare minimum amount of resources when we're and isolate our application with these bare minimum amount of resources then we can go ahead and pull in whether whatever other dependencies you can we might want so we're gonna say from Python three point eight point O then we're going to create a working directory and that's going to be a user source app and so this is going to be the active directory as if we were you know just clicking around down here whatever this is my active directory this will be our active directory now we're going to go ahead and say copy we're gonna copy the requirements so in this case here we want to be able to just copy over the specific requirements or our packages or a list we'll say well it makes more sense if I do it but a list of requirements essentially that are the packages that are required for this application to run so we can do here is say pip freeze requirements txt there freeze and then with a little caret I believe it's I don't know other things called carat but a carat facing to the right and that I'll essentially pipe the requirements from pip into a requirements txt text file and create that so now we did that and we see this requirements not txt with all the with all the dependencies that are installed in our current virtual environment okay so now we can go back to our docker file and now we want to do is actually copy that requirements file over into our docker file so we're going to say copy requirements copy requirements txt into that working directory and then we want to go ahead and say pip run we got to run the command run pip install - our requirements txt so what that'll do is go ahead and actually install iteratively all of those requirements for us and then we can go ahead and have a now we can go ahead and have a command that executes our actual application after we copy that over so we're gonna sell ahead and go ahead and say copy dot dot which will actually copy over all of the files that are in that local directory that we're in currently so what you see here basically we're hanging for this app that py I'll copy that over and then we were gonna go ahead and and once we copy over that app dot py file into our docker container or docker image that we're building here now we can go ahead into an entry point in which case we just call the command Python app dot py all right and so when we call a Python app dot py essentially what we're doing is we are going to head and so listen or review this from the top but essentially to finish we're just executing the program but let's review this so again we're pulling a virtualized kernel with just enough resources to essentially run our Python program we're defining a working directory we're copying the requirements we're covering the requirements file into that working directory we are then running pip install - our requirements dot txt to install all those requirements in that and this image new image we're creating then we're copying over our app file and then we're defining an entry point for when to for how we're going to execute this and now the reason we're copying these things over in steps here is because docker actually caches these individual build steps to make it the faster to build subsequent images when you're rebuilding this image so you do this kind of in separate in layers essentially so that's what that's what's kind of going on here so now let's go ahead here and try to build this image so just so we can verify that if we wanted to we could run our application first here we can say Python app py and an attempt was made in a socket I might have a docker process running there I do docker kill confident Louie oh all right No [Music] oh confident Lewin there it is okay so that's a way you oh by the way a couple commands I - a docker PS this will show you all the doctor processes that are running previously I did have a docker process running and it was listed as you can see here this right here but now I I killed that process so that is no longer running pour confidently with me so now we're gonna go ahead here and we're going to actually we're gonna run this container verify that our app is working all right it is we don't need to go through all this so we can see it's running on this point here - 0.0.0.0 port 80 and I wouldn't go ahead and actually build this docker image so we can go and we're gonna do that now so we're gonna say docker build - tea and then we're gonna tag this image we're so we're gonna tag this image with a some kind of metadata that allows us to reference it so docker build - tea we're gonna call it flask flask app and just flask app 0 or flask at or just gonna yeah flask at 0 we'll call it for the first iteration and then we're going to put just a dot which symbolizes that the docker file that we want to build with is located in this current directory so now we can go ahead and do that and hopefully this is gonna work for us so now what it should be doing is again it's gonna pull down that initial Python 3 point 8.0 docker image which might take a little while and then it's going to go ahead and run and execute these commands so while it's doing that we're actually going to flip over to the Azure cloud and especially than I thought okay so we're gonna flip over to the azure cloud and what we're gonna do here is create a an azure app and that an azure container registry so we can do is if you guys haven't yet go ahead and sign up you get $200 in free credits from Microsoft Azure which is nice for hosting your own random applications if you want to force a school projects or for for your own money-making endeavors or whatnot so we can go ahead and put we want to go to container registries and Azure we're gonna add a container registry and again this is going to be repository for a container or later gonna push it up to this and then we're going to go ahead and reference this container use that rough this container in this repository when we're acted and deploy it directly from there so registry name you just call it flask app that's already in use so we can say these are global namespaces so they have to be somewhat unique so we're gonna say John John's flask app John's flask app flask apps and so now we're going to go ahead and do we have a subscription choice now so when you guys sign up your subscription is essentially like okay a free trial pay-as-you-go pay with Microsoft support like all these different things it's just a kind of a subscription is a way to isolate B cost of your of the resources that are going to exist within that subscription so now we're going to go ahead and create a resource group and a resource group is similar to what you might imagine a class to be in a net program so essentially it's a way to logically isolate a set of resources instead of vm's a set of services on Azure cloud and so for the azure container registry we need to select a logical isolation for it and so we're actually is gonna create a new one as we go here so we're gonna go ahead and call this one John's flask apps keep it simple here so we're going to name the same as our container registry now we have a location you ideally you put this location as close to you as possible in this case to minimize any kind of latency when you're pushing and pulling docker images and whatnot and then we just have standard premium or basic for I think it believes bandwidth right more performance and scale yep so it's that'll that'll so we're gonna go with standard under standard for that and now we're going to go ahead and click create so what this is gonna do is create that container registry let's check back and see how this building was done so looks like we successfully built our container image so you see essentially went through these steps here so we have it basically go ahead and executed these different things you can see said copy requirements like txt over Iran pip install lash our requirements txt installs these flat and a few of flasks other dependencies then we go down and we see copy moving over the rest of our files and then we go ahead and we tag our flask app successfully tagged it so we have a way to reference it with some metadata so essentially now we can go ahead and test running this thing so we should be able to say docker image LS and this will show us a bunch of docker images and right here we have our flask apt 0 that we just created so if you guys see at the top you probably will see that as your first one once you've installed doctor on your machine so we can go ahead now and say flask we can go ahead say docker run and we want to specify the port so we're gonna specify the port on our local machine which is going to be 80 and it's going to map to the port that the web server is running on in the container which is also a T so we specify that by saying docker run - P and then 80 colon 80 they were going to say a dash D and the dash d stands for detached so we don't want we're not going to run this in an interactive mode it's going to run it as a sub process and then we're going to go ahead and nobody specify our image so we say flask flask app 0 alright so you see now it has outputs like a little hash there and now we get to go see something went wrong so we see that well now we're going to run docker PS there's no image run there's nothing running so sometimes something probably went wrong so we can try to run this in interactive mode and see what went wrong here and there's nothing that's not good so why would that be happening if we're going doctor run - p80 mapping the ports flask app and we have our flask up here no where's our image here's our image flask f0o we run it and so it means our entry Python app duppy why hmm so something's wrong here pulling are pulling are let's try to rebuild without the cache so me I'm not sure what's going wrong here but we're going to try to debug it a little bit so we're gonna say we're gonna specify the no cache flag which will should make us pull fresh see a working directory is still the same I don't know I wouldn't make a difference [Music] let's try was building it with a different version here okay so let's try this again here with our flash gap one okay so for whatever reason it's working now so now we can go to localhost 80 and we can see we can see that so now we get our hello world I'm not entirely sure what went wrong there sometimes it's good to rebuild without your cache dependencies because some kind of some kind of caching might interfere with with what's going on for whatever reason so so now that means that essentially our images up and running we're able to access it by mapping our localhost port or our local port 80 to the container port 80 and then we have this IP address that is to find a run on which is 0.0.0.0 which you can reference my localhost so now let's go ahead here and say we got so open pouponne so now we want to do is push to our azure container registry so what we can do here so the website I'm working on okay so now we have our container registry John flask apps so now we want to do here is actually push to that container registry so how do you do that so the first thing you got to do is download the azure SDK and the azure sdk is what's going to allow us to log in and log into our container registry from the command line our azure container registry we're going to log into from the command line and then we're going to go ahead and push up that container image so I already have the azure CLI installed but if you don't go ahead and Google as your CLI I'll put a link to in the description and it's a pretty easy straightforward install it'll add this to your path and you'll have this AZ command that you can run from your that you can just run from the terminal and you see it's gonna spit out a bunch of options here as to as to what I can do with that all right so so easy is just the azure CLI it's a way to directly interface with the azure cloud from a command line environment so we can go ahead now and say as your login or AZ login now it's gonna put up a window I'm gonna select the account I'm gonna log in with here this is where you would select your account that you want to log in with and now we're gonna go ahead and it says login successful I can now return to my CLI and boom so now I can say a Z a CR list and this is going to say so it's a sure easy Azure container registry list and now we can see here is we have our list of azure container registries and we see a login server right here so we have ad been enabled not yet we're gonna have to enable that later and then login server so just like a docker registry you have a lot a way to log into your or are say even a github account or you have a way to log into it and then you can go ahead and push and pull from that repository so now we're gonna go ahead here and say easy login and we're gonna go ahead and login or AZ ACR login - and we want to put in John's flask app John's flask apps alright so now because of previously authenticated with my account it's going to log me into this registry here so we get the prompt that login succeeded and now we can go ahead and actually push this docker image up to that container registry so let's go ahead here and we say LS and we have our I don't know why I did that it's just kind of have it so now we want to go ahead and push the container registry so we want to retag our image now and the way we're going to do that was we're gonna say docker image LS we're gonna look and find our application and we know it's flask app with the tag 1 is the one we want so we're gonna say docker tag flask flask app 1 and we want to tag it with the name of the container registry in front of that we're gonna scroll up here tour says login server and we're gonna go ahead and copy that then we're gonna say docker tag flask app with the name the container registry a slash and then the flask app 1 so you have docker tagged flask app Wan referencing the image on a local machine that are basically prefixing it with the the foreign repository and then we're gonna go ahead and retie it so it's gonna create another image so we have her docker image LS and now you can see here we have John's flask aperture cRIO slash flask app the tag of one all right so now we want to go ahead and say a Z ACR oh no it's a docker push docker push and we want to push this this image right here so we're gonna copy this and flask app and one so now what's going to go ahead and do is push that entire flask app up to that container registry and we're gonna see that up here in our container registry here shortly so when that's done so guys I know I was going through this pretty quick have you just been watching so far again the necessary things to install our docker on your machine you want to install the azure CLI you want to create an azure account you get 200 bucks in free credit you can employ all your applications and whatnot it's a pretty good deal and then you go ahead and you got this stuff here so yeah so now we're waiting right now for to push that image up to our container registry and then we're going to go ahead and create an app service with it in as your so when we say create an app service with an azure we're gonna use Azure app services is basically a managed service by Azure that allows you to host applications with a certain runtime it also allows you to use you can also specify it to use containers and it's going to give us a free public IP address it's going to set us up with SSL it's overall it's a good solution to one of many solutions to hosting applications on as your so this pushing is taking a while the internet isn't great here so it's keeping rolling but yeah so anyways other good things to just to know about these containers are great absolutely great because they perform they're a really clean delineation between development and DevOps or deployments essentially so again if you have your application your developers can put all their configuration files imagine you have a room and you have you have all your configuration you can hang it wherever you want in the room doesn't matter and then you can just package it wrap up that whole room into container and you can send off that container to your develop into your your ops team your deployment team or whatnot and then they're gonna take you then they don't know need to know anything about what's inside the container they can just consider it a single process and run that as a process and you see that's very powerful in addition the reason that containers are excellent because you can use them with kubernetes and we're gonna we're gonna talk about kubernetes later because i think it's a really cool thing but essentially kubernetes is a distributed we're just gonna talk here well as things question so essentially the kubernetes is a distributed operating system or an operating system for a distributed system okay and a lot and it happens to use containers as its its components essentially so you have a container I don't want to go into kubernetes right now but since we have a container that is that is wrapped up with additional metadata for control and then it organizes and scales up your containers relative to your load and whatnot but anyways containers make you invariant to your operating system they integrate with fantastic distributed operating systems AKA kubernetes and they can be deployed cross cross platform costs a hybrid in hybrid cloud hybrid premise environment and this is taking forever okay so let's go ahead here and we can get this you can look at this app service thing another thing we need to do is actually set the administrator is set the administrator so we need to enable the administrator I'm not I gotta find the command for that and doing while back here so anyways what we do when we set this administrator is it allows us to essentially oops this is where I'd be nice to have ffz F I believe or you can just search previous commands that's racy or haha here is so now we do is we enable the administrator and when we enable the administrator it's gonna allow us to actually connect to the azure container registry from the azure app service so that's just something we need to do real quick here so we're gonna run that update that um oh I realize they cancelled the push hello God okay nobody doing okay so you enabled the administrator let's go ahead and just push that again so that'll take care of that good okay boom so we should have pushed let's go ahead and check it out so we can go to our a sure our container container registries we see John's flask apps we go down here to repositories and then a repositories we see flask app with our version of one alright so now we're in business we have our flask app we can't an Erised it we push it up to our we pushed it up to the azure container registry and now we're going to go ahead and set up that app service so we go to app services we're going to create a new app service here and this app service is again a managed service by as your is going to assign us a public key a public IP address is going to assign give us SSL and it's going to make it very easy we have our resource group I kind of forget what we called it I think John John's flask apps the web app name is gonna be John's let's just call a flask AB then we select docker container so you can do code and you can specify your your runtime like all right like Python 3za runtime Python tow seven is a runtime PHP 7.2 is a runtime you know you know all these different code the specifications essentially your code specs are runtimes so you can specify at runtime and then upload your your own application or we can go ahead and do the docker container which is better so now we get to go and select our location we're gonna put it in East us and we've got our app service plan that's gonna go into effect and this just again it's just kind of a way to kind of encapsulate what's going on here and then we go ahead and we go to our docker configuration so we go ahead and we're going to run a single container from our container registry and it go ahead and auto filled because there was only one there so again we have our registry which is John's flask apps in this case or whatever you named your registry we have a flask app which is our image then we have the tag which is one so next up here we're gonna go ahead to monitoring you can enable application insights which is a way to get really granular telemetry on your application but in this case we don't need application insights and it's not available to us what we let mean ideally you do need application insights or any of its analogs but it's not available to us for this forward for containers in this in this situation here so we're gonna go ahead and set up some tags these are this tags that you can assign to your to your resource that we're creating here which is our resources our azure app service and essentially we can assign tags to track costs and didn't kind of filter our global resources that we've provisioned in Azure by tag and track cost by tag and whatnot so now we're gonna go ahead and we're gonna skip that we don't need to do that so we're gonna review and create so now flask apps not available John's flask all right so now we review and create and now essentially the way asher works is asher is essentially the way Asher has this thing known as the agile resource manager and that is largely an API for every single service that you can provision on Azure and then basically what we're doing here in these blades they're windows with a give them fancy names called blades but essentially these little windows that and these little blades that we have here that work parameterizing essentially when I say parameterizing things like the names like the resource group the name of the docker file the tags etc what we're doing is we're parameterizing what is really a template that is essentially sent to an to an azure resource manager api endpoint and then it goes ahead and it provisions that resource given that template so this is interesting when we look at infrastructure as code later but yeah that's what it's doing so now we're gonna go ahead and click create and we have our deployment that is kicking off here so this is gonna take a little bit of a minute but yeah anyways guys I'd encourage you to look into Azure and not just asher by any cloud platform and mole not any cloud platform stick to AWS or Azure for now those are the most popular in terms of what businesses are gonna be looking for if you're looking for a job or and tend to have the largest set of of offerings as well GC p is obviously a good competitor however if we break it down a sure has a smaller portion of market share overall market share then when in contrast to AWS it has a larger market share than gzp however Azure I believe makes the most money out of their market share so is the platform of choice for many of the largest companies AWS intends to cater again to the whole spectrum of companies but the azure is in Microsoft is very deeply Microsoft tool sets are very deeply embedded in in businesses these days so it's it's definitely to be honest with you I think Azure cloud is the best one to learn yeah you can learn AWS obviously that's still in very high demand but I think that Asscher is kind of adopting on a multi real multi cloud strategy and going full force with that I don't see that and there dude they're doing Microsoft in general is openness is doing a lot of open-source work I don't see that from these other companies and I think that that is potentially the best way forwards in terms of a market share and being well as relevant is it kind of embedding yourself an open-source so now we see our resources popped up here deployment is still underway we're going to give it some time and then we're going to check out that deployment we're gonna see if we can get this same one to ran it locally this little hello world that popped up from running our docker image locally we're going to try to get that from our final our public IP address that's assigned to this as your app service that is running here our container instance so again to rehash guys we created a flask app we created a docker file we went ahead and took that docker file and then created an azure container registry we we took the docker file we ran docker build to create an image and then we took that image and we pushed that image up to the container registry and then once that image was in the container registry we referenced it from our Azure app service and it's going to deploy it on the add your app service which is again a highly available service that offers us a whole lot of things from there's a whole lot of things to make life easy so deployment succeeded go to resource and now cross your fingers guys when we go to this IP address here we see URL HTTP John's flask a flask app as your website's net we should get a hello world but the container has to warm up the first time that we actually visit this address so if we go here and we see container settings what we should see is container warm up a little issue when we look at these logs here we should see some container warm up blogs when this loads so yeah so I'd be curious to hear your guys opinions on unlike all this as well because there's a lot of ways we loaded and there it is hello world so it worked but back to back to what I was saying so yeh first it worked that's great but um that's what I was saying so I would love to hear what you guys opinions on these sort of things so do you want to learn more about docker this was like a fast and furious like boom boom boom we're using docker we're using flask we're using Azure we're using Azure container registry using Azure app service there's a lot at once so and we talked a little bit about infrastructures the templates that are sent to the as a resource manager those are arm scripts which is as your infrastructure as code there's a lot of directions go a lot of things a lot of moving parts here so what I want you guys what I want to know from you guys is what you might want to learn more about do you want to learn more about docker do you want to learn more about infrastructure as code do you want to learn more about Azure services do you want to learn more about building web apps with flask there's a whole lot of different things here I would love to get you guys input so anyways thanks for watching I really hope you did find this valuable a thumbs up would be great if you enjoyed this video comments would be great I again I just love hearing what you guys have to say and comments are great for great for the visibility of the channel as well so all that would be great and be sure to smash that thumbs up on smash that subscribe button as well and as always guys thanks for watching and I'll see you next one
Info
Channel: John G. Fisher
Views: 16,295
Rating: undefined out of 5
Keywords: python, azure, docker, python flask, flask, deploy python app, deploy python app fast, how to deploy a python app, how to deploy on azure, deploy docker on azure, azure container registry, azure app service with a container, azure python app, python azure, dockerize flask app, docker python app, how to dockerize python app, containers, containerize python, containerize python app
Id: H5hs4LreRS0
Channel Id: undefined
Length: 37min 8sec (2228 seconds)
Published: Tue Nov 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.