How to configure Nginx to serve static files from a Docker container with FastAPI
Configured Nginx to serve static files from a FastAPI container.
Introduction
In this guide, you will learn how to configure Nginx to serve static files from a FastAPI container using Docker. We will show you how to define a volume in Docker Compose and configure the appropriate permissions so that Nginx can access your files. Let's get started!
Step 1: Project Structure
Make sure your project has the following directory structure:
/your-project │ ├── Dockerfile ├── docker-compose.yml └── img └── product └── your_image.jpeg
In your Fast Api application
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
Step 2: Create the Dockerfile
Create a Dockerfile for your FastAPI application. Here's a basic example:
# Use a Python base image
FROM python:3.9
# Set the working directory
WORKDIR /app
# Copy the required files
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
# Expose port 5000
EXPOSE 5000
# Command to run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]
Step 3: Configure Docker Compose
Create a docker-compose.yml file that defines your FastAPI and Nginx service. Here's an example:
services:
fastapi:
build: .
ports:
- "8000:5000"
volumes:
- .{path_server}:{path_container} # Mount the volume for the static files
Step 4: Configure Nginx
Create an nginx.conf file to configure Nginx and serve the static files:
server {
listen 80;
server_name servername;
location location_to_folder_share {
alias path_absolute_to_folder_share or path_server absolute ; # /home/$User/projects/folder_shared
try_files $uri $uri/ =404; # Return a 404 if the file does not exist
add_header X-Content-Type-Options nosniff;
}
}
Step 5: Adjust Permissions
Make sure Nginx has permission to access the static files folder. Run the following commands on your server:
sudo chmod -R 755 /home/$User/projects/folder_shared
sudo chown -R www-data:www-data /home/$User/projects/folder_shared
Also check that all the directories above have execute permissions:
sudo chmod +x /home
sudo chmod +x /home/$User
sudo chmod +x /home/$User/projects
Step 6: Start Containers
Once you have everything set up, start your Docker containers with the following command:
docker-compose up --build
Step 7: Check Access to Static Files
Try accessing one of your static files via the URL:
- https://host_url/location_to_folder_share/your_image.jpeg
You can view the image without errors if everything is set up correctly.
Conclusion
You have successfully configured Nginx to serve static files from a FastAPI container. Using Docker and Docker Compose, you can easily manage your services and ensure your application is production-ready. Good luck with your project!
Additional Notes:
- Make sure to customize the paths and file names to suit your project structure.
- This post includes basic steps and can be expanded depending on the complexity of your application and the configurations you want to implement.
- This setup was tested on a reverse proxy where local nginx is used on the server which uses stream for the docker containers on the same server.
- Also remember to configure the static folder in your FastAPI app
