Unable to connect FastAPI to the Docker Container from the host browser on Mac M1

  • Docker version: 20.10.12
  • Docker desktop version: 4.4.2

Problem
I am not able to connect to the Container of my Fastapi app that is being hosted at 0.0.0.0:5000 from the host browser on Mac with M1 chip. When I run: “Docker run -p 5000:5000” and click on the link to the local host, the browser returns a 403 error. The Fastapi app does work in the python terminal.

My fastapi code:

if __name__ == "__main__":
    # Use this for debugging purposes only
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=5000)  # , log_level="debug")

Docker file

FROM python:3.10.3-buster

RUN apt-get update --fix-missing&& \
    apt-get upgrade -y  

ARG MY_ENV=dev

ENV PORT=5000
EXPOSE ${PORT}


ENV MY_ENV=${MY_ENV} \
    PYTHONFAULTHANDLER=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONIOENCODING=UTF-8 \
    POETRY_HOME="/opt/poetry" \
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    POETRY_NO_INTERACTION=1 \
    PYSETUP_PATH="/opt/pysetup" \
    VENV_PATH="/opt/pysetup/.venv"

RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
    cd /usr/local/bin && \
    ln -s /opt/poetry/bin/poetry && \
    poetry config virtualenvs.create false


COPY ./pyproject.toml ./
RUN poetry install

COPY . /app
COPY . /app/src

WORKDIR /app
CMD ["python", "src/main.py", "--host", "0.0.0.0" ]

Dockerfile image also works on GCP cloud run so I figured there must be something wrong with my docker on mac settings?

Any suggestions will be much appreciated.
Thanks