How to deploy your Streamlit Web App to Google Cloud Run using Docker
Summary
TLDRThis video tutorial guides viewers through deploying a machine learning or deep learning model using Streamlit for UI development and Google Cloud for hosting. Key steps include setting up Docker and gcloud CLI, creating a Streamlit UI, containerizing the app with Docker, and deploying it on Google Cloud Run. The presenter also covers continuous deployment for seamless updates and emphasizes the importance of testing locally before deployment.
Takeaways
- 🚀 **Deployment Readiness**: The video is aimed at those who have a machine learning or deep learning demo ready for deployment.
- 🌐 **UI Development**: Streamlit is recommended for UI development due to its simplicity and integration with Python, requiring no knowledge of JavaScript, HTML, or CSS.
- 🔗 **Resource Links**: The presenter provides links to a Medium article and other resources for further reading and assistance.
- 🛠️ **Prerequisites**: Key prerequisites include Docker, gcloud CLI, a Google Cloud billing account, and the user's own machine learning model.
- 📝 **Documentation and Community**: Streamlit's extensive documentation and active community are highlighted as valuable resources.
- 💻 **Local Docker Container**: The process involves creating a local Docker container, testing it, and then building the final container using Google Cloud Build.
- 🔄 **Container Registry**: Google Cloud Container Registry is used to store and manage Docker containers, which can be deployed from Google Cloud Run.
- 🔧 **Configuration Settings**: The video discusses configuring settings such as port specification, resource allocation, and timeout settings for the deployed app.
- 🔄 **Continuous Deployment**: Setting up continuous deployment with Google Cloud Run allows for automatic updates to the app when changes are pushed to a GitHub repository.
- 🔗 **Public URL**: Upon successful deployment, a public URL is provided, allowing access to the app for sharing and use.
Q & A
What is the main topic of the video?
-The main topic of the video is deploying a machine learning or deep learning demo using Google Cloud services.
What are some prerequisites for deploying the demo as mentioned in the video?
-The prerequisites include having Docker installed, gcloud CLI installed, a Google Cloud billing account enabled, and your own Python machine learning or deep learning model ready for deployment.
What UI framework is recommended in the video for developing the interface?
-The video recommends using Streamlit as the UI framework because it integrates directly into Python code and requires no knowledge of JavaScript, HTML, or CSS.
Why is Docker used in the deployment process?
-Docker is used because it abstracts the app into a self-contained environment, making it portable and easy to move across different platforms that support Docker containers.
What is the purpose of Google Cloud Build mentioned in the video?
-Google Cloud Build is used to automatically build the Docker container image and deploy it directly to the Google Cloud Container Registry.
How does the video suggest testing the Docker container locally?
-The video suggests building the Docker image locally, running it, and mapping port 8080 to test if the app works inside the container before deploying it.
What is the significance of the Google Cloud Container Registry?
-The Google Cloud Container Registry is a service that helps keep track of all the containers pushed to it, allowing users to manage and deploy them easily within the Google Cloud console.
How can one ensure their app is updated efficiently after making changes?
-The video suggests setting up continuous deployment by linking the GitHub repository to Google Cloud Run, so any changes pushed to GitHub are automatically deployed to the web app.
What is the role of Google Cloud Run in deploying the app?
-Google Cloud Run is used to deploy the containerized app from the Container Registry, providing a public URL to access the app.
What does the video suggest regarding the timeout settings for the app?
-The video suggests specifying a timeout based on the app's needs, with a default of 300 seconds (5 minutes), which can be adjusted up to 3600 seconds (30 minutes) for larger apps.
What are some potential issues to watch out for when containerizing an app?
-Potential issues include runtime problems that may not have been apparent in the local environment, which is why it's important to test the app thoroughly in the local Docker container before deploying.
Outlines
🚀 Introduction to Deploying ML/DL Models
The speaker introduces a video tutorial aimed at individuals ready to deploy their machine learning or deep learning models. They mention covering everything from the user interface (UI) to deployment, making it accessible for sharing with others. The speaker references a medium article for further reading and suggests using Streamlit for UI development due to its simplicity and integration with Python. They also highlight the need for Docker, gcloud CLI, a Google Cloud billing account, and the viewer's own machine learning model. The speaker acknowledges that while they won't cover basic setup details like Docker installation, there are many resources available online.
🛠️ Containerizing the App with Docker
The speaker outlines the process of containerizing the app using Docker, emphasizing Docker's role in creating a self-contained environment for the app. They explain that this portability ensures the app will run consistently across different platforms that support Docker. The tutorial includes instructions for creating a Dockerfile, building the Docker image locally, and testing it to ensure it works before deploying. The speaker also discusses the importance of exposing port 8080, which is the default for Google Cloud Run, and provides a command for building and running the Docker container locally. They stress the importance of testing the app locally before proceeding to avoid runtime issues on the cloud platform.
🌐 Deploying to Google Cloud Container Registry
The speaker continues with the deployment process, focusing on pushing the Docker container to the Google Cloud Container Registry. They provide a command that uses the Google Cloud CLI to tag, build, and push the container to the registry. The speaker discusses the importance of setting a timeout for larger builds to prevent timeout errors during deployment. They also mention verifying the container's presence in the registry and explain how to link it to Google Cloud Run for deployment. The speaker shares their experience with large builds and the need for patience and proper configuration to ensure successful deployment.
🌟 Final Deployment and Continuous Integration
The final paragraph covers the last steps of deployment, including navigating to Google Cloud Run, creating a new service, and selecting the container image from the registry. The speaker discusses service settings such as resource allocation, unauthenticated invocations, and timeout configurations. They also touch on the efficiency of continuous deployment by linking the GitHub repository to Google Cloud Run, allowing automatic updates to the app when changes are pushed to GitHub. The speaker concludes by encouraging viewers to ask questions and offering to create additional videos on specific topics if requested.
Mindmap
Keywords
💡Machine Learning
💡Deep Learning
💡Deployment
💡UI
💡Streamlit
💡Docker
💡gcloud CLI
💡Google Cloud
💡Containerization
💡Cloud Run
💡Continuous Deployment
Highlights
Introduction to deploying a machine learning or deep learning demo
Overview of the process from UI to deployment
Reference to a medium article for detailed instructions
Prerequisites for deployment: Docker, gcloud CLI, Google Cloud billing account, and a machine learning model
Assistance available online for setting up Docker and gcloud CLI
Introduction to Streamlit as a UI framework
Streamlit allows for quick UI development with minimal knowledge of JavaScript, HTML, or CSS
Example of a simple UI built with Streamlit in under 50 lines of code
Streamlit community and documentation as resources for support
Alternative UI framework suggestion: Radio
Step-by-step guide to containerize the app using Docker
Explanation of Docker's role in creating a self-contained environment for the app
Instructions on building and testing a local Docker container
Use of Google Cloud Build for final containerization and deployment
Importance of testing the app locally before moving to cloud platforms
Process of pushing the Docker container to Google Cloud Container Registry
Final step of deploying the app on Google Cloud Run
Options for resource allocation and timeout settings on Google Cloud Run
Continuous deployment setup with Google Cloud Run and GitHub
Access to the public URL of the deployed app
Offer for further assistance and the possibility of a follow-up video
Transcripts
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
Ver Más Videos Relacionados
Deploy Hugging Face models on Google Cloud: from the hub to Inference Endpoints
Invoke application deployed in Cloud Run from Apigee Proxy
End of local AI Apps ?? HuggingFace Spaces + Docker + FastAPI
Deploying a Machine Learning Model (in 3 Minutes)
The intro to Docker I wish I had when I started
How to Run a Python Docker Image on AWS Lambda
5.0 / 5 (0 votes)