Poetry는 TOML파일로 의존성을 관리한다.
이 두 파일을 도커 이미지에 추가해서 빌드하면
requirements.txt로 의존성을 추가해주는 작업을 대체할 수 있다.
requirements로 관리하는 것에 비해
는 장점이 있다.
poetry new <project_name> - 아래로는 <project_name> == sample 이라고 하겠습니다.
cd sample
poetry add fastapi uvicorn
pyproject.toml에 의존성이 아래와같이 나온다.
# pyproject.toml
[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.103.1"
uvicorn = "^0.23.2"
touch Dockerfile
cd sample && touch main.py
main.py에는 아래 내용 작성, 모듈 나누는 건 요구사항에 맞게 하면 된다.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"Hello World"}
Dockerfile
poetry install에서 —no-root를 명시해주는 이유는 does not contain any element 오류 방지를 위함이다.
FROM python:3.10
WORKDIR /sample
RUN pip install poetry
COPY pyproject.toml poetry.lock ./
COPY sample sample
RUN poetry install --no-root
EXPOSE 8000
ENTRYPOINT [ "poetry" ,"run", "uvicorn", "sample.main:app", "--host", "0.0.0.0" ]
컨테이너로 실행하면 도커 컨테이너 런타임의 호스트는 0.0.0.0 이다.
uvicorn 기본 host는 127.0.0.1 (local host)이니까 변경
docker build -t sample .
docker run -d -p 8000:8000 sample
localhost:8000 에 접속해보면 동작을 확인 할 수 있다.
기본 개발환경은 위와같이 셋업하면 된다.