How to deploy your Streamlit Web App to Google Cloud Run using Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so you've got some machine learning or deep learning demo and you're ready to deploy it somewhere that's awesome this is the video for you um and we're gonna go through everything from the UI to getting it deployed and ready and available for you to use and share with your friends with your colleagues whoever you want to share with and however you want to use it so for the UI we'll we'll just jump forget the UI sorry we'll just jump straight into it I'll be referring to this medium article on my page I'll link it in the description below so you can follow along with me otherwise just you can follow along in this video by yourself too that works um jumping straight into it some of the things you're going to need are oh also this is uh what I'm referring to you may notice it'll be a little bit different this is just a draft right now by the time this video is up it should be available for everyone some of the things you're going to need to get started are just to have Docker installed on your system a gcloud CLI installed on your system a Google Cloud billing account enabled and your own python machine learning deep learning model that you want to get deployed if you need help with any of these things whether that's installing Docker the gcloud CLI you're setting up your Google Cloud billing account there's a ton of videos and support for that online feel free to just check those out yourself just for the sake of time and just efficiency I'm not going to dive into them in this particular video let me know in the comments so if you want me to cover some of those things and how I do them otherwise there's a ton of resources out there for you to figure out how to do that stuff too um once you've got those things set up we then an outline of of how this process goes is first we'll develop your UI using streamlit because it's one of it's it's a really awesome UI framework that doesn't require any knowledge of JavaScript or or HTML or css really it just integrates directly into your python code and you can get started in literally a matter of minutes it's it's really quick really efficient and the documentation available is is awesome they have a lot of support as well so if you're confused and you're looking for some some answers there's there's awesome Community around streamlit here's an example of my UI right here this is literally like it's kind of crazy to think that I can flesh out a whole UI including a login screen and my home page in under 50 50 lines of code and a lot of this is really actually just boilerplate it's a template a couple templates that I can just squeeze together if you want to find some templates or find documentation on how to build your app I've linked all of that in the blog you can check it out here here streamlined its own home page here's the documentation streamless documentation so yeah please check that out if you want an alternative to stream let's say it doesn't work for your particular use case you can check out radio it's another great alternative and please don't feel daunted or feel scared that oh my gosh here's another framework I need to learn to get my web app up and running I promise you it's it's it's extremely extremely simple it'll take you maybe like an hour max to to build out a very simple one um but yeah so let's let's dive into these steps so now that we've kind of talked about the UI which is streamlined of course let's go into step number two and kind of unpack what that that what happens there a little bit so we're going to need to containerize our app um and we're going to do this in two steps first we're gonna containerize it locally using Docker and for for those I'm no Docker expert but I'll explain the the little understanding that I have on it um basically from what I understand Docker abstracts your app to its own it's kind of like a virtual environment as you can say for your app in particular that works inside of itself it's self-contained to the point where once you know it works in that container you can move that container around it's very transportable so you can move it if you want to have it on AWS or if you want to have it on Google Cloud because you know it works in this container and it has all the dependencies it needs in this container it can go and work anywhere else that supports these Docker containers and because Docker is widely used it's it's a very high likelihood that it will be supported um and so for this particular case we're going to do it in two steps we're going to build a local Docker container test it locally make sure it works inside this container and then we're gonna actually use Google Cloud's um the gcloud CLI and Google Cloud build which I will reference here Google Cloud build and right now it's not in the blog but I think I should add it because it's a key step but we're going to use Google Cloud build to help build our final container which and it'll automatically build this image and deploy it directly to a Google Cloud Google Cloud container registry which is a registry that helps you keep track of any and all containers that you push to there and you can figure out how to use them however you want inside of the Google Cloud console itself so the commands for this are here so Docker um sorry streamlit has extensive documentation on how to deploy with Docker right here and you can check this out um but they they walk you through deploying a very simple web app um using docker we're going to make a few adjustments for this use case I would highly recommend checking this out if this is your first time doing it it'll just help you become more familiar with the process and and make little changes of your own for your use case here's the docker file that I wrote for mine and this is kind of like it's basically just a a copy of this with some minor changes and those minor changes are I'll explain them now is first I've exposed port 8080 both here um in the expose here and in this entry point because what Google Cloud run does by default is it runs on port 8080 so you're going to want to specify this just so you don't run into any startup issues in the beginning although you can't change Google Cloud runs default Port later on but just to save you time let's just specify it from the start so you can use your app as soon as possible and once you've specified your Docker file um we're going to build that image locally using this Command right here Docker build Dash T and then image name can be whatever you want it to be um it's not it it doesn't it's it doesn't really matter and excuse me there should be a space here and don't forget this period just hit enter and it'll build that container for you then you're gonna run it and we're gonna test to make sure that it works inside that container locally using this we're going to specify the port 8080 we're going to map port 8080 on our machine support 8080 in this Docker container and we'll run it we'll specify the name of the container which you just created um this is a really important step make sure it works here locally before you move on anywhere else otherwise if it doesn't work in this container it's not going to work on Google Cloud run it's not going to work on AWS so just make sure that you've got everything working in my case um there were a few errors issues not particular errors but runtime issues that I came into with my app when I tried containerizing it and I I was stubborn and tried deploying it without fixing them thinking it would just magically work but it won't just spend spend a little bit of time making sure it works in in your local Docker container before moving on to the next step sweet and the next step is the final Docker Bill final build of your container and pushing it to um the Google Cloud container registry which I will link here in the blog as well this is the container registry so we're going to use this command which is it uses the Google Cloud CLI which you've installed and what this does is it tags and builds your Docker container and automatically pushes it to the Google Cloud container registry which you can then link or or reference from Google Cloud run you can reference that specific container and then so say like Google Cloud runs here actually I'll show you I'll show you an example in my case but yeah you can reference that container which you pushed to the to the registry inside Cloud run link it and then deploy it from cloud run I'm using this command so this project ID what this is is I'll go to my services right here you'll create your Google Cloud billing account and um wherever you are if you're in Cloud run or if you're in you can even be in the compute engine area you're always going to have this header bar you can just click this and whatever your project name is here's the ID and you're going to copy that and you're going to come on over and swap it for this value right here and some project name once again this can be anything you like in my case I named it like app.container or something like that or app dash container and then this is an important one I maybe I mentioned I forgot I'm like kind of spacing out a little bit but I'm not building this particular project with you guys because in my case it's a pretty large build there's a five gigabyte checkpoint file for my project which takes a long time to deploy which is why I'm just walking you guys through what I've done but because my project was so big I kept running into timeout errors so it's really important if your project is larger to specify a timeout otherwise it'll just it'll just keep on failing so I was kind of just stubborn and put a two hour timeout so that I didn't have to worry about it timing up now once you run this command it'll it'll do everything automatically and the final result should be something like this so you can come here I'm just going to search for the container registry and here it is I guess it's under the CI CD section I'm just going to click container registry and the final result should be something like this and in my case it's called captionit container and this name Will coincide with whatever you choose to name it here here's some project name and that'll be the final result you can one once once the once this command finishes running just hop on over to your container registry and verify that your container is there and once you've done that we can move on to the last and final step which is getting your app deployed this is what we want this is why we want to be um congratulations if you made it to this point let's push on forward and get your app out there so now we're going to navigate over to Google Cloud run um I have mine pinned over here otherwise you can just search for it and here you see my app streamlined I named it streamlit um you can name it whatever you want um you're not going to see anything here to start off with you're going to actually have to create it so let me just expand this a little bit you're gonna have to create click this button create service and then inside here your container image URL don't worry you don't have to memorize anything or find some URL you can actually just select and here it'll link that container registry like I talked about you should see something like this which is a reference to that container registry that I taught that I showed you guys before here's my container registry and here are a few images in my container registry in your case I don't know how many you'll see I think you should just see one because I I did this process quite a few times I created a few images but you should just see one so you can select that and then hit select and that'll be your your containerized app you can give it a name um here this this uh this particular section if it uh it helps you save money by allocating the CPU only or or the resources CPU Ram whatever only when the app is called so that way when there's downtime no one's on your app you're not wasting money um with with uptime um you're going to want to check allow unauthenticated invocations what this does is it allows just anyone to access your url so you share it just like a normal website if you don't want that you can of course click this require authentication um and then another thing you can go down here if you might you may need more resources so here is a section where you can specify the port like I said 8080 is specified by by default um one important area is because once again my app was larger I was using an open source AI Transformer I wanted more CPU resources so you can just actually specify up to eight cores or eight V8 V CPUs um whatever that means and how up to 32 gigabytes of RAM this timeout what this is is after 300 seconds we can check how much that is 300 divided by 16. so after about five minutes based on this 300 um your app is going to time out and you you'll have to hit the refresh button for it to start up again you can specify this up to 36 100 which is I think 30 minutes um so for a 30 minute timeout so yeah you can you can mess around with these these settings up to your preference and then eventually when you're done you can hit the create button and when you hit that create button it should take a little bit to build that container deploy it and but it should do it all automatically you won't have to do anything else and eventually you'll see this green check mark you'll have your own service created and then here will be your service details and you'll be provided a URL right here this is a public URL that'll allow you to access your personal app so we can click that which is mine um and once again it's just starting up so it'll take a take a little bit um and here's my streamlit app starting up but yeah you should have access to revisions the really cool thing about Cloud run is if you make any changes you can redeploy your app once again just using that um command that I showed you over here uh this command you can send it back up create a new container and then um it you can you can link it as a new revision or something and you can always redeploy and you'll have your updated web app the thing is though that's that's inefficient because say in my case it took me like I don't know like 30 minutes each time that to get it up and deployed that's inefficient you can actually set up this thing edit continuous deployment right here and what this does is it allows you to hook up your GitHub repository to Google Cloud run and then you can push like right here this is my code which is linked to GitHub any changes I make here I can push to GitHub and then the Google Cloud run service will automatically pull those changes from GitHub and then um it'll it'll show those changes on your web app I can make a separate video on this I've I've got it set up for me I can set up I'll probably make a separate video on this later let me know if if that's something you want urgently um and I can I can do that video sooner but that's something I plan to make it's a very extremely efficient way of of um of making changes to your app yeah that's basically the principle you can look into it yourself of course um but yeah and it's still running um like I said my app's a little larger so it takes a little bit but yeah I'll just exit out of here you'll have your own public URL to view your app but uh that should be about it honestly for this process I I hope it's this straightforward for you um let me know in the comments if you have any questions I'll do my best to help you I I want to make sure that you guys get to this point as well so please yeah let me know if there's anything you need um Let's uh we'll do what we can take
Info
Channel: Faizi Fifita
Views: 8,313
Rating: undefined out of 5
Keywords:
Id: Djnnp-g3ep0
Channel Id: undefined
Length: 16min 28sec (988 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.