DEV Community

Cover image for How to Dockerize FastAPI
Jonas Scholz
Jonas Scholz Subscriber

Posted on • Edited on

60 7 7 7 7

How to Dockerize FastAPI

If you're just here to copy and paste, here's the final Dockerfile that will produce an image for your FastAPI app:

FROM python:3.9-slim

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app

CMD ["fastapi", "run", "app/main.py", "--port", "3000"]
Enter fullscreen mode Exit fullscreen mode

And here's the .dockerignore file you should use:

__pycache__
*.pyc
*.pyo
.venv
venv
dist
build
Enter fullscreen mode Exit fullscreen mode

Depending on what else is in your project, you might want to add more files to the .dockerignore file! You should realize if the context copying takes too long.

To build and run the image, use these commands:

docker build -t fastapi-app .
docker run -p 3000:3000 fastapi-app
Enter fullscreen mode Exit fullscreen mode

Not just here to copy and paste? Let's go over what's happening in the Dockerfile!

let's do this

(can I still use that gif or is it uncool already? :/)

The Setup

For this tutorial, I assume you have a FastAPI project set up. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. If you have a different setup, you might need to adjust the Dockerfile accordingly.

Typically, you'd run pip install -r requirements.txt and then fastapi run app/main.py --port 3000 to work locally. For deployment, we'll use a similar approach but within a Docker container.

Let's get into the details of the Dockerfile!

The Dockerfile

FROM python:3.9-slim

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app

CMD ["fastapi", "run", "app/main.py", "--port", "3000"]
Enter fullscreen mode Exit fullscreen mode

So what's happening here?

  1. Base image:
    • Uses Python 3.9, providing a solid and widely used base image. You can also use a more recent version of Python, whatever you need.
  2. Working directory:
    • Sets up /code as the working directory for subsequent instructions.
  3. Dependency installation:
    • Copies requirements.txt to the working directory.
    • Runs pip install to install the dependencies specified in requirements.txt. --no-cache-dir helps keep the image size down.
  4. Application code:
    • Copies the rest of the application code into the container.
  5. Start command:
    • Specifies the command to run the application using fastapi run app/main.py --port 3000.

This approach is very minimal, getting your FastAPI app up and running quickly inside a container. You can also use uvicorn or nginx when you're used to it. You can read some of the official documentation here: FastAPI

Make sure to add the .dockerignore file to ignore unnecessary files like __pycache__ and any virtual environment. This will speed up the build process and reduce the image size. Here's a good starting point for your .dockerignore:

__pycache__
*.pyc
*.pyo
.venv
venv
dist
build
Enter fullscreen mode Exit fullscreen mode

Deployment

You can deploy this Docker container to any cloud provider that supports Docker. For example, you could use platforms like Heroku, DigitalOcean, or AWS ECS. Because I am the co-founder of Sliplane, I will show you how to deploy it there.

After signing up, you can create a new service by selecting your Github Repository. Then just keep the default settings and click on deploy.

Sliplane Service Create

After deployment, your FastAPI app will be available under a subdomain of sliplane.app, usually it's just your service name.

You can also see the logs of your app, see metrics such as CPU and memory usage, add persistent storage, and much more. Whenever you push to your repository, Sliplane will automatically deploy your app.

If you want to try out Sliplane, the first 2 days are free! Try it out and let me know what you think :)

Deploy FastAPI in 2 Minutes 🚀

Next Steps

Is there anything else you want to know? Do you need help dockerizing your FastAPI app? Do you need help deploying it to a specific platform? Feel free to reach out!
You can find me on X or just comment here on this blog.

Cheers,

Jonas

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (4)

Collapse
 
bettdouglas profile image
Douglas Bett

Great article. Thanks for sharing.
One question though...why not use the slim image, because the base python image is almost 900MB but the slim image is about 150MB...

Collapse
 
nikhilesh002 profile image
Nikhilesh G

Yeah I use slim / alpine images

Collapse
 
code42cate profile image
Jonas Scholz

Good point, I usually use alpine images but missed that for some reason here. Slim is better in most cases, fixed it!

Collapse
 
shayy profile image
Shayan • Edited

Thanks for this! I was going to try FastAPI later next week and this will be really helpful.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay