Search code examples
pythondockerpython-poetry

setting up docker with python 3.9 + poetry getting 'does not contain any element'


I am trying to setup docker for local dev and i keep getting "does not contain any element" when i try to use poetry with docker. Originally I used the requirement.txt, however i would prefer it to have poetry because where i work we have a pyserver which we need to pull some of the .wh from.

So i am trying to keep it basic. This my structure and some basic code

enter image description here

-------------------main.py-------------

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

----------------------uvicorn_conf.py------

from uvicorn.workers import UvicornWorker
bind = "0.0.0.0:8000"
workers = 4
worker_class = UvicornWorker

-----------Dockerfile-------

FROM python:3.9

# Set the working directory to /app
WORKDIR /app

# Copy the entire project directory to the container
COPY . .

# Install Poetry and project dependencies
RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev

# Start the server
CMD ["poetry", "run", "start"]

---------pyproject.toml------

[tool.poetry]
name = "my-app"
version = "0.1.0"
description = ""
authors = ["py_dev <test@testing.com>"]
readme = "README.md"
packages = [{include = "my_app"}]

[tool.poetry.dependencies]
python = "^3.9"
fastapi = "^0.95.1"
uvicorn = "^0.21.1"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
start = "uvicorn my_app.main:app --config uvicorn_conf.py"

And every time i run build, I get this error. enter image description here I am not sure how to fix this. Any advice or direction on how to get it working would be brilliant. Thank you in advance.


Solution

  • There's no "my_app" directory in the screenshot you posted, so you should update "my_app" to "app" in your pyproject.toml:

    packages = [{include = "app"}]
    

    See https://python-poetry.org/docs/pyproject/#packages for more info.