docker 部署 fastapi

docker 创建网络

docker network create -d bridge docker_public_network

docker 构建镜像

docker build -t  fastapi_demo:v8 .

requirements.txt

fastapi~=0.79.1
pydantic~=1.9.2
PyJWT~=2.4.0
pymongo~=4.2.0
python-multipart
redis~=4.3.4
uvicorn~=0.18.2
xlrd~=2.0.1
xlwt~=1.3.0
elasticsearch~=8.3.3
python-dotenv~=0.20.0
starlette~=0.19.1

Dockerfile

FROM python:3.7.9
RUN echo '开始构建.............'
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
VOLUME [ "/home" ]
WORKDIR /home
COPY requirements.txt requirements.txt
RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --upgrade pip && pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r requirements.txt
RUN echo '构建完成.............'
CMD ["python3"]

docker-compose.yml

version: '3'
services:
    fastapi_demo-v1.0:
        # 指定容器运行的镜像
        image: fastapi_demo:v8
        # 自定义容器名称
        container_name: fastapi_demo-v1.0
        # 将主机的数据卷或着文件挂载到容器里
        volumes:
            - .:/home
            - /etc/hosts:/etc/hosts
        # 启动的默认命令
        command: python3 main.py
        # 容器的端口xx映射到宿主机的端口xx
        ports:
            - "9003:9003"
        # 配置容器连接的网络
        networks:
            - docker_public_network
        # 服务的日志记录配置
        logging:
            driver: "json-file"
            options:
                max-size: "100m" # 单个文件大小为200k
                max-file: "2" # 最多10个文件

networks:
    docker_public_network:
        external: true
docker-compose up 
docker-compose up -d

查看进程
docker-compose ps

你可能感兴趣的:(docker 部署 fastapi)