Up and Running With FastAPI and Docker

UPDATED ON:

undraw svg category

It’s May 2020 and we all have a lot of extra indoor time. I’ve hunkered down and learned a few new technologies and frameworks. I’d like to share my favorite one with you here - the FastAPI framework.

Though I’d heard good things about FastAPI for a while, I never had a chance to dig in. Now that I have, I must admit I’ve been pleasantly surprised with the developer experience. I’ve also been fortunate enough to integrate it into my professional work and our team has enjoyed the benefits as well.

This series of posts is going to walk through the things I’ve learned composing my first few FastAPI backends.

Up And Running With FastAPI

Part 1 is here and will focus on developing the skeleton and basic plumbing needed to get any project idea started.

Just for fun, we’ll build the backend for a crowd-sourced cleaning marketplace called Phresh. Users can schedule cleanings, others can select jobs to take up and get paid for their work.

Environment and Setup

Never heard of Docker before? This might be a wild ride for you. See my article on Just Enough Docker To Get By , and then come back here.

Already up to speed? Great. Make sure Docker desktop is running.

We’re going to structure our application like so:

I’m sure you can handle this on your own, but for consistency, here’s the commands you’ll run to setup most of that structure:

Everything look ok so far? We’re about to add some dependencies, spin up a quick and dirty server, and run Docker - so grab a cup of coffee and prepare yourself.

Install packages

This app will use quite a few packages by the time we’re done with it, but we only need a few to get up and running for the time being.

Don’t worry about installing anything - Docker will handle that for us!

Head into your requirements.txt and update it with the following code:

requirements.txt

Not too bad. We’ll add more in a minute, but for now only four packages:

Create a server

Ok. Finally time for a little python. Only a little though.

Create a file called server.py inside the api directory, along with an __init__.py file.

Inside server.py, add the following:

server.py

A few interesting things going on here. We have a factory function that returns a FastAPI app with cors middleware configured. Don’t worry too much about the cors stuff - this is a rabbit hole that I don’t feel like diving into at the moment. If you want to read more, MDN has some great docs on it.

You’ll also notice we’re importing this middleware from the starlette package. FastAPI is built on top of starlette, and we’ll occasionally dip into the underlying architecture to accomplish a few things. Just a heads up. You don’t need to worry too much about this either, but feel free to checkout the docs here if you want to learn more.

The developers behind FastAPI have been hard at work trying to create an interface over most of the Starlette architecture, so it’s actually possible to import this directly from fastapi now.

server.py

Spin up a docker container

That was fun. Now let’s do a little docker handywork.

In your Dockerfile, add the following code. No need to understand this all yet either - we’ll dive into the specifics later on.

Dockerfile

We pull the slim-buster docker image for python 3.8, set the working directory, and add environment variables to prevent python from writing pyc files to disc and from buffering stdout and stderr.

Then, we copy over the requirements.txt file, install the necessary dependencies, and copy our app into the backend folder.

Like I said - not a big deal if you’re not up to speed with many of the commands here. It’s not our focus, though we will dissect it in more detail in a future post.

If you do want more info, the majority of this docker setup is derived from Michael Herman’s excellent TestDriven.io tutorial . A link to his full course on FastAPI can be found at the bottom of this post.

In your docker-compose.yml file, add the following:

docker-compose.yml

A few things going on here

And finally - lets build our docker container and get our server up and running.

Bootstrapping our application

To build the appropriate Docker container, run the following from your terminal:

This will take a little while, so sit back and sip on that coffee you made earlier.

When it’s done building, enter your container with:

You should see some friendly logging giving you some info on how your server is doing.

Open up your favorite browser and go to localhost:8000. You should see the first json response from your server:

Routing

Let’s set up a few routes to get our application off the ground. The routing system in FastAPI is pretty straightforward.

We’ll start by creating a new directory inside of backend/app/api called routes and a new routing file, cleanings.py.

Inside your cleanings.py file, we’ll create a router, and add a few standard routes to get a feel for how this works in FastAPI.

api/routes/cleanings.py

Then, in the backend/app/api/routes/__init__.py file, we’ll create another router, import the router we just built, and assign it to the cleanings namespace.

api/routes/__init__.py

And now do the same in your server.py file.

server.py

Now head back to your browser and open up localhost:8000/api/cleanings/ and you should see the json response consisting of both cleanings we defined in our GET route.

Amazing! And with that, we’ve bootstrapped a simple API using FastAPI and Docker.

Wrapping Up and Resources

In the next post, we’ll set up a container hosting a PostgreSQL database and hook it up to our FastAPI backend. In the next couple, we’ll start testing our application using Pytest, Docker, and httpx.

Github Repo

All code up to this point can be found under the master branch.

Special thanks to Daniel Aguiar for correcting mistakes in code sample from the original post

Tags:

Previous Post undraw svg category

Just Enough Docker to Get By

Next Post undraw svg category

Configuring a PostgreSQL DB with your Dockerized FastAPI App