Containerize a Python FastAPI app with Docker in 4 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i'm going to cover containerizing a fast api app using docker i already have a basic hello world api from my previous video that i'll be reusing here so the first thing we need to do is to create a docker file in here we specify the base python image to build from i'm using python 3.9 slim i'm using the slim version which only contains the minimal packages needed to run python just to keep the size down this makes python available to us on the container so we can install our requirements with pip and run our code using python the next step is to copy over our source folder into app source if you're using a different directory name you'll have to make that change here we then copy over our requirements.txt file so that can be used now we need to set the working directory for the container with that done we can install our dependencies so we pip install requirements.txt next we expose the port the app will run on i'm using the default port 8000 change this here if you need to and finally we set the command to run when the container starts in this case we're going to run new recon and pass it several options source.main colon app is the entry point for our app source is the directory name i've used main is the name of the file and app is the name of the fast api app in the code if you've changed any of these values in your code update them here next we have set the host to all zeros this makes the application available on our local network and finally i've set the reload option on the container this is so that i can make a change to the code and see reflected without having to restart the container now you can build the image using the docker build command i'm tagging mine as fast api dash hello world and giving it the version 0.1 once that's done we can run the container using docker run binding the port 8000 on the container to port 8000 on our machine i've also given the container a name so we can interact with it later without using the container id then i specify the tag from the build command now if we call our endpoint we can see the api is running and returned our message and in the terminal output from the container you can confirm that one last thing to know with fast api at the current version is that if you run this container it will fail to exit if you try to close it using control c this is due to an issue with the reload argument a bug in uvicon causes that to mess with the interrupt signal so it doesn't stop this way instead you can use the docker kill command passing in the name given the run command in my case my api and that will kill the container if you were to run this in a docker compose instead that would automatically close the container after the default time period of 10 seconds and that's it you've now got a hello world api using fast api running in a docker container hopefully this has been useful and hopefully it highlights how easy it is to get going with fast api and docker please like and subscribe if you found this useful as i'll be making more videos diving deep into fast api as well as covering other frameworks under the languages if anything was unclear please drop a comment below and i'll try to help out or check out the github link in the description thanks for watching
Info
Channel: It's That Ian Guy
Views: 22,802
Rating: undefined out of 5
Keywords: docker, fastapi, python
Id: 2a5414BsYqw
Channel Id: undefined
Length: 3min 56sec (236 seconds)
Published: Thu Dec 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.