Dockerize FastAPI project like a pro - Step-by-step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll see how to dockerize a fast API project let's start by creating Python's virtual environment activate the virtual environment now install fast API in unicorn let's create main.py file for intellisense locate the virtual environment in vs code import fast API from Fast API initialize the app like this now create the get request Handler in the root endpoint this endpoint will return Json with Hello World message let's run the ubicorn server now and in the browser if we go to 127.0.0.1.8000 we should see the hello world Json message let's go to the Swagger UI and test the API from there as well try it out and execute stop the server now say the dependencies on requirements.txt file we can now start dockerizing our hello world application built in fast API create the docker file we are going to use Python's 3.8.10 slim image as the base image let's set slash app as our work directory copy everything from the present directory to the work directory and write a command to install the dependencies in the docker image to not to copy unnecessary files and folders We can create the docker ignore file and write the following lines now we can create the docker compose yml we are using version 3. let's name our service web we can use present working directory where we have our Docker file as the build context now write the command which will be executed when the container starts this will be the Unicorn command which will start the fast API server next we can map the host system's 8000 port to the container's 8000 port let's build the container and see if it works in the browser we can go to 127.0.0.1 Port 8000. and ola it works and it also works from the Swagger UI let's see if the hot reloading works oops it don't seems to be working we have to sync our host machine with the docker container so the uvicorn server can listen to the changes in our files for that we have to mount the volume in Docker compose file we have to restart our Docker container the server is up again let's do some changes in the API response and yes it seems to be working now because we saw the server reloading in the logs now our next task is to read some data from the environment using environment variables let's create a DOT ENT file and add a key value pair inside it in our main.py file we can fetch the secret value from the environment and return it with the API response in our Docker compose file we have to provide the path to our environment file everything looks fine let's rerun the server again and ola it's fetching the value from the mounted environment file thank you so much guys for watching this quick tutorial in the next video we'll see how to make this project production ready
Info
Channel: Stackless Tech
Views: 13,525
Rating: undefined out of 5
Keywords: python, fastapi, tutorial, docker, dockerize, programming, learning, learn, python3, api, api development, learn fastapi, dockerize fastapi, fastapi dockerize, dockerize python, fastapi microservice, python microservices, learn advanced python
Id: CzAyaSolZjY
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Thu Apr 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.