昨天发了一个SayHello FastAPI版本,今天部署上本身的服务器了
体验地址: http://49.232.203.244:9001/message.htmlhtml
前置条件:如下在centos7.5 云服务器实验经过前端
yum -y install git # 安装git curl -sSL https://get.daocloud.io/docker | sh # 安装docker
git clone https://gitee.com/zy7y/sayhellopython
git clone https://github.com/zy7y/sayhellonginx
上面两个命令选一个执行就可了git
cd sayhello
github
在sayhello目录下新建以下Dockerfiledocker
FROM python:3.7 COPY . /app WORKDIR ./app RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ EXPOSE 80 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
简单说下上面内容作了什么事情,不必定正确加了些我的理解shell
FROM python:3.7 # 拉取基础镜像python3.7,本机已有该镜像就会使用该镜像,没有就去远端仓库拉取,速度慢就须要换下源地址,百度便可(这里应该就是拉下镜像后弄成了个容器) COPY . /app # 将当前所在目录下全部文件 复制到 容器里面 /app 目录下 WORKDIR ./app # 指定工做目录,个人理解是后面执行的命令 都至关于在这个目录下执行了,根目录的形式吧 RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ # 这步是在容器里面执行 pip 安装依赖 EXPOSE 80 # 将容器中80 端口开放 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"] # 容器运行时将执行 uvicorn main:app --host 0.0.0.0 --port 80 启动服务
docker build -t sayhello .
会自动执行dockerfile里面的CMD命令后端
docker run -d --name sayhello-fastapi -p 8000:80 sayhello
IP:8000/message
,获得以下页面先确认message.html中的
baseURL
是否是后端服务的IP地址(127.0.0.1 不行)centos
cd sayhello/static/
FROM nginx:1.15.2-alpine COPY . /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
docker build -t sayhello-front .
docker run -d --name sayhello-front-9000 -p 9001:80 sayhello-front
感谢资料提供者/做者