您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页Docker部署FastAPI(详细)

Docker部署FastAPI(详细)

来源:爱站旅游

Docker部署FastAPI


如果是全新的Ubuntu系统,请先更新一下apt,但是更新速度慢需要加速一下
gedit /etc/apt/sources.list

然后在文件里复制粘贴一下

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse

好了,现在可以开始更新了

sudo apt update

如果要顺带更新一下已安装的包

sudo apt upgrade

现在,就是在Ubuntu上安装Docker
sudo apt install docker.io

添加加速源,不然拉取镜像走不动(image镜像在docker官方社区中,速度慢)

tee /etc/docker/daemon.json <<- 'EOF'
{
"registry-mirrors": ["https://5xcgs6ii.mirror.aliyuncs.com"]
}
EOF

装完之后,建议重启一下系统,然后将服务启动开。

sudo systemctl start docker

环境准备好了,还有Docker镜像需要pull
docker pull daocloud.io/library/centos:7

这个Docker镜像是从daocloud上拉取下来的。如果需要查看,也有链接。


现在进入到项目的同级下,创建dockerfile文件
# from Images tiangolo/uvicorn-gunicorn-fastapi:python3.7

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
MAINTAINER Auther YourAccount = YourGithubLink

# Adjust the time
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# Copy files to target file.
COPY ./app /app

# Install library
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ crc16
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ aliyun-python-sdk-core

Adjust the time这个是我写过的一个方案,也就是更改Linux系统的时间分区。

COPY ./app /app将你项目文件夹下的所有文件拷贝到/app中,而/app文件夹本身就存在于镜像中。

RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ crc16是我的项目需要crc16库来对数据进行校验和。

RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ aliyun-python-sdk-core是阿里云PythonSDK

接下来就是制作镜像了
docker build -t ProjectName .
记得写上镜像名!

当镜像制作好了之后,我们可以根据镜像来拉起一个容器

docker run -itd --name NewProjectName -p 80:80 ProjectName

外部端口可以改变,毕竟80端口一般都是Nginx组件在占用着。而内部端口,就不需要更改了。

ProjectName就是刚刚我们制作的镜像名,而NewProjectName是对容器命名,别搞混淆了。


访问Swagger文档路径localhost/docs
访问另一个文档路径localhost/redoc

如果还有什么问题,可以在文章下边留言,博主尽能力之内给大家解答一下。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务