Here's an example of how you can create a simple FastAPI application with Celery for handling file uploads and processing, along with a Dockerfile and docker-compose.yml file.
Project Structure:
project/
app/
main.py
tasks.py
requirements.txt
Dockerfile
docker-compose.yml
app/main.py:
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from celery import Celery
from pydantic import BaseModel
import os
app = FastAPI()
celery = Celery('tasks', broker='amqp://guest@rabbitmq', backend='redis://redis')
class FileUpload(BaseModel):
filename: str
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
filename = file.filename
file_path = os.path.join("/app/uploads", filename)
with open(file_path, "wb") as f:
f.write(file.file.read())
celery.send_task('tasks.process_file', args=[filename])
return JSONResponse(content={"message": "File uploaded successfully"}, status_code=201)
app/tasks.py:
from celery import Celery
import os
celery = Celery('tasks', broker='amqp://guest@rabbitmq', backend='redis://redis')
@celery.task
def process_file(filename):
file_path = os.path.join("/app/uploads", filename)
# Process the file here
print(f"Processing file: {filename}")
# Save the processed file
processed_file_path = os.path.join("/app/processed", filename)
with open(processed_file_path, "w") as f:
f.write("Processed file content")
return filename
requirements.txt:
fastapi
celery
redis
rabbitmq
Dockerfile:
FROM python:3.9-slim
# Set working directory to /app
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Expose port 8000 for the FastAPI application
EXPOSE 8000
# Run command to start the FastAPI application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
docker-compose.yml:
version: '3'
services:
fastapi:
build: .
ports:
- "8000:8000"
depends_on:
- rabbitmq
- redis
volumes:
- ./uploads:/app/uploads
- ./processed:/app/processed
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
redis:
image: redis:alpine
ports:
- "6379:6379"
celery:
build: .
command: celery -A tasks worker --loglevel=info
depends_on:
- rabbitmq
- redis
volumes:
- ./uploads:/app/uploads
- ./processed:/app/processed
To run the application, create the uploads and processed directories in the project root, then run the following command:
docker-compose up --build
This will start the FastAPI application, RabbitMQ, Redis, and Celery worker. You can upload files to the FastAPI application using a tool like curl:
curl -X POST -F "file=@example.txt" http://localhost:8000/upload
The file will be processed by the Celery worker and saved to the processed directory.
Note that the volumes directive in the docker-compose.yml file is used to persist data even when the container is restarted. The uploads and processed directories are mounted as volumes, so any files uploaded or processed will be saved to the host machine and will not be lost when the container is restarted.