FastAPI: Docker部署(19)

用Docker去部署应用已是目前最流行和通用的方式。现在把之前的项目部署到Docker上去。

准备Dockerfile

项目根目录下创建Dockerfile

1
2
3
4
5
6
7
8
9
10
11
FROM python:3.7

WORKDIR /fastapi

COPY ./requirements.txt /fastapi/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /fastapi/requirements.txt

COPY . /fastapi/

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

Build Docker Image

去到项目根目录下,执行下面命令

docker build -t myfastapi:user .

验证

1
PS E:\fastapi> docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
myfastapi    user      0790ae3493a3   14 seconds ago   972MB

Start Docker Container

执行下面命令,部署应用到Container

docker run -d --name fastapiservice -p 80:80 myfastapi:user

验证

1
2
3
PS E:\fastapi> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b550b5a7ebb myfastapi:user "uvicorn main:app --…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp fastapiservice

验证部署成功

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!