Uvicorn 不会用 CTRL+C 退出
我有一个使用 uvicorn 的 Python FastAPI 应用程序。我把它打包在 docker 容器中。当我使用如下命令运行应用程序(在 Windows 10 计算机上的 Power Shell 中)时:docker run -p 8080:8080 my-image-name
我收到以下 uvicorn 启动文本
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
:按 CTRL+C
终止应用程序,没有任何反应。我总是最终不得不关闭终端才能让它停止。
这是因为它在 docker 容器中运行吗?
我知道我可以在分离模式 (-d
) 下运行容器,但有时我想在容器日志发生时查看它们。
如何终止 docker 映像和 uvicorn 进程并保持终端仍在运行?
I have a Python FastAPI app that is using uvicorn. I have it packaged in docker container. When I run the app (in Power Shell on my Windows 10 machine) with a command like this: docker run -p 8080:8080 my-image-name
I get the following uvicorn startup text:
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
When I press CTRL+C
to kill the app, nothing happens. I always end up having to close the terminal to get it to stop.
Is this due to the fact that it's running in a docker container?
I know I can run the container in detached mode (-d
), but sometimes I want to watch the container logs as they happen.
How can I kill the docker image and uvicorn process AND keep the terminal still running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
--tty
和--interactice
运行容器,以便转发您的键盘按下操作。此外,您应该确保您的 入口点,如果有的话。
顺便说一句,关闭终端通常不会停止容器。在执行 docker ps 时,您应该仍然会看到它在运行。
You need to run the container with
--tty
and--interactice
so that your keyboard presses are forwarded.Additionally, you should make sure that you don't use shell syntax for your entrypoint, if you have one.
On a side note, closing the terminal, doesn't stop the container, normally. You should still see it running when doing
docker ps
.