In this tutorial, we will go through the steps to set up uv with a FastAPI application in a Docker container. uv is a high-performance, event-driven web server for Unix and Python. FastAPI is a modern web framework for building APIs with Python. By combining the two, we can create powerful and efficient web applications.
To get started, you will need the following tools installed on your system:
- Docker
- Python
- FastAPI
- uv
Here are the steps to set up uv with a FastAPI application in a Docker container:
Step 1: Create a FastAPI application
First, create a new directory for your FastAPI application:
$ mkdir fastapi-app
$ cd fastapi-app
Next, install FastAPI and uv using pip:
$ pip install fastapi uvicorn
Now, create a file named main.py
in the fastapi-app
directory and add the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def read_root():
return {"Hello": "World"}
This code creates a simple FastAPI application with a single endpoint that returns a JSON response with the message "Hello World".
Step 2: Create a Dockerfile
Next, create a Dockerfile in the fastapi-app
directory to build a Docker image for your application:
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
This Dockerfile specifies a Python 3.9 base image, sets the working directory to /app
, copies the requirements.txt
file, installs the dependencies, copies the FastAPI application code, and runs uvicorn to start the server on port 8000.
Step 3: Create a requirements.txt file
Create a file named requirements.txt
in the fastapi-app
directory and add the following dependencies:
fastapi
uvicorn
This file lists the Python packages required for your FastAPI application.
Step 4: Build the Docker image
Now, build the Docker image for your FastAPI application by running the following command in the fastapi-app
directory:
$ docker build -t fastapi-app .
This command will build a Docker image with the tag fastapi-app
.
Step 5: Run the Docker container
Finally, run the Docker container for your FastAPI application using the following command:
$ docker run -d -p 8000:8000 fastapi-app
This command will start the Docker container in detached mode, mapping port 8000 on the host to port 8000 in the container.
That’s it! You now have a FastAPI application running in a Docker container with uv as the web server. You can access your application by visiting http://localhost:8000
in your web browser.
In summary, uv is a powerful web server that can be easily integrated with FastAPI applications in Docker containers. By following the steps outlined in this tutorial, you can create efficient and scalable web applications with ease.
Original uv video: https://www.youtube.com/watch?v=igWlYl3asKw
Thanks for video
Excellent video as always! Just a question… In the Dockerfile, is it possible to pass the uv binary as local file instead of passing it from a remote registry? Just thinking of security.
The quality of the videos is excellent as always, a video with Django integrated with this wonderful tool would be nice.
Waiting for deployment
It would be awesome if we can move .venv to custom location within docker.
This is my first subscribed channel where I activate notifications. You are killing it bugbytes. You are more than ready to start creating full courses.
Is that approach the fastest? I have local development in mind e.g. django with celery etc where adding a dependency forces the rebuild of the container which can take a while with pip. Seems that uv cannot „play out“ its advantage of package caching in such a setup.
thank you for your tutorial! would love to see uv and django together
Make the same video for Django please
Great as always 🌹
Best Django and other python web frameworks. Thank you for taking your time to make such quality videos.
Can you make a video of using Django and Apache Kafka? I will be much appreciated
Really nice video
i am facing this error
i followed each step done by you.
c => ERROR [stage-0 5/5] RUN uv sync –frozen –no-cache 0.5s
——
> [stage-0 5/5] RUN uv sync –frozen –no-cache:
0.433 error: Project virtual environment directory `/app/.venv` cannot be used because because it is not a valid Python environment (no Python executable was found)
——
Dockerfile:12
——————–
10 |
11 | # Install the application dependencies.
12 | >>> RUN uv sync –frozen –no-cache
13 |
14 | # Run the application.
——————–
ERROR: failed to solve: process "/bin/sh -c uv sync –frozen –no-cache" did not complete successfully: exit code: 2
Thank for adding docker with uv
•
I hope in the next video make it
Full Django Ninja with celery and docker on Render hosting
Including the celery management when updating code of celery without interrupting the consumed tasks or reset the celery docker image.
My best regards
awesome video again
Is docker setup same for django?
I was waiting for this video with docker thanks. Hope we will see in the near future videos regarding the testing and security phases
good me need uv – Docker django
Best Django channel! As well as other useful Python videos.