Docker Build: Dockerizing a Python App with FastAPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] well it's not working in prod indeed it's not working in prod well it's not my problem it works just fine on my machine and on the dev server well well i told you all systems are isos and if it's working in-depth it's working in practice it's iso oh yeah essentially i mean pretty much almost almost what do you mean almost you knew there's maybe a very small gap in minor versions and for some plugins nothing particularly important hello hello everybody welcome to today's show hey michael how are you hey peter it's super to be here hello hey everyone out there awesome awesome yeah hello everybody out there watching live uh you know i'd like to start the show off if you want please you know put in where you're at in in the world it's nice to see where everybody's tuning in from uh but thanks for joining us man i'm excited michael we've we uh i was on your podcast and we've been talking for a while uh excited to have you on welcome um i'm super excited to be here it's it's really neat thanks for the opportunity you were a great guest on the talk python podcast and people really liked that episode on docker and python so maybe some now it's python and docker i don't know how we switch the orders but let's switch it up and i'll be on your show yeah exactly and don't say too many nice things i'll blush don't make me blush on youtube no well anyways before we get too far uh why don't you introduce yourself and tell the folks what you do i'm michael kennedy i do python things i've been doing software development forever not quite forever 25 years which is pretty close to forever in software terms but i run the talk python podcast the python bytes podcast and i also run talk python training founded that where we have a ton of python courses to teach people how to do python things somewhat including docker as well and i guess i'm also a python software foundation fellow uh recently so yeah that's me oh yeah when did that happen congratulations thank you that was 2020. yeah i didn't even know that i was in the running for it and i just got this notification and i'm like wow this is crazy oh they're very humble yeah that's that's awesome oh cool i see we got some folks from boston poland bangkok nice thanks for joining appreciate it yeah yeah so so um yeah so python we talked a little bit on your show but yeah python's super interesting to me right because it's such a you know it feels like it's a new language now but yes but it's not right how long is it python is an enigma in some really interesting ways you know it's it's a language that has exploded on the scene is since about 2012 it's really if you look at like stack overflow trends and stuff it's it's it was going kind of bam it just did this take-off where it surpassed java and javascript and all the other languages and yet it was created in 1991 basically when java itself was created right so the the enigma part to me is how does this language sort of slow boil in a pretty popular but not crazy kind of way for 25 years and then take off and uh i think the reason that it's taken off is so many people are coming into computing not thinking i want to be a software developer but they're like i need to get just a little bit more than what i can do now with excel or with just built in apps i just need to do a little bit more what's the easiest simplest way to do that they often land in python to really quickly create a web app or really quickly do some data science thing they're like hey i could just keep doing this and they often don't even think of themselves as programmers but they get stuck in this world and that's really creating this explosion and people are really taken to it i think it's great yeah yeah yeah exactly and it's such a um it's a powerful language right that that you can do all those things with yeah i i do i am not a python uh developer by any stretch of the imagination i can read it and then there's funky stuff that i i go wait anyway what's that um i thought this was simple what's this thing going on here yeah oh so should we get into a very heated argument over curly braces or not that might be fun i thought we would do that that's all during the show yeah absolutely we gotta have them we can't have them tabs versus spaces while you're at it come on yeah but honestly i've had i've seen that argument before right of uh yeah you need curly braces i i can't you know it's super interesting you know what like if if i had to write python in notepad would be hate in python so much because if you actually had to write the structure that would be really hard but i don't even think about it because in my my code editor i just type you know create a loop enter create a new statement enter and it just it manages the structure kind of almost invisibly by just putting this up and zoom i think it works out really well in practice even if it sounds like a crazy idea yeah yeah i'm with it i'm on board hey whatever you know whatever works right um yeah it's funny i so i do some mentoring and and um help folks coming from different careers into into software engineering and a lot of them go to boot camps um and you know with the tools we have nowadays vs code and pycharm i think it is you know you have all these plugins and editors and highlight syntax and yada yada i make them all take that off right go back to a standard text editor no more crutches right because i've had a problem i would do interviews with very junior folks and i would say okay write a function in javascript and they would literally struggle over how do you how do you just write function brackets co right because they would do fnc tab right boom right yeah and um yeah so you've earned the right you've been programming 75 years i think you said something like that so i think we've earned the right but i'm just i'm if anybody's watching and you're new seriously pull away all the crutches feel the pain and then you understand why those tools are there and what they're doing under the covers i think for sure yeah that's important yeah awesome well so today we're going to build some some uh some code we're going to write some code write some python code some api stuff put it all into a docker container and run it and do some fun stuff are you ready you ready to rock and roll i think i'm ready to rock and roll yeah awesome okay let's jump right in awesome first half will be python second half will be more docker right well yeah do the python thing then we'll put it into docker awesome okay we're getting a question on the quality stream all right let me uh yeah you know if we go higher than 720 it gets um you get very low uh text and it's not the greatest uh maybe it's just an internet connections hello from brazil we got brazil ukraine austria nice hopefully we'll get better my apologies for for low quality it it works on my machine though it's working just like just like the video says yeah um all right awesome whenever you're ready let's pull it all right so i fire up my screen and we'll roll here oh fantastic prepare for inception for a moment okay there we go there we go what are we going to do we're going to work with this framework called fast api and fast api is one of the python web frameworks that has really taken off lately if you look at historically there's been flask and django as the popular two frameworks this came out a year and a half ago and is now contending to be as popular as those already and the reason is is it embraces so many of the cool new developer enabling features of modern python whereas those older ones the the flask and django been around for a long time so they're kind of not stuck but more in the way of that you know 2010 than 2020 right because it's hard to change the apis once they exist yeah we're going to use this to build an api that allows you to ask questions about movies and you say given a movie who directed it so our goal will be to build an app ideally if we have time a server side and a client-side app that work together that will let you say type in a movie title and it'll and you can guess you directed it hit the enter and uh hit enter and it'll tell you well it was released on this date and it was directed by so and so we'll see see who's right and in order to make that happen we're going to do for this i'll give you like a web 2o sort of thing we're going to do a mashup okay we're going to go back we're going to use a service that has some already and we're going to wrap that up into our like repurpose this other service so over here we have this movie service uh talk python and it lets you search for things like show me all the the popular movies that have run in the the thing like the rundown and runaway bride and blade runner and so on and so we're gonna get this kind of data live in our api and then turn it into um more simpler information that kind of answers that question that i talked about yeah nice nice and runner runaway bride is a fantastic movie by the way absolutely just saying um absolutely all right so in order to do that i'm gonna go over here and i put just enough structure into this thing so we don't have to worry about you know getting everything set up so here we're over in pycharm we have a virtual environment that i set up python 39 which is the latest and we're going to first create the web app the web api and then we're going to put it in docker and like i said if we have time we could spend the extra five minutes and write a little app that wraps that up awesome are you ready ready well you're getting ready to type i'm gonna i'm gonna because you mentioned web 2.0 and um a lot of folks might not know what that is right i think that's that was a while ago but that was like kind of the precursor to this cloud native microservices right it was like the idea of services on the internets and connecting them all right yeah yeah yeah i mean it was sort of the first beginnings of like the cloud like let's put things together and make make apis and things can work together and it just brings me i just think of yahoo when i think of it yeah kind of makes me sad honestly yeah yeah yeah awesome so all right you ready i'm ready i'll show you why this fast api framework also is uh popular so we're gonna have to specify our if i can spell api as a hard word because we literally say the letters so to do this we're going to need two libraries we're going to need fast api which is what we need to actually be at but we can't run it directly so we'll need a web server we need something called uv acorn which is a production ready async multi-threaded type of server nice and i'll click that to install those and then i'll wait and apparently they're installed one thing that's a little wonky about pycharm is it sometimes things are misspelled when they're just package names so yeah tell it it's not misspelled all right so we've got this uh set up to use the libraries right and it's also people might notice there's a little p here and there's a like extra cursor there's peter so we're using the code with me new thing that jetbrains has so we're trying to have uh both sort of follow along here all right so here's the the zen of it uh we're going to go over here and import fast api and we'll import uviacorn and then we want to have some kind of api endpoint and for now i'll just call it index we'll we'll adjust it in a minute and it's going to return something like message is hello world obviously we'll make this html in a second now this is just a function not an api endpoint so to make it an api endpoint we've got to say go over here and create one of these fastapi.fast api like so and we say app.get you have all the verbs get put post head delete patch let's just go with get and we'll say that before slash and then the last thing here we're going to do is we have to run it so we want to be able to import this in like a production server say hint in docker and and run it as a background daemon type thing or in linux but we also want to just run it here so the easiest way to do that is follow this if under name equal some remain convention this is like public static void main type of convention it's a little wonky but it's how python works so we'd say uv corn dot run microphone is in the way and you say run this app and that's it we've now created a web app peter what do you think done ship it yeah well let's try to run it for that maybe we should check that that's right it's a good time to talk about not testing exactly let's see hello world perfect nice perfect so so that's it so we got our little hello world working let me move us over here and right now it's returning this ht um not html it's returning json all right if we go look it'll say applic the content type is application json and in a minute i'll go and work on switching this over to something slightly nicer but for now let's just just leave it there yeah sounds good okay so let's go and actually build a proper api here and let's do it like this let's go write another little library this would be our little data access library that's going to go get the data from wherever it needs to database api whatever i'll call it movie data and in order to use this thing we need to go and talk to our movie service right so i'm going to grab we want to go and run a search for a movie so i'm gonna grab that url and we're gonna go down here and write a function called movie title subtext and it's going to return we'll figure that out in a second okay just get it to download something i'll go over there so the url is going to be something super simple and what we want to do though is we want to put that search string like this is searching for run like blade runner and whatnot right right so here i'll show you a cool python trick a charm trick so normally if you want to create a string you can you could do this equals http plus title that might work you could do this title. and put a little curly brace and put out a format but the new the new hotness is to put it like this with one of these string interpolations and you put this in here right nice yeah that's cool the problem is i always write right right right right right i get to here i'm like oh i need one of those back back back back back back back back yeah you got to put in f and then forward forward but uh they added something here where you can go if you just go and type this and you select something out of the variable space it'll rewrite the string which is kind of hot nice okay so we got our url and now we need to go get it um i'm going to use a library called httpx and check this out pycharm's like oh you probably need this library from the internet so i do that's fun nice let it download it and if it's really good i'll say oh you should also put that in your requirements file so the next person that installs it now knows they need it as well so we did that perfect and then what we need to do is we need to come down here and we need to write some code to download it but this library is one of these async libraries like think node.js style like everything has to be a callback but it uses async and await right so normally what we would write we write something like with http x dot client amazing client client and then we would go response equals client.get and give it the url from there but this all these operations have to happen asynchronously so they need to be turned into basically little continuations so we created async with block you think with block and then down here we have to you can see there's should now be autocomplete maybe i'll take it back uh i also got to async this that's probably why it's not loving it well we gotta await i'm getting this not there and what this should be is we can give this a type so we can work with it better there should be a async um no a response oh cool so very very like you said very similar to node yeah you think it went pattern yeah i think it's pretty similar to modern javascript it looks just like the c sharp versions although this little content is like an asynchronous block of code it's a little bit unusual that's neat but it's it's unusual and let's just do just print out the response and the response text just to see that things are kind of hanging together you know what i mean yeah yeah so let's let's go over here and we'll import uh movie data like that and let's have another api this will be our real api endpoint that does actual interesting stuff cool yeah so while you're typing so uh for for you computer science nerds out there um so what michael did was created a facade right separation concerns right here is where we have um handling our api requests right and it does that and then it uses uh the movie underscore data i believe yeah uses that to go and get data so now we can say uh it was a dot get i think i forget yeah research yeah right no i'm looking at the wrong file let me look at the right file yeah so get movie right so behind that facade allows you to call hey well i know just called um get movie right yeah one of the benefits is like if for example right now we want to go get it just straight from this api but maybe we want to cache it maybe that api starts charging us money and we'll say look we're just going to store the answer for a day if you ask for it again we're just going to get it from redis or something and you could easily change that without rewriting this part of the code right exactly yeah yeah perfect yeah that's the idea okay so there's a couple things we have to do here i have to specify somehow some kind of title being passed in we do it like that and we come here and say this is a title which is a string that's the title over and that's close but remember this is an async movie so when it gets back you can see that little arrow it gives us a co-routine not the result you don't get the result unless you await the codes and it actually doesn't even run so we gotta asyncify this and await this and fast one of the cool things about fast api is all this stuff is just built in like we don't change anything or do anything it just it likes the async stuff nice all right moment of truth we need to go to api slash movie slash something and see what happens so let's go here uh let's try running we knew that was going to work yep oh we didn't return anything but that's okay what happened it didn't crash that's already super exciting you have no error look at all this json we got so we get this yeah and it was a 200 okay so this is cool we got it tells you a little information says here's the keyword you search for here's the hit so what we really want to do is just go to the hits and take the top one and return it okay so i'm going to grab this text real quick here because that's what we're going to have to model the real quick down here so instead of printing out while that was fun that was fun what i'm going to do is say uh data equals response.json and i'll convert it from text to a dictionary parse the json we want to make sure it worked what if the network's down what if the site hates us and tells us no more no more requests or whatever we want to just do a little check here so we can say raise for status which means if it's not a successful type status code basically 200 it's gonna hey it's gonna crash right do i need to await json no okay good so then we'll come down here and we'll say it's going to be data of hits if not results there might be no results you might ask for something that we have no data on and we'll return nothing otherwise we're going to turn first we'll return results of zero right this will be let's make this a variable at a movie like that but we're going to do something a little bit better in just a sec but let's just make sure that we can get this um get this working so if not movie i don't know what the right response here is probably you say it's a 404 right if you ask for a movie that doesn't exist for forwarding things that sound okay that works for me and i think that's um that is uh uh spec compliant yeah compliant am i i'm gonna get beat up here on the internet for saying that but i'm pretty sure right the idea is hey i asked for this resource it's not there give me a four or four not an empty resume or ordinary or you can send an empty result oh i forgot you know what yeah um uh in flask i believe you raised not found one and this one you just said the status code is 404. all right so if it's not there that's that otherwise or let's just return the movie for a moment that seems like a decent response so it again check it out nice we got this this data which is just grab the top one which apparently is the rundown directed by peter berg it involves bounty hunters and the amazon i'm guessing i've never seen it but here it is right but there it is and so what we want to do is we'll just grab things like this this code maybe the director the keyword and the year just something real simple so what we would like to do is we would like to give a little bit more information back right when you're building an api it turns out not that one it turns out that it's really good to document your api and fast api comes with built-in open api documentation so for example it knows that this one takes a title and says cool there's a response uh you get some kind of string maybe that's not very helpful so let's do a really quick thing here go um create a models directory this is common in the fast api world i want to create this thing called a movie uh file it's gonna be a movie model class movie model and it's gonna use this library called pedantic so we'll say this is a base model which we import from pythagoras comes with fast api and let me just pass it let me just paste this thing that i copied down here so we can grab the bits and just get it to line up but what's going to happen it's going to have a title which is a string and you just specify the field to this and it will automatically parse the data convert it make sure that the things are there so we got like keywords this is going to be a list of string i have a value of an empty list the director good and what else we wanted the year year which is an end okay so that should be enough we don't need the other data hopefully everything's happy with this this quickly is pycharm saying you should add this as a dependency to the requirements file but it's a strong dependency of pycharm so it's not python i have a fast api so it's not really needed but just so it doesn't look broken i'll put it good yeah so what we can do with this this is an incredible library that people should definitely use if they're they're not already so what we can do is over here we can go and say you know what this returns an optional these movie model things which we gotta import at the top and instead of doing this we just say we're going to create a movie model and pass over the dictionary and the star star turns dictionary key values into keyword arguments instead of a dictionary with a name equals michael it'd be name equals michael in the the function uh nice okay yeah those are those are the things that i'm not a i'm not aware i see that yeah these are the weird little tricks why is there so many stars this is not a pointer to a pointer it was yeah my first days kick in i was like yeah that's a pointer to a pointer exactly and then we can go over here and we can say the response model is one of these as well and because this actually comes back it's one of these things i think it knows i can say you have to say this is the dictionary of it it won't actually serialize it by itself all right with that super minor change we've now got data validation but also we have a rich open api schema that we can use so people can just point like there's libraries you can point at open api schemas and actually generate the resulting models that you need to consume them so we're being a good citizen by by doing this here yeah it also lets us write our client much easier yeah yeah so let's just make sure this thing still runs it does it didn't crash i mean it should look the same except for notice that we have a lot simpler data like we just said all we care about is the title the keywords the director and the year nice now i did i did find this movie i did find the the uh movie what do you call it poster we can no i'm just kidding you know i think some of those may have posters i think it had i think there might have been some poster integration over here at some point but i think the way it worked is you would take the imdb code and then you could send it off to another api that'll give you the poster but yeah let's let's not go too far right right okay okay yeah we got a working api we're done i mean we could basically be done at this point we could say this is it we're done right um in fact let's go ahead and leave it uh like yeah um i'll just say no what to do somebody pulls it up in six months from now like how did that work again oh wait if somebody just clicks here see hello call this right normally if i weren't just short on time i would just go and add like a proper html landing page but whatever right right it works right okay so are you good with this you liking it yeah i'm liking it all right now i need to build a server out of this right uh i have i have built a server out of this before quote theoretically we'll see how close to what i've written lines up with what i envisioned my server to be so what i did is i envisioned that i had already set this up on like locally on a linux machine or maybe i've created a vm and i've typed in some commands to create it or possibly i've even fired up docker with the dash i t bash gone in there and typed in there till i got it working right right and that looked um not sure where did that go not this one somewhere i think i put it i think i moved it here no let me go and grab a copy real quick cause i did check it out somewhere hold on i need my where's my and maybe i don't have it that's all right we can we can type it in it's no big deal it's pretty sure but the what i wanted to envision was that i have i've got this file with a set of commands here right and it would look like apt and app update and apt upgrade fy right so this in linux i mean a lot of people know but some people maybe don't to do docker you're basically doing a small subset of linux admin right you're like okay i need to run a series of linux command line operations that will then take a base server and turn it into the server i like and then put it into a container and make it something that docker can do magic on right right yeah so let me just type out so i'll talk through these and then we just run them that way i have to talk to him again so we want to make sure that we have the latest version right so maybe i've got a image i've checked it out it's a slightly older version of whatever i'm working with and since then like a new version of python's been released or a new version of nginx or whatever it is you want to use so this will make sure that goes and detects all the latest dependencies and also security security people care about that sometimes yeah so we're going to need a couple of things build central uh python 3 pip python we don't need virtual environment but we need python 3-dev i believe there we go so this will be what we need to run base python then we're going to run some commands to install the stuff that we need so first we're going to upgrade our dependencies things so this is the library that manages installing stuff like npm for node or newgedford.net or something like that and we want it it almost always is out of date so the first thing you want to do is get a current version of the tools that are going to install everything else we're going to need g unicorn this is the production server that's going to run basically going to wrap up that uv acorn thing and then it also needs a couple of their libraries a uv loop which is a high performance async event loop and then http tools i have no idea what that does but it crashes if you don't have it right and then what i need to do i got to make sure that we copy this dot service file requirements actually it's just requirements permits.txt over to some location let's say we're going to put it into slash app right something like that yes i have a little subdirectory on our linux machine slash app i'm gonna put that there and then we want to install those requirements [Music] yeah yeah go for it keep going uh jumping there go so uh i just pulled up the link to join the the slack channel that's the main community page uh if you go there you'll see a link to the slack and you can join into our slack huge community in there yeah that's probably the best place you'll find others with similar hardware and get their experience of running desktop on it um yep okay awesome all right yeah also let me just point out uh one thing by the way that this is the um this is the apple m1 chip so it's a little bit or a little bit out on the edge we're running some some cutting edge docker stuff here right awesome yeah and actually it works just fine which is pretty good so we also need to do let's just i'll put like a copy like dot we want to copy um this over to this app as well that's something we might want to do and then we're going to need to run a command uh which is going to be a user local bin unicorn i'm just copying this out because i know where it comes from and then we're going to have to have a couple commands we have to say bind to zero i'll say 80. then we you can specify how many workers we'll create like a web farm so this is really cool on one server i could just have one server process but if that thing gets hung up if it runs out of memory if it crashes it's all sorts of reasons that you might want to have more so we can compare and say actually create four sub processes that all are going to be sort of load balanced but only within the machines we'll do that all right and then we're going to say k-k-u-v acorn.workers.uvacorn worker so this is like how do you want to run things and we're going to run that on main colon app so over in main find this thing and run it nice and that'll work for the moment we'll probably want some more but that's it this is the basic version i want to make some changes but this is the basic version of my setup script that somehow i've i copied but didn't copy so there it is anyway so this this is the idea right this is what i need to do to my server to set it up yeah so uh yeah that's like you fire up an ec2 instance you you uh ssh into it and then you would be running these right michael exactly that's what you do there would probably be like this copy would probably there would probably be like a get clone or something like that to get this materials there right right and then you would you would like you know move the stuff around but yeah if copy could like magically copy to the server yeah yeah that would be cool so what do you think should we dockerize it yeah i was gonna say this is very you're setting me up very nicely here michael i appreciate it give me a nice softball uh but yeah hey this is painful we should doctorize it right absolutely i think that's and and um what's funny is i would i just watched solomon's uh original talk at pie conf mm-hmm richard's pie comes in europe yeah right he first announced docker right and he talks about like a five-minute lightning talker something incredibly short right like just hey everyone give me just a moment i'm gonna show you something that's going to change the world yeah and he just went blah blah blah blah and ran through it and to be honest when i first watched it i was like because i wasn't a i'm not a linux uh you know systems developer and he got to the point where he said oh yeah docker run and we're gonna do this and everybody's like whoa and he's like yeah so what that did was isolated or processed set up networking for you binded this did a read write file system blah blah blah blah blah blah and like later i go oh okay but what he did talk about is in there is exactly what you're doing here right and he laid out that story of like oh you go to a server you run your commands to install everything then you install it oh you need to put it on this server let me go remove everything i manually did and redo it and so yeah so these steps using a docker file is the steps you would you know pretty much do to install and run your software on it on a on a server right yeah this is like the admin kind of stuff maybe you'd do it with ansible or chef or puppet or something like that but yeah these are the way i would usually do i'd probably create a vm and i'd log in and i'd i'd have this script and i'd be like all right let's just do some of this and see how that paste and see how that works that seemed to work let's try this see how that worked right yep and and that's the way i create uh darker uh images from scratch right so i'll do create it with just the base scratch and then right and then attach to it run my commands and then you can save checkpoints right um get it all working okay cool and then now you have your dockerfile right yeah exactly yeah so a lot of this stuff is kind of discovered by by exploration right like oh i tried to just install python3 but when i pip and saw stuff it wouldn't compile some of the dependencies so i need build dependencies in this and you just you don't necessarily know that up front but you know you try it right it doesn't work and you're like why doesn't this work yeah the thing that's nice is you get into these files and then you almost don't have to remember it right you can just okay well just this is how we do it i remember why this is here but i have to remember that it's there yeah yeah and we'll get we'll we'll get to it when we talk but you know it's also uh you can layer these images on top of each other right so a lot of the the build essentials and some of these tools that all your dev teams are using you can create a base image that has everything in there yeah for the specific version of uh python you need with pip um with requirement right and not maybe not the program they'll be specific for the app but and then share that with your dev teams yeah exactly like that stuff right there could be the starting point right right for your project and they're like okay copy my requirements and install them copy my source code and then run it done yep right that's how i would do it in practice i probably won't do it here just first cycle time right just yeah put one dockerfile but uh also it's really important the order of these things when we get to docker so let's dockerize it huh let's let's sounds good so notice pycharm gave it an icon that's kind of cool that probably means something so and also it's autocomplete how about that gotta love that yes so i'm going to use ubuntu okay i'm starting to like this autocomplete there that's pretty sweet which is the latest could be other servers it doesn't really matter but like the script with the app and stuff like it's one of those styles so let me copy this over notice i added a change directory i realized like we were running the wrong working directory it needs to be where the main is yeah so put this over here and it's pretty straightforward right to convert from the linux commands that i want to run to the docker command so like run right right and a lot of these are just run i'll hit all the run ones real quick this one however is is the super run that's back on sec and then copy to yes and you might michael correctly i'm wrong i'm back i'm back to you driving with you but you might have to put services in front of requirements or no you have oh so yeah let me i so what i could do is i could do let's put a copy here yeah i could do this we don't need these dots i just put the dot there for i don't know i could just do this right okay and then install it but each one of these lines can uh you know docker can like create a slice a save point basically right so if something like if if say this line changed this wouldn't have to be redone but everything below would have to be redone yeah yes yeah yeah yeah so it'll cache your layers right so when it builds yep exactly so i kind of have gotten ahead of us a little bit but i put it in this order so that because normally this is kind of a slow and painful process these are these two steps but we'll cache this into a layer and only if the requirements file changes do we have to rebuild that layer whereas if we did it with just this and then the install any code file change at all like a css file would require or reinstall the requirements yep yeah no uh a little bit there yeah perfect i think what i missed um i see what you're doing now you're putting uh requirements into app requirements then installing then we're moving the services all everything right services and all of that code into the app as well yeah yeah and now now you're you're requirements you're happy everything is in one place okay gotcha yep i'm i'm picking up what you're putting down michael awesome man so we're gonna put this to the side for a minute let's just let's see what we can do with this so this being pie charm it tries to make stuff easy for us i can right click on this and say run and it will just download it'll do a docker build basically which is what you're seeing here so all those steps that we had above are now happening right now we're in the app update step now we're in the this build step i'm only just trying to take a quick guess at seeing what i'm going by yeah because that's an apt install operation happening right there you see the downloads yeah and then in a moment it'll cache that layer and we'll see those python steps but you can see why you want this cached right like this is you don't want this on every ci builder every deploy all right uh yeah i know that's still still on that step though of course you very much want cash yeah this looks very familiar what node uh no developers are familiar with yeah yeah the package although we're not you know not compiling or anything or whatever it's just pulling everything down but once you get that cash flow you said it speeds it way up yeah exactly so yeah here you can see it's doing the pip install stuff uh now it's actually on that one uh where are we oh we did our requirements and it ran and what did it do when it ran it exited i think i'm pretty sure it's gone oh no it might just be doing it hanging out in bash or whatever so that's not super super helpful uh can we get to it i don't think and pycharm i don't think there's anything else we can do to it but if we go over here we can say docker images let me make that a little bit bigger for people and which one would it build 98 ca there we go so we could say things like stop this thing we say docker run that's it i t or dash i t dash i t uh bash and i put it in the wrong knife no you're all right i don't think it got i it looks like it might not have built and it said it started let's deploy which one is it the one that ends here unfortunately oh do i need to go i'm sorry go go back uh do it do a docker ps or actually do a docker psa dash hey yeah yeah um attach it sorry ah there we go okay so it ran there's a crease no this is the one we want there's the image yep docker run i dash i t that bash that's what it's hoping to get so now we're inside the container right and we want to do things like well let's make sure we got python we don't but we have python3 all right with 385 that seems pretty decent let's go over to our slash app and see what's here oh look there's the requirements the stuff that we put our main like the complete there's our main this is all good we could do a pip three list let's see it installed fast api and g unicorn and httpx so it looks like it looks like this thing's set up the thing to do though is oops you go out of the way the thing to do is to say what if i run python3 main py will it run okay so it looks like it's running this one is on port 8000 but that's just because we're running the dev mode so the final thing to do is to figure out what happens when we start this container and i like to log in so i can just see like all right what is happening here and that that final thing is this i called it the super run so what we want to do i don't want to do this if he doesn't really love it when i paste let's first right yeah it just doesn't like my continuations i think in the let's try this unrecognized argument what am i doing here in my i mean you're on your own i'll just start yeah you're throwing out random answers like faxing drive for you i might have the order wrong let's see then bind it's for workers oh here we go um okay typo in line eight but that was uh that was a while ago nevermind yeah yeah i think it would probably fix that why does it not like this hold on i have to go duck duck go hold on oh you're saying you need a space there i think it's very possible yeah uh unicorn worker i think this this is all one word there's definitely one word it's like a namespace name yeah the space right there the dash b is a bind right sp is bind to that that horizontal port w is let's just do let's do a simpler version we don't need all of this stuff yet maybe it's just the order yeah like when i'm paste it like that i just need to put map is the very first thing in this example here hold on a second did do cool uh cool in 80. i don't think so let's look at the docs docs were doing it uh hold on sorry folks no this is see this is this is uh this is good it's legit it's happening here all right yeah um because everybody thinks we get on these shows michael and then and then we just code from top to bottom right and then when we're regular coding we do that and you know you don't right you you fumble around you forget a parameter you know you don't write you don't write start your search you know once you figure out the command you're just running it in the script every day right and google has ruined my memory i know it's just so easy yeah it's just so you jump into google what are those exactly you're you're a 10x developer then i think i think so i i'm telling you i had this working before um i think i think you need to i think you need uh dash b space 0.0.0 colon 80. that's my guess okay asp i'm sure i actually think it's going to work in uh docker if i just run it as a three point i'm pretty sure no space between those so do a colon in between there yeah yeah what hire me dude that's the python dude are you kidding me voting challenge i think i i don't know why this is anyway we'll come we'll come back to this so you're getting not quantification overflow we're not sorry sorry michael we're not going to stack overflow we're gonna we're gonna fight through this okay sorry michael good yeah i know good thank you for that um so we got it running over in our doctor now right perfect but what i would like besides that to just work more simply is when i just say docker run that's why i call it super run i just want this to be the thing that happens so i'm going to go over here and say entry point yes right now also when i hit go it should be super fast it should just go all those layers are done we're going to add the entry point and then just start the container right right theoretically i mean you're right i think that this is the actual problem that's you're right i needed a coal that's the problem okay yep it's how to go everything should already be done it's up and running and yeah i did probably misspell the works to workers oh yeah i have this all blown up yeah look at that it's running on port 80 and you see all these worker processes firing up in kind of this load balancing way which is fantastic and so we just go to it right oh no oh sad face oh i know i told it i wanted eight thousand maybe maybe eight thousand it's gonna fail i know it doesn't why is that co we need to expose the port we do and uh python is super helpful for that so i can go down here and hit this expose the port this is like a temporary thing don't do it there because every time you rerun it or make a change it goes away and it's super frustrating so go over here and say edit configuration and say i want to mess with some of these things you can give it a name like um movie service or whatever you want to call it that doesn't really matter um but you want to buy in some ports and you're like okay well there's a directory here that's odd browser what do you mean that right browse the ports what's happening it really just means open the silly ui to like let me type so i want to go to like make it look like 8 000 to me but the server it thinks it's port 80 and view local host from the outside does that look good but on the inside on the inside we're listening to this so we can actually get to it because local host on the inside it won't come out right right so perfect let's try to run this again and see how it works yes uh cap birdie uh live coding is fun very fun all right so now uh let's try instead of going to this well actually it's the same address isn't it yes it works yeah it works now you might be like um did it really work let's yes like if i go over here to my run like you don't even see i have i don't even have a run right now but it's not not running now we don't see the output right here that's kind of underwhelming like i would like to see this thing processing requests and stuff so let me make a minor change over here i'm going to define two environment variables that the unicorn will use called access log but this will make it go to just centered out let's see is that right yeah you think i mistyped it before you think is that right i'm just kidding i think it is we'll see and that goes to warning and then down here or say i want this to go to access log file is quote dollar access oh look at that auto complete that was sweet if we run it one more time i should just have a different run command so it starts super quick it deploys it fires it up and now we put this on the side we make see those requests coming in docker yeah nice now requesting forever run we'll look for blade as in blade runner either trinity we could do blade runner blade runner awesome what do you think well i'm interested in those keywords what were those keywords it's funky crew that last one was like med corporation human android relationship blood apparently blade trinity is a vampire there we go blade blood okay this is this is the this is you're gonna start blushing here michael look michael kennedy's voice is perfect for an audio book would definitely buy a book narrated by him you can't code where the heck but i listen to the no i'm just messing with you yeah i can't configure i can't set my uh host name very well but other than that we're good no this is fantastic this is awesome right and folks out there watching right this is the exact process you go through right a lot of people i'm you know i've been wearing docker for four years i started working with docker before that right and i'm no expert by any means right there's just you could peel back the layers but this is a great way to start even if even if you know a lot about docker start this way right use this use your install scripts look at any kind of automation you were doing to to deploy your apps pull that out start building your docker file don't try and and get the lightest smallest image possible get the fattest clunkiest working image right with your app and then start pulling out and trimming follow our best practices we have best practices on our website um there's a bunch of talks dockercon is coming up uh end of this month we'll have some best practices talks there then start laying on your best practices think about security right but get your application running first if you're trying to worry about best practices making slim images your app's not working you're going to get goofy errors you forgot a you know something in you didn't add something to the os base image that you needed to run a certain module yada yada right on and on and on but the advantage is once you build that docker file and even have the image you could pass the image around you want to do that real quick michael do you have do you have a docker account and we could push this image up there and i can pull it down i do oh you know what never mind i would love to well you could push it anyways but um so we were working on some new fancy features and um let me be careful what i talk about but uh yeah and and so anyways i have a i have a very edge version of desktop running and it's broke so i wouldn't be able to pull it and run it anyways but yeah but the idea is we have a built image and michael say hey peter can you take a look at this right and he could push his image into hub i could pull it down run it local it's his service i could connect all my services to his version that he's running right and we could use compose to do those type of things it's a super easy way especially if you're a cloud native first where you have micro services to pass these micro services around and images you don't have to worry about to get set up and running on your local machine with a different python version you know all those things right um yeah and if you're in desktop sorry i'll be quiet here in a minute no keep going i'm enjoying it keep going uh we do have a new feature that's coming out that we're working on called dev environments to do exactly that and actually i didn't start that discussion thinking about talking about dev environments i just remember we're working on it but it's a way to do exactly what we're doing we're kind of programming together we're working together i have my dev environment set up working and i want hey pete take a quick take a quick look at this um pull i'm going to share my dev environment with you my whole dev environment it's all done in containers and volumes and different things and you pull that up i get the exact same environment that michael has i can help test i can help connect my other services to it makes it extremely easy to instead of checking out a git branch okay now let me install the correct requirements i have if you're on node right like python does a little bit better with doing your workspaces and those type of things um but yeah it's it's very painful anyways uh you'll find out more at dockercon so that was that was uh uh i'd set myself up here i guess so if you haven't already registered please go register it's free we have we're going to have close to 48 over 45 talks um open source to enterprise uh success stories great keynotes um and we're going to be talking about a lot of cool stuff that we've been doing uh over the next year so make sure you register join us it's live free that's awesome my pitch yeah and it's a virtual you can just attend from anywhere i'm guessing say that michael is it virtual or in person virtual yeah yeah i think um well i don't know i wish it was in person i would love to see i would love to see the yeah the tribe but yeah i think it was just a little bit too early i think at the end of the year yeah but people from all the people from all the different places can attend right yeah yeah really i mean it's been really great for us i think last year you know we went virtual we weren't the first to go virtual and we had over 80 000 registrants wow that's crazy that you would blow moscone center out of the water with that yeah yeah i was at the um the year before i was at the keynote right and we had five thousand people five six thousand something like that in attendance at the keynote right and it was just a sea of faces so 80 000 is a i think that's probably as large as the the dallas what's the dallas football stadium yeah that's a huge huge new one yeah that's crazy yeah so and it's nice it opens up to around the world right where folks couldn't you know buy a ticket fly all this san fran even if it was in europe right you still have to buy a ticket fly to the city get hotels right it's and and for those that you know can't afford that you miss out a little bit so yeah um yeah we're trying we're going to have a lot of community um channels local community channels in local languages um so trying to this year trying to connect trying to bring that um in person to the virtual super hard to do for sure but um yeah just trying to connect more into the community so yeah that's really cool well man this has been really fun yeah it's been awesome do you got five more minutes or so we can keep going let me put you back okay yeah yeah put me back up i think just for just for a minute i think because we've got you know um we've got our image built here uh which one probably this is the one i'm guessing um yeah we like this latest one here this image i should probably give it a name but we've let's get out of pycharm for a minute and just put the pie chart away docker run this and there's our server running right and we can we can go over here do i have acp install no this library it's like curl but way way way better oh michael before you do that go go back to your other terminal and um for your docker run do a dash oh yeah yeah yeah thank you yeah i totally hold on no worries i feel bad when i kill so i was reading comments i didn't know you you're totally right that i need to um if i need to kill that one um and then i just do what dash p is it eight thousand eighty yeah eight thousand coolant any yeah you might have to do it before the before the um container id no well we'll see we'll see um anyway we just do curl just so yeah the curl should work it should work it's just not as good i know you're probably right and i i can't kill it from over here can i gotta do a docker ps kill yeah and you can do a doc docker r m f and that'll that'll remove it but stop it first the kill will be if it's kind of stuck then you can kill it yeah okay you got it yeah six one half dozen the other local right if we were in prague you would use them a little bit different because you want to be able to drain connections those type of things right you want to just hard kill it yeah i mean hey i worked production back in the day and that would just kill him you know well yeah give it a shot let's see yeah there we go perfect all right so we've got it working and it's just running over here like we're no longer doing the pycharm things which have a little magic server so i thought it might be fun to create a little application that consumes this library we could do it real quick so um who directed yes simon caught that yep other way around kill dash nine all right so over here we're going to use a library called requests because we don't need the async stuff on the client side it's the server that needs the scalability all right we'll wait for that and we will add that like so cute so whoever gets this next can run with it so we just want to go and write a really really simple application that's going to do a few things so we're going to say get the title equals title from the from the user and then we'll say movie equals get movie from service movie and sorry no movie otherwise we'll just do a little print statement what is this movie thing going to be we're going to rob our little model and put it over here like that i mean technically it's the same sort of thing i kind of want to keep them separate i'm just drop this pedantic model we added i will say from movie model that so this can be one of those and then that'll let us do things like movie.title sweet right just nice yeah was released in movie set year here and directed by director all right so we just got to write this little function which is super easy it says return input i could make this a little better title before good and then this one slightly more complicated but barely f title i guess we gotta pass it off to the service and here we just say response equals requests i've got rel where's our url going to be it is one of these here just register for dockercon nice thank you sir tell your friends you get um no there's there's no there's no but yes tell your friends all your friends and let me know too if you were trying to get into our community slack and you can't get in um put a chat in here and sometimes the slack is interesting so our link to get into the slack dies like every 45 days and you can't redo it and why is joining slack so hard i don't know and i go you're you're a developer you should fix this right like make it just auto renew and just never in the amount of time yeah all right so i think we're good so what we're going to do is we're going to go to our little docker hosted api we just created and do a request make sure it worked get the json convert to one of these models pass it over here all right let's go here and run this thing and i'm gonna aha we're not quite there hold on we need our little main thing i'm gonna put a another one of these and we'll put this over on the side like that and this one can go like that i'm gonna run this code and hope it worked what do you wanna i'm going to put out a movie i'll put blade runner yeah corrected this one you think oh shoot i forget um oh i'm i'm not going to very very famous one that's right i'm going to run it see if it works oh look at that it worked for this time yeah okay blade runner was released in 1982 and directed by ridley scott all right let's try it one more time because this is fun star wars now the the api is kind of crappy it has to be an exact an exact substring like prefix uh so episode four this is the best the best ones by the way star wars new hope nice right directed by uh george lucas but if you just get a star wars like later one turns out that's doug walker yeah but isn't it cool like we got it running over in docker we've got a little client talking to it we've got our little app in like as much production as we're going to get from this demo and yeah there it goes awesome awesome that's perfect yeah and then you could um obviously dockerize your little client you can run those images wherever you want it's beautiful thank you so much man that was fun yeah absolutely i'll i'll put up the github repo here this is where it's going to be i need to log into github apparently uh on my streaming account it's it's expired or something so i'll do that in a moment and then i'll just do a push so okay awesome yeah and i'll um i'll grab this github link i'll put it in the show notes yeah perfect so you can pull it down um but yeah man i really really appreciate it oh let me um let's uh please everybody go check out uh talk python is that probably the main main place to go jumping off point for all things talk python yeah that's probably the right place just uh visit and go from there awesome and uh here is the github url in case any folks want it right away and i'll put it up on the on the screen awesome thanks yeah you've got to wait just a moment for me to log into my github account and do a push but it'll be there yeah okay cool um well then i'll wait but it's in the chat uh and again i'll put it in the show note so you won't miss it um well thanks everybody i really appreciate everybody coming and joining us from all around the world we had a ton of people that was really cool uh michael thank you so much yeah we got it we got our time back on we gotta i'd love to yeah we can do some more okay cool python things it'll be great yeah we'll build some more stuff in python and and go go a little farther down the rabbit hole i'd love that for sure absolutely and i'll mess up some other command line argument again no no i i if you didn't i would be worried that's what i would be worried about right i was like oh he's really good really something wrong with this guy oh he's a genius yeah no because i mean but seriously yeah we all i mean yeah but like more more than you speak in the audience i did not want to just come with like a perfect doctor file and a perfect thing so here's my app i press go boom it works i just wanted to build it from scratch in a real natural way and let people sort of see at least the process that i kind of go through yeah that was fantastic i loved it loved it yeah cool well awesome again thanks everybody for joining michael thank you and uh we'll talk soon you bet bye everyone
Info
Channel: Docker
Views: 9,242
Rating: 4.9396229 out of 5
Keywords:
Id: qQNGw_m8t0Y
Channel Id: undefined
Length: 70min 1sec (4201 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.