Live Coding: Refactoring Docker Related Files in a FastAPI App
This is a live coding video that goes over how I tackle Dockerizing a project without any prior experience with FastAPI.
I’m starting a new project with a friend and just for fun we decided to try FastAPI out since it’s a fairly small API back-end and we wanted to see what FastAPI is all about.
Neither of us have any experience with it, but she started the app off using a boilerplate project that was found on GitHub. In this video I go over changing around pretty much all of the Docker related files and also learn a bit about FastAPI along the way.
There’s no cuts in this video, it was recorded as I did the work for the first time. Since I was doing the work anyways, it felt like a good spot to record a video around how I go about learning new things in real-time.
# Live Demo
Timestamps
- 0:40 – I only built the original Docker image beforehand
- 1:40 – Upping the project and fixing a FastAPI validation error
- 4:24 – Checking out what the app does in a browser and curl
- 6:56 – Trying out a few FastAPI config experiments
- 9:29 – Investigating the Dockerfile
- 14:24 – Pulling in a new Dockerfile from my example Flask app
- 17:30 – Getting the latest versions of each Python dependency
- 19:29 – Copying over new .gitignore and .dockerignore files
- 21:35 – Creating a new docker-compose.yml file
- 24:45 – Creating a new .env file
- 30:26 – Iterating quickly on our uvicorn CMD with docker-compose exec
- 36:15 – Removing the reference to .env in the FastAPI settings
- 38:04 – Fixing “empty reply from server” by binding to 0.0.0.0 not 127.0.0.1
- 39:31 – Double checking if our env variables and volumes work
- 43:00 – Trying to get a config file to work with uvicorn directly
- 49:55 – Loading uvicorn through gunicorn as a worker type
- 57:37 – Moving all of our ad-hoc gunicorn flags into a config file
- 1:00:04 – Does it actually work? What about certain config options?
- 1:03:45 – Creating a new branch and committing our changes
- 1:04:16 – Swapping PROJECT_NAME to use our existing COMPOSE_PROJECT_NAME
- 1:07:49 – Checking out the auto-generated OpenAPI documentation
- 1:08:33 – Reviewing our changes and addressing the SERVER_NAME
- 1:14:34 – Setting the SECRET_KEY another time and finishing things up
- 1:18:59 – Any questions?
Reference Links
- https://nickjanetakis.com/blog/best-practices-around-production-ready-web-apps-with-docker-compose
- https://github.com/nickjj/docker-flask-example
- https://github.com/nickjj/docker-django-example
- https://nickjanetakis.com/blog/replacing-make-with-a-shell-script-for-running-your-projects-tasks
What would you have done differently? Let me know below.