How to build docker image for python flask app

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone thanks here back again with another video and in this video we're going to see that how we can convert a flask app which is made in Python in a Docker image now this is a really basic flask app that we have created which obviously runs on a web and just give us a hey message hey python now flask is one of the most easiest way of how you can build a simple python application on the web or maybe just apis and yes there are other ways like Fast API and Django and a lot of database could be involved but slowly baby steps we're moving through the Journey of going through with the docker so this is the most easiest one the whole idea behind creating these kinds of videos is so that you understand a little bit Journey about the devops and sometimes you even want to know that I don't really understand what is the business logic in these applications that developers has coded I just need the bare minimum information and wire that information I can make a container or I can make an image out of any application whether that's coded in JavaScript or in Python let's just assume I don't know python let's just assume I don't know even a tiny bit of java and still I'll be able to host these application the today's turn in this specific video is for python flask application let's go ahead and see how we can do that this is the one of the most easiest one that you can do so I don't even need to check what's there inside the index.pi the first thing that is important for me is the requirement what are the requirement for these application now this is where you actually go ahead and interact with your developer he says hey this is a Basics last gap which needs to run on the port 3000 that's all I care and that's all I really want to know I don't know what it produces as an output what success and all of that that's the developer role we just want to put this application as an image and obviously we want to push it onto the docker Hub just for fun okay all right so first thing we're going to do is simply go ahead and say uh Docker file so that's what we want now in this case this is a python image so we want to take a base image which is going to be a python image now there are a lot of python images available both for version 2 and 3 but obviously this is a Python 3 version that we want to call up so really simple we start with the base image always and always with the base image so the base image in this case we are going to go ahead with python I'm going to hit control space and it can give me a lot of suggestions so I'm going to go ahead put Colin then again hit Ctrl space and gives me a lot of versions of these images so in a minute there we go we have a lot of version of python 2. I'm not interested in that I'm version 3 that is where I'm interested and you can see from 3.16 to 3 or 15 a lot of version 3.16 seems a little bit too much updated for me so I'm gonna go with alpine 3.15 Alpine is nothing just a strip down version a little bit lower in the size but again depends on what kind of python is being used in your application you really want to consult with your developer a little bit on that all right so this is the basic then what we do is really the basic uh this remains common in most of the applications so we find the working directory you can put it just in the slash but it is not a good idea so you always want to have a directory because this is like an operating system almost like and in that the slash is very sensitive directory so you want to create a new directory just like you have a desktop something like that is there so we do just the app and it doesn't need to be always like app it could be just hitesh but hitesh doesn't seem great the app is actually much better then we go ahead and simply say that hey we want to copy and the copy some information uh there are ways how you can copy then run the command then can copy rest over the files these are caching and all these optimization we don't want to go into that as of now we simply want to say hey copy everything that you have and just go ahead and put it into slash app so that's it we defined a working directory we move all of our files into that working directory that's what the basic of this image does then obviously we need to run some command because when you take an image which is based on python it is based on python the python is installed in that but all the requirements like flask maybe Django or something else is not present in that so we need to install that so we go ahead and run the command in case your company or you deal with a lot of python command uh you should run these few of these commands the first one is actually pip install and then we pass on a flag of Dash R which gives all the requirement in a file so which is going to be requirements.txt and make sure you don't do typo because I do a lot requirements uh should be the exact same file requirements.txt so make sure you take care of that once this is done that means in a new image which is based out of python all the files have been copied all the requirements are also done the only thing which for this basic flask app remains is exposing the port and then finally running the app so let's go ahead and see that we have seen that in the last video as well we can go ahead and say hey expose 3000 it always doesn't need to be 3000 it could be five thousand eight thousand whatever the port makes sense you want to expose out of that machine machine or an image you can go ahead and do that and finally you have to fire up a command we'll talk more about these in the series itself or the docker by the way I do already have an existing series but I want to freshen it up so we're going to go ahead and have some command and the command is really simple it's just a python file which we want to run in the same directory remember we have already mentioned our working directory that's where all the things happen we'll say dot slash index dot py so we want to run this base file and based on that there could be 100 other files and dependencies or all that this is the entry point we want to mention once you do this that's it that's your basic Docker file not too much of the command obviously as the complexity begins to increase uh maybe that's a Django file just basic Escalade or maybe postgres is involved maybe mongodb is involved there's a lot of things that can come up again and based on that we have other files even spoiler alert Docker compose is there but we don't want to track them right now at least in this video but this is all what you have to do this is a great exercise to build up a lot of images and all of this by the way in case you want to go for that uh here's a basic python file or you can just go for here's the basic python file you can pause and write that but I don't recommend this I recommend you to go on to the flask documentation let me just show you that actually it's super easy let me just go ahead and open this up so if you go ahead on to Google and just say flask uh hello world you can just go up into the a lot of files are there even the flask quick start documentation a lot of great websites are there which actually helps you out there uh we'll go on to the plus a flask uh getting started and there it will give you the documentation of how you can get started with the basic application so notice here uh this is all you need to copy uh this is the route slash on which hello world is being defined and you are just running it and that's basically it it can do all the job for you uh there is additionally a couple of more lines that you can write but this is all this is all you need in case you don't are not able to find or don't want to write it just go get it up here that's all that's all you need for the practice purpose in case you are seasoned in Python you want to write a couple of more lines of code like if and all of that that's fine if you don't write it that is also fine flask actually works without this as well all right so we got this one now let's go ahead and try to build this as an image in case you have followed the last video where I showed you how you can build images using the node.js this is almost going to be similar that with that but I'll show you that again the fact the requirement is you should have Docker installed in that on your machine at least so the way how we do it is simply say Docker we pass on a command to build and then we pass on a dash T the dash T is to give you a tag and I always always prefer that my tag should be something which comes up from hub.docker.com and your username in my case I've already logged in into this so I can check my profile and this gives me my username so this is hey node.js we pushed it in the last video and this is my username so I go up here and I say that hey I want to tag it first with my username then whatever the name you want to probably Define here so in this case I would like to say hey Dash python uh python flask would be better because I do have a plan to push up more python projects and show you more example with the Django and couple of others as well but no promises on that and this is the basics hey python flask so this is the app now again we'll talk about these tags you can go ahead and say latest this is the default tag that when you pull them image it pulls up but if you don't want to pass it on because we'll be incrementally updating it you can go ahead and say classic 0.0.1 dot release again if you don't pass on anything pass on latest that's a default tag but you can go ahead and pass on this as well once you have done this the most important part is put up a DOT this dot defines that the docker file based on which this image should be created is available in the current directory which is Docker file now if I go ahead and show you my dashboard on the docker desktop in the image we have just the hey node.js with the tag itself we'll be creating another one I can go ahead and show you by refreshing it nothing is there and we can go ahead hit run this and this is going to take just a few seconds because it's not a big image on that depends on your internet speed as well but I think it should be fairly easy and now we have got uh this Basics up here copied and all of that so I should have another image uh in theory at least so there we go python hey python flask and we have got the same tags and release and all of that so now what I want to do is I want to run this image first because before pushing it to HUB I obviously want to check out whether this actually works or not so to run that all I can say is Docker container and then I can run this app with a Dash D which stands for detach mode hey don't keep my terminal busy give me my terminal back and you keep it running in the background and then we obviously need to expose the port because it's a web application runs on a certain port and I would say hey in the docker the 3000 ports were running and on my main machine I can be saying 4000 or I can say 3000 wherever you want to map those Port together and then obviously I have to mention these name which says uh hey python hey python flask and I have to give it the version because otherwise it pulls up as this default I don't I haven't given that so I'll say 0 dot oops my bad accidentally hit enter okay we're gonna say uh 0 dot Z oops 0.1 Dot release that's a long one and then all I have to do is hit enter after that so I'm going to hit enter and this is supposedly in theory if it is all correct it's going to run a container uh behind the scene for me and we'll be exposing a port on my main machine it's taking a little bit while probably did I hit enter something wrong let's try to find out where it is going wrong right now nothing and the image is there all right let's wait for a couple of seconds did I accidentally run some command wrong all right uh let's let's actually close this and let me open it up as a full screen so that we can actually borrow this name uh maybe I did accidentally type something wrong okay all right one more time this happens to every one of us Docker container we want to run the container we are saying hey run it in detach mode with a port exposed of 3000 and we'll map 3000 on our real machine as well and then I'll paste this one as it is and after a space I'll hit enter and there we go this time it works probably I mistyped something I'm sure here or probably here I don't know where okay so this is now running and I can verify that by saying Docker uh container and I can say LS small for listing down so there we go container is running now let's quickly check on Port 3000 whether we are getting uh Hey python this time ah great but what if I want to stop this so I'm going to go ahead and say docker container uh stop and I want to stop zero seven six F so I want to stop that I want to run this command again but this time I don't want it to run on my main machine for 3000 all right a small glitch and now it's recording back so okay so what we're trying to do is we were trying to uh just change the port here so let's go ahead and say uh we're gonna go ahead and run this Docker container and we want to just click LS nothing so far and we want to run this so we're going to say docker container run and what we want to do is run in the detach mode with a dash P option and we initially uh went through with the 3000 but on my main machine now I want to expose 4000 Port but on the container it's always 3000 that's what we mapped while building this image so that should be fine and now we simply go ahead and mention that hey this is the image that we want to run and that's it so it gives us a 97c so let's go back up here so if I go ahead and check it out this is where Star recording stopped 3000 nothing is there and if I go ahead and say 4 000 there we go so you got the idea this first one is my machine Port this is the image Port I cannot change it once the image is being built but yeah I got the idea that how this is being done so let's go ahead and say Docker container stop and I want to stop 9d nine and I can just run uh no such container 90 or 97 my bad 97. okay all right so now all the containers should be freed up uh this means I can push this uh particular image onto the docker Hub so that anybody can use it I'm gonna go ahead and say docker push and I'll name this one same I'll say that hitesh my username and then all I have to do is actually I can copy and paste this one I've already got this as my clipboard so there we go so this is my username and this is what I want to call my image and of course you have to pull in the tag with this exact release otherwise if I just say it colon latest then it will pull the latest but this incremental update will help us later on as we'll be studying at the docker so this is the basics let's try hit that and obviously it will take a little bit time not much but little bit can we do a fast forwarding in the editing I think yes all right so finally it's being pushed and let's go back and see on to the docker Hub whether we got uh python as flask and there we go all right we can see that hey python flask is now available and we can see that there we go anybody can use this one of course they also have to use the tag uh this tag exactly and this will help to get them this file all right so again uh you noticed here that as a Docker or as a devops engineer you don't have to Too Much worry about what's happening what is the business logic and all that you were just responsible for creating the image and as you will move forward you will realize that this is the image that somebody needs to give you and you can run as many containers as you like of this exact image without worrying about what's the business logic what's this but again yeah this much of the piece you need to write and this is mostly available on the internet on the documentation or through such YouTube videos which I'm pretty sure you're gonna subscribe as well that's it for this one let's go ahead and catch up in next video
Info
Channel: Hitesh Choudhary
Views: 86,931
Rating: undefined out of 5
Keywords: Programming, python, devops, cloud, docker, kubernetes
Id: 0eMU23VyzR8
Channel Id: undefined
Length: 16min 33sec (993 seconds)
Published: Fri Oct 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.