Docker上部署fastApi

Docker上部署fastApi

1. 项目结构

在这里插入图片描述

2. 配置文件

project目录下新建requirements.txtDockerfile来进行配置

requirements.txt

用如下代码生成requirement内容

if __name__ == '__main__':
    popen = os.popen('pip list')
    with open('requirements.txt', 'w') as f:
        for index, i in enumerate(popen):
            if index > 1:
                split = i.split(' ')
                k = split[0].strip()
                v = split[-1].strip()
                f.write(k + "==" + v + '\n')
anyio==3.5.0
appnope==0.1.2
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asgiref==3.5.2
attrs==21.4.0
Babel==2.9.1
backcall==0.2.0
beautifulsoup4==4.11.1
bleach==4.1.0
Bottleneck==1.3.4
brotlipy==0.7.0
certifi==2022.5.18.1
cffi==1.15.0
charset-normalizer==2.0.4
click==8.1.3
cloudpickle==2.1.0
cryptography==37.0.1
cycler==0.11.0
dask==2022.2.0
debugpy==1.5.1
decorator==5.1.1
defusedxml==0.7.1
entrypoints==0.4
et-xmlfile==1.1.0
fastapi==0.78.0
fastjsonschema==2.15.1
fonttools==4.25.0
fsspec==2022.5.0
gensim==4.2.0
greenlet==1.1.1
h11==0.13.0
hanlp-downloader==0.0.25
harvesttext==0.8.1.6
idna==3.3
importlib-metadata==4.11.3
importlib-resources==5.2.0
ipykernel==6.9.1
ipython==7.31.1
ipython-genutils==0.2.0
ipywidgets==7.7.0
isodate==0.6.1
jedi==0.18.1
Jinja2==3.0.3
joblib==1.1.0
JPype1==0.7.0
json5==0.9.6
jsonschema==4.4.0
jupyter-client==7.2.2
jupyter-core==4.10.0
jupyter-server==1.17.1
jupyterlab==3.3.2
jupyterlab-pygments==0.1.2
jupyterlab-server==2.12.0
jupyterlab-widgets==1.1.0
kiwisolver==1.4.2
locket==1.0.0
MarkupSafe==2.1.1
matplotlib==3.5.1
matplotlib-inline==0.1.2
mistune==0.8.4
mkl-fft==1.3.1
mkl-random==1.2.2
mkl-service==2.4.0
munkres==1.1.4
nbclassic==0.3.5
nbclient==0.5.13
nbconvert==6.4.4
nbformat==5.3.0
nest-asyncio==1.5.5
networkx==2.6.3
nltk==3.7
notebook==6.4.11
numexpr==2.8.1
numpy==1.21.5
opencc-python-reimplemented==0.1.6
openpyxl==3.0.9
packaging==21.3
pandas==1.3.5
pandocfilters==1.5.0
parso==0.8.3
partd==1.2.0
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.0.1
pip==21.2.2
prometheus-client==0.13.1
prompt-toolkit==3.0.20
psutil==5.9.1
ptyprocess==0.7.0
pycparser==2.21
pydantic==1.9.1
Pygments==2.11.2
pyhanlp==0.1.84
PyMySQL==1.0.2
pyOpenSSL==22.0.0
pyparsing==3.0.4
pypinyin==0.46.0
pyrsistent==0.18.0
PySocks==1.7.1
python-dateutil==2.8.2
python-louvain==0.16
pytz==2022.1
PyYAML==6.0
pyzmq==22.3.0
rdflib==6.1.1
regex==2022.6.2
requests==2.27.1
scikit-learn==1.0.2
scipy==1.7.3
seaborn==0.11.2
Send2Trash==1.8.0
setuptools==61.2.0
six==1.16.0
smart-open==6.0.0
sniffio==1.2.0
some-package==0.1
soupsieve==2.3.1
SQLAlchemy==1.4.32
starlette==0.19.1
swifter==1.1.3
terminado==0.13.1
testpath==0.5.0
threadpoolctl==3.1.0
toolz==0.11.2
tornado==6.1
tqdm==4.64.0
traitlets==5.1.1
typing_extensions==4.1.1
urllib3==1.26.9
uvicorn==0.17.6
w3lib==1.22.0
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==0.58.0
wheel==0.37.1
widgetsnbextension==3.6.0
zipp==3.8.0

Dockerfile

FROM python:3.7

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

# 禁用缓存并批量安装包(后面的链接是利用豆瓣源安装,速度会加快)
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt -i https://pypi.douban.com/simple/

RUN pip uninstall -y jieba

COPY ./ /code/app
RUN mv /code/app/jieba /usr/local/lib/python3.7/site-packages/

EXPOSE 8085

CMD ["uvicorn", "app.main:app", "--reload", "--host", "0.0.0.0", "--port", "8085"]


3. 构建Docker镜像

在terminal进入到项目文件夹,输入:

docker build -t 项目名称 .

4.启动容器

docker run -d --name dockertestpy --network=mytest -p 8000:8085 testpy

PS:由于本项目中要用到另外一个容器的mysql,因此要对network进行设置,配置一个bridge模式的网络,才可访问另外容器中的mysql。

5.常用docker操作及注意事项

  1. docker rmi -f testpy 强制删除镜像
  2. docker exec -it testpy /bin/bash 访问容器文件系统
  3. container start/stop 容器启动
  4. 项目代码中尽量写绝对路径
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值