How I deploy serverless containers for free

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the most annoying things in the world is when you're trying to steal an image from the internet and it looks like it's going to have a transparent background but when you download it it looks like this luckily nowadays there are all kinds of good tools for removing backgrounds from images like remove BG or the new AI Tools in Photoshop now if you watch my videos you'll notice I use a lot of images with the background removed and it's extremely inefficient to have to go into Photoshop upload an image remove its background and then reexport it and bring it back into Adobe Premiere as a developer this inefficiency is to unacceptable and my only option was to build my own app from scratch in today's video I want to show you how I built this background remover from scratch but more importantly I want to talk about why I dockerized it and explain how I deployed it to the cloud for free earlier this week on the main Channel I made a Docker video which you should definitely check out first if you have no idea what Docker is but that video was sponsored by Docker this video is not sponsored I don't work with sponsors very often but when I do it's usually with tools that I have firsthand experience with and Docker is something I use all the time by containerizing this background remover which is just a python web app I'm able to run it locally with the click of a button and also deploy it to the cloud with a single command without Docker I would have to go into my terminal make sure I have the right python dependencies installed and then run the app in the background every time I want to use it and on top of that deployment to the cloud would be a lot more complex and also more expensive first I want to talk about the app itself the reason I built this app in Python and not my typical choice of JavaScript is that there's a python package called remove BG which is based on the uet model to magically remove the background with AI I oh man I just realized I said the a word again I guess we have to reset the counter but really this video has nothing to do with AI the model itself is heavily abstracted to the point where all we do is open an image with pillow call the remove function from this Library which returns a new image with the background removed it's an extremely practical use case for image models but I don't want to use it from the terminal I want to be able to drag and drop images directly from my browser into it so I can then drag the result directly back into my Adobe Premiere timeline to do that I built a little app with flask which creates a single HTTP route that handles both git and post methods git displays the initial web page and then when we drag an image into that web page it makes a post request which calls that remove background function now the website itself is rendered in this index.html file which uses nothing but plain JavaScript and CSS there's an HTML form with a file input then when that form is submitted it makes a post request to the root URL I also wrote a little bit of JavaScript here that will automatically submit the form when a file is dropped onto it just to make the process even more efficient that's the entire app and now I can run it from the terminal with the python command but now here is where Docker comes in I want to be able to use this code on multiple computers and I also want to deploy it to the web so I could even use it from my phone or some other device and I want everyone in the world to have the opportunity to use my awesome invention at first you'll need to have Docker installed I'm doing that through Docker desktop but in the past I've used tools like podman which is developed by red hat and is also a good option but now we need to go into our code and create a Docker file the docker file itself is very simple it starts with the official python base image creates a working directory for the app installs the dependencies copies the code exposes a port and then runs the app the only unusual thing I'm doing here is taking the actual AI model weights and copying them into the docker image the weights are about 175 megab and doing this prevents the actual python package from downloading asynchronously which will slow things down in general now let's build the image and make sure to give it a tag that'll take a minute then if we go into Docker desktop we should see it here in the images panel to actually run the image as a container we simply hit the play button button and make sure to map the port to something we can use on Local Host and now the app is always ready to go in the background with Docker pretty cool and that's how I use this tool 90% of the time but I also want to show you how to deploy it to the web there's a bunch of different options for deploying containers to the cloud and there's also some free options if you're cheap like me the most well-known option is elastic container engine on AWS with a related service called fargate that can deploy your container in a serverless way which means it will scale down to zero when it's not in use and then scale back up once the requests are coming in you've also got services like the app platform on digital ocean which starts at $0 a month but my go-to for deploying random utilities like this is Google Cloud run to deploy something that's dockerized though you first need to get your image on a registry every cloud has one built in and on Google it's called artifact registry what you do is create a repository for your images that'll store them in a specific region and then you can copy this link up here which can be used as a tag on your images so it knows where to upload them let's go into the terminal and use the docker tag command to tag our existing image with this namespace once that's done we can use the docker push command to upload it to Google Cloud now one caveat is that of course you need a Google Cloud account and you'll also need the g-cloud CLI tool installed on your system but once that's done you should then be able to see the image in Google Cloud one nice thing about this is that if you want to use this image on a different machine you can simply pull it from this repo but now let's head over to Cloud run and deploy it to the internet create a new service then the first thing you'll do is Select that container image now from here we have a bunch of configuration options but if you want to make this a public web service the most important one is to allow unauthenticated invocations that means anybody can access it from a public API or URL the next option is CPU allocation one problem with serverless deployments is that when the app is not being used it scales down to zero which is great because it means you're not paying for anything but the trade-off is a cold start which means it takes like four or 5 seconds for the thing to boot up when the next request comes in in my case that's not a problem but if you want to eliminate cold starts you can make sure that the CPU is always allocated it's just going to cost more because you'll always be burning through these free CPU seconds every month now from there let's go down to the container options and one thing we'll also want to change here is the allocated memory for the container it takes a lot of memory to run the AI model so let's bump this up to 2 gb one other thing I want to do is also decrease the amount of Auto scaling this thing can do instead of 100 maximum instances I'm only going to allow three we just don't need to be prepared to scale for this type of app but it's nice to have that option if you're building something viral let's go ahead and deploy it and a few minutes later we should have a URL where we can actually access our python app on the web pretty awesome and one huge benefit of having this all dockerized is that our code is portable so if we want to get off Cloud run we could take that to any other cloud service and deploy it there just as easily and that's basically all there is to it there's a ton of other stuff we could talk about when it comes to Docker and Cloud run but let me know what you want to see next in the comments I do have one update for fireship pro members I'm currently finishing up a new stripe course that's designed specifically for people building software as a service products more details to come on that soon thanks for watching and I will see you in the next one
Info
Channel: Beyond Fireship
Views: 451,396
Rating: undefined out of 5
Keywords:
Id: cw34KMPSt4k
Channel Id: undefined
Length: 6min 32sec (392 seconds)
Published: Fri Mar 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.