Python/FastAPI

FastAPI Docker로 배포하기

thxxyj 2024. 3. 15. 12:15
728x90

File Tree

 ┣ app
 ┃ ┣ common
 ┃ ┃ ┗ config.py
 ┃ ┣ database
 ┃ ┃ ┣ conn.py
 ┃ ┃ ┗ schema.py
 ┃ ┗ routes
 ┃ ┃ ┗ xxx_api.py
 ┣ docker_setting
 ┃ ┣ Dockerfile
 ┃ ┣ fastapi.yaml
 ┃ ┗ requirements.txt
 ┗ main.py

 

 

main.py

from fastapi import FastAPI

# custom module
from app.routes import xxxx_api
from app.database.conn import db

app = FastAPI()
app.include_router(xxxx_api.router)
db.init_app(app)


if __name__ == '__main__':
    import uvicorn

    uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True)

 

Dockerfile

FROM python:3.9

WORKDIR /code

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

COPY ./app /code/app

COPY ./main.py /code/main.py

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

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

 

 

docker-compse.yaml

version: "3"

services:
    fastapi-app:
        image: fastapi:1.0
        container_name: fastapi-app
        privileged: true
        ports:
            - 8000:8000
        volumes:
            - ./app:/code/app
        environment: 
            - TZ=Asia/Seoul
        command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload

 * uvicorn에서 파일 변경이 일어나면 재시작을 위해 --reload

728x90

'Python > FastAPI' 카테고리의 다른 글

FastAPI parameters: path, query, request body  (0) 2024.03.05
FastAPI install  (0) 2024.03.04