1

If I run from the terminal uvicorn main:app --reload everything works.

When putting it in a my_script.sh file with:

#!/bin/bash
exec uvicorn main:app --reload

then nothing happens. Why?

I found this: Uvicorn/FastAPI executable, but it seems unanswered.

Chris
  • 18,724
  • 6
  • 46
  • 80
supersick
  • 261
  • 2
  • 14

2 Answers2

1

You can instead add a main method (more specifically, if __name__ == "__main__":) inside your existing Python script (i.e., main.py in your case) or a new Python script, where you run the uvicorn server, allowing you to set parameters, such as host, port, reload, workers, etc. (as shown here), and simply execute that Python script from inside your bash script. Have a look here for all the available options.

import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

Edit: Since you mentioned below that you "have the script defined in the entrypoint section of docker-compose", make sure you give the exact path to the bash script, as well as it has permissions to execute. Have a look at the questions here and here. I would also suggest to take a look at the relevant FastAPI documentation.

Chris
  • 18,724
  • 6
  • 46
  • 80
  • Hi there, i tried this as well with no result :( I have the script defined in the entrypoint section of docker-compose. When i start it it just exits with errcode 0. – supersick Feb 24 '22 at 16:23
0

you can remove the exes from bash script. might be it is a cause of your failure.

Shubham
  • 33
  • 5