I have a FastAPI docker running on my debian 9 server with --net=host running on port 8000. I would like to make the FastAPI service open to the public, but do not know really how.
I can do curl localost:8000/test and will see the return but when I do curl domain:8000/test it will respond connection refused.
I have opened the port sudo iptables -A INPUT -p tcp --dport 8000 -j ACCEPT but still not seeing anything.
Do I have to configure uvicorn somehow to resolve the domain internally, similar to what apache does with virtual hosts?
netstat -tulpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
sudo ss -tulpn
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 127.0.0.1:8000 *:* users:(("gunicorn",pid=44998,fd=5),("gunicorn",pid=44983,fd=5))
or should this rather return 0.0.0.0:8000 because I can ssh into the host no problem?
Dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-alpine3.14
COPY ./app /app
USER root
COPY ./requirements.txt /requirements.txt
RUN apk add py3-pip && \
pip install --upgrade pip
RUN apk add --no-cache mariadb-connector-c-dev ;\
apk add --no-cache --virtual .build-deps \
build-base \
mariadb-dev ;\
pip install -r /requirements.txt;\
apk del .build-deps
ENV HOSTNAME fastapi
ENV PYTHONPATH '/:/app'
COPY ./gunicorn_conf.py /gunicorn_conf.py
since you can access FastAPI from your host machine, that means docker port mapping works just fine. what is left is to add the hostname to the origins in the Router.py module something like this \
origins = [
"http://127.0.0.1:8000",
"http://localhost:8000",
"http://yourhostgoeshere:yourport",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
remember to change HTTP to HTTPS if you don't have a proxy.