Hello, I encountered a strange situation while running the Sanic application (in version 22.9.1) in Docker. I have prepared a sample code (python code is based on the examples that I found in the documentation) to reproduce the issue.
Dockerfile
FROM python:3.11.0-buster
COPY app app
COPY run_app.py run_app.py
COPY requirements.txt requirements.txt
RUN ["python", "-m", "pip", "install", "-r", "requirements.txt"]
EXPOSE 8000
CMD ["python", "run_app.py"]
requirements.txt
aiofiles==22.1.0
httptools==0.5.0
multidict==6.0.2
sanic==22.9.1
sanic-routing==22.8.0
ujson==5.6.0
uvloop==0.17.0
websockets==10.4
app/__init __.py
from sanic import Request, Sanic, json
def attach_endpoints(app: Sanic):
@app.get("/")
async def handler(request: Request):
return json({"app_name": request.app.name})
def create_app() -> Sanic:
app = Sanic('testApp')
attach_endpoints(app)
return app
run_app.py
from sanic import Sanic
from sanic.worker.loader import AppLoader
from app import create_app
if __name__ == "__main__":
loader = AppLoader(factory=create_app)
app = loader.load()
app.prepare(port=8000)
Sanic.serve(primary=app, app_loader=loader)
While running created container, I am getting a Sanic Exception
sanic.exceptions.SanicException: Sanic app name "testApp" already in use.
I have noticed that adding dev=True to the prepare method suppresses this issue
app.prepare(port=8000, dev=True)
I do not see this exception when I run the app in the terminal on my machine
(System Version: macOS 12.6 Kernel Version: Darwin 21.6.0 Python Version: 3.11.0).
On the other hand when I modify the code in run_app.py to as follows
from app import create_app
if __name__ == "__main__":
app = create_app()
app.run(host='0.0.0.0', port=8000)
I am able to run the app in Docker, but I am getting Sanic Exception when I execute it in terminal.
sanic.exceptions.SanicException: Sanic app name 'testApp' not found.
App instantiation must occur outside if __name__ == '__main__' block or by using an AppLoader.
Is this expected behavior? How should I run Sanic applications in Docker?
Thank you in advance.