Fastapi: [QUESTION] Get access to fastapi app outside of a docker container

Created on 25 Oct 2019  Â·  3Comments  Â·  Source: tiangolo/fastapi

Description

I have successfully created a docker container for a tensorflow model deployment using fastAPI. However, after exposing the 8000 port and trying to connect to this application, I fail to make the connection.

Here's how the app looks like:

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}


if __name__ == "__main__":
    uvicorn.run(app, port=8000)

How can I connect to this application running in the container? Which url should I use since 0.0.0.0:8000 is currently not working?

Additional context
Here's the dockerfile

# Tensforlow 2 official image with Python 3.6 as base
FROM tensorflow/tensorflow:2.0.0-py3

# Make working directories
RUN  mkdir -p /home/project
WORKDIR /home/project

# Upgrade pip with no cache
RUN pip install --no-cache-dir -U pip

# Copy application requirements file to the created working directory
COPY requirements.txt .

# Install application dependencies from the requirements file
RUN pip install -r requirements.txt

# Copy every file in the source folder to the created working directory
COPY  . .

# Run the python application
CMD ["python", "app/main.py"]

Here's a screenshot of the ports but I get no luck when I launch the app with 0.0.0.0:8000 in the browser after running the container with docker run -it -p 8000:8000 myimage

image

question

Most helpful comment

@euri10 none taken. In any case, it was fixed by specifying the host in the uvicorn app run as suggested by this article. Issue will be closed now.

All 3 comments

no offense but in no way it's a FastAPI question,
that said it seems your container runs app/main.py, so either it contains a
__main__ method that runs uvicorn as described in the docs, either change
the CMD to run uvicorn.
if that's already the case I don't get it

On Fri, Oct 25, 2019 at 1:31 PM Bernard Brenyah notifications@github.com
wrote:

Description

I have successfully created a docker container for a tensorflow model
deployment using fastAPI. However, after exposing the 8000 port and
trying to connect to this application, I fail to make the connection.

Here's how the app looks like:

import uvicornfrom fastapi import FastAPI

app = FastAPI()

@app.get("/")def read_root():
return {"Hello": "World"}

@app.get("/items/{item_id}")def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

if __name__ == "__main__":
uvicorn.run(app, port=8000)

How can I connect to this application running in the container? Which url
should I use since 0.0.0.0:8000 is currently not working?

Additional context
Here's the dockerfile

Tensforlow 2 official image with Python 3.6 as baseFROM tensorflow/tensorflow:2.0.0-py3

Make working directoriesRUN mkdir -p /home/projectWORKDIR /home/project

Upgrade pip with no cacheRUN pip install --no-cache-dir -U pip

Copy application requirements file to the created working directoryCOPY requirements.txt .

Install application dependencies from the requirements fileRUN pip install -r requirements.txt

Copy every file in the source folder to the created working directoryCOPY . .

Run the python applicationCMD ["python", "app/main.py"]

Here's a screenshot of the ports but I get no luck when I launch the app
with 0.0.0.0:8000 in the browser after running the container with docker
run -it -p 8000:8000 myimage

[image: image]
https://user-images.githubusercontent.com/29863388/67567241-d4f42280-f729-11e9-8653-54e03b766ac8.png

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/655?email_source=notifications&email_token=AAINSPUYLOOECIE2TO6HMMDQQLKJPA5CNFSM4JFCFQL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HULSWYA,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAINSPUSQZYO5G6UAGWHW6TQQLKJPANCNFSM4JFCFQLQ
.

--
benoit barthelet
http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE

@euri10 none taken. In any case, it was fixed by specifying the host in the uvicorn app run as suggested by this article. Issue will be closed now.

Thanks for the help here @euri10 ! :clap: :bow:

Thanks for reporting back and closing the issue @PyDataBlog :+1:

Was this page helpful?
0 / 5 - 0 ratings