When I'm sending the request using FastAPI (/AddTasks) I'm getting
ConnectionError: Multiple exceptions: [Errno 111] Connect call failed ('127.0.0.1', 5672), [Errno 99] Cannot assign requested address
I'm running the container using docker run -p 80:80 image_name
Here is how my request looks like
@app.get("/")
async def read_root():
return HTMLResponse(content = "POST /AddTasks<br />GET /GetStats")
async def send_rabbitmq(msg = {}):
connection = await connect("amqp://guest:guest@localhost/")
channel = await connection.channel()
await channel.default_exchange.publish(
Message(json.dumps(msg.dict()).encode("utf-8")),
routing_key = "fastapi_task"
)
await connection.close()
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=400)
@app.post("/AddTasks")
async def add_tasks(
task: Task = Body(
...,
example = {
"taskid": "task1234",
"description": "Example description",
"params": {
"test1": "1234",
"test2": "5678"
}
}
)
):
global successful_tasks
await send_rabbitmq(task)
successful_tasks += 1
return {"message": f"Task {task.taskid} added"}
Dockerfile:
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "80"]
RabbitMQ is turned on my computer using brew services start, I think the issue maybe with docker container connection to the rabbitmq, however I have no clue how should I fix that.