0

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.

Montana
  • 37
  • 1
  • 8
  • You should check if a previous run of your application is still listening on the provided IP:port. It appears to be the case since the server cannot run. – zer0 Jul 15 '22 at 21:22
  • Also, can you provide your `Dockerfile`? – zer0 Jul 15 '22 at 21:23
  • `127.0.0.1` in Docker is "this container", not another container or a service running outside Docker. On a MacOS host (which would typically use Homebrew as a package manager) you can use the special host name `host.docker.internal` to reach the non-Docker message queue. – David Maze Jul 15 '22 at 22:06

0 Answers0