项目结构:
pycharm结构:
拷贝到xftp时的结构
入口文件:main.py
from fastapi import fastapi app = fastapi() @app.get("/index", name="欢迎kb88凯时d88尊龙官网手机app官网登录首页") async def index(): return {"msg": "欢迎来到sayhello!"}
项目依赖:
使用命令可以直接在根目录生成requirements.txt文件
pip freeze > requirements.txt
requirements.txt 内容如下
aiofiles==0.5.0 aniso8601==7.0.0 async-exit-stack==1.0.1 async-generator==1.10 certifi==2021.5.30 charset-normalizer==2.0.3 click==7.1.2 colorama==0.4.4 dnspython==2.1.0 email-validator==1.1.3 fastapi==0.67.0 graphene==2.1.9 graphql-core==2.3.2 graphql-relay==2.0.1 h11==0.12.0 idna==3.2 itsdangerous==1.1.0 jinja2==2.11.3 markupsafe==2.0.1 orjson==3.6.0 pip==21.2.1 promise==2.3 pydantic==1.8.2 python-dotenv==0.19.0 python-multipart==0.0.5 pyyaml==5.4.1 requests==2.26.0 rx==1.6.1 setuptools==57.4.0 six==1.16.0 starlette==0.14.2 typing-extensions==3.10.0.0 ujson==4.0.2 urllib3==1.26.6 uvicorn==0.13.4 watchgod==0.7 websockets==8.1
dockerfile:
from python:3.9.5 copy . /app run ln -sf /usr/share/zoneinfo/asia/shanghai /etc/localtime run echo 'asia/shanghai' >/etc/timezone workdir ./app run pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ expose 8080 cmd ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
xshell进入项目文件列表路径
构建镜像文件(命令结尾的小点“.” 别忘记了)
docker build -t 项目名称 .
构建成功后运行容器即可:
docker run -d --name 新取的容器名称 -p 8000:80 刚刚构建的镜像名称