文章首发及后续更新:https://mwhls.top/4074.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!
打开 Settings – Docker Engine
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
}
}
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
},
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
docker pull python:3.8.13-slim
docker run --name PDRS -ti python:3.8.13-slim /bin/bash
exit
docker container ls -a
docker exec -u 0 -it 21ce830a7d27 /bin/sh
docker ps --size
具体流程见: vscode远程连接docker容器
终端操作:使用 Ctrl+Shift+~ 打开终端。
注:这一步我做的时候忘写了,这是后来补上的,如果出错,建议参考这里:关于服务器通过Python搭建API服务
安装:pip install fastapi uvicorn
main.py – fastapi 代码
# main.py, copy from http://www.zhangkexuan.cn/2020/11/01/about-python-api/
from fastapi import FastAPI
app = FastAPI()
@app.get(‘/’)
def index():
return {‘message’: ‘你已经正确创建 FastApi 服务!’}
执行:uvicorn main:app -- reload
报错:ERROR: Error loading ASGI app. Could not import module "main".
os.system('uvicorn main:app --reload')
来启动,但因为 vscode 的工作路径不对,所以报错如上。os.chdir('./code_path')
,其中,./code_path
为 main.py 所在路径。测试:启动后,vscode 会弹窗提示已经映射 docker 端口到宿主机,可通过 http://127.0.0.1:8000 打开,终端输出如下:
INFO: Will watch for changes in these directories: ['/root/code']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [10502] using statreload
INFO: Started server process [10504]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:33858 - "GET / HTTP/1.1" 200 OK