1

I am trying to deploy my FastAPI app to a docker container (P.S. I am a beginner in using docker). I used the below code. Without the DB, this code is working fine in the docker container. However, when I try to try to connect to DB, the path "URL:port/" is showing connection refused. I used the exact steps as mentioned in uvicorn-gunicorn-fastapi-docker

from fastapi import FastAPI
from pymongo import MongoClient


app = FastAPI()

client = MongoClient('localhost', port=27017)
db = client.safetypointer


@app.get("/")
def read_root():
    all_users = db.users.find({}, {"_id":0})
    return {"Hello": "World" , "allUsers": all_users}
robert patrik
  • 89
  • 5
  • 16

1 Answers1

1

Local host should likely be replaced with the name of your mongo container. I would also recommend taking a look at docker-composer as it can make deploying two connected containers easier.

Here is a link to an example with flask it would be similar with fastapi: awesome-compose