Docker packages your app with everything it needs, so it runs the same anywhere. FastAPI lets you build fast, efficient APIs with Python.
Together, they make development simple and deployment reliable. FastAPI keeps things fast and easy, while Docker ensures consistency across environments.
In this guide, you'll learn how to containerize a FastAPI application with Docker.
Prerequisites
Before starting, make sure you have:
- Python 3.8 or newer
- Docker Desktop or Engine installed
- Docker Compose also installed
- Basic understanding of FastAPI concepts
Setting up the project structure
Let's create a straightforward FastAPI project demonstrating key features while being Docker-friendly.
First, create a new directory for your project:
Set up a virtual environment to isolate your dependencies:
Activate the virtual environment:
Install FastAPI and Uvicorn (an ASGI server required by FastAPI):
Create a requirements file to track dependencies:
Now, create a simple main.py file as your application entry point:
This FastAPI application includes:
- A root endpoint returning a welcome message
- A health check endpoint for container monitoring
- A POST endpoint demonstrating data validation with Pydantic models
- Configuration to listen on all interfaces (needed for Docker)
Let's test the application before containerizing it:
Visit http://127.0.0.1:8000/ in your browser to see the welcome message:
You can also check out the automatic API documentation at http://127.0.0.1:8000/docs - one of FastAPI's great features:
After confirming it works, stop the server with CTRL+ C.
Creating a Docker container for FastAPI
Now let's containerize our FastAPI application to make it portable and consistent across environments.
Create a Dockerfile in your project root:
Add the following configuration:
This Dockerfile:
- Uses a lightweight Python 3.13.3 base image
- Sets up a working directory for our application
- Installs Python dependencies first (for better caching)
- Copies our application code
- Exposes port
8000for incoming connections - Starts the application with Uvicorn (or alternatively with the FastAPI CLI)
Create a .dockerignore file to exclude unnecessary files:
In your .dockerignore file, add the following content:
Next, build your Docker image by running:
This command creates an image called fastapi-app using the current folder as the build context.
Once the image is built, start a container with:
This launches the container in the background (-d) and connects port 8000 on your machine to port 8000 inside the container.
Now open your browser and go to http://localhost:8000/ to make sure your FastAPI app is up and running inside the container:
With that, your containerized FastAPI app is live and ready to go.
Working with container logs
Logs are essential for monitoring and debugging your FastAPI app. Docker captures all output from your application, so you can easily check what's happening.
To view logs from your running container:
These messages confirm that Uvicorn has started and is handling requests on port 8000.
To follow the logs in real time (great for development and troubleshooting):
Visit a few endpoints in your browser to see new log entries appear as they happen.
To view only the latest log lines:
And to see logs from a specific time:
When you're done with the container, stop and remove it:
Or, if you prefer to have the container clean itself up automatically when it stops, use the --rm flag:
This helps keep your system tidy by removing the container as soon as it exits.
Using Docker Compose for development
While basic Docker commands are fine for simple apps, Docker Compose is better for managing more complex development setups. It lets you define and run multi-container applications using a single configuration file, making managing things easier.
First, stop any container you started earlier:
Then, create a docker-compose.yml file in the root of your project:
Add the following configuration:
This setup comes with several advantages:
- Live code reloading applies changes to
main.pyinstantly without rebuilding - Environment variables make it easy to manage development-specific settings
- Health checks automatically monitor your app’s status
- Restart policy helps your container recover if it crashes
- Simplified commands make it easier to manage the whole environment
Start the development environment with:
You'll see output similar to:
The most powerful feature of this setup is modifying code without container restarts. Try adding a new endpoint to main.py:
Save the file and visit http://localhost:8000/version in your browser to see the changes take effect immediately:
The logs will show Uvicorn detecting the change:
To run Docker Compose in the background:
View logs from all services:
Or follow logs in real-time:
When you're done, shut everything down with:
This workflow brings together Docker’s reliability and the quick feedback loop needed for development, making it a great fit for FastAPI projects of any scale.
Final thoughts
Using FastAPI with Docker and Docker Compose gives you a solid foundation for building and running web APIs. It simplifies development, ensures consistency across environments, and scales easily as your project grows.
For more details and best practices, visit the official docs: