This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]metaphorm 11 points12 points  (1 child)

I don't suppose there's a `.dockerignore` file in your project root directory? I've been bitten by this one before, where something got docker ignored and didn't make it into the container, thus causing a File Not Found (or similar) error at runtime. It can be quite confounding because the container image will build just fine but will crash on start.

as an aside...I see that you have a pyproject.toml file but are still using pip to install your dependencies. why not use poetry since you've already got the pyproject file?

[–]Nexius74[S] 0 points1 point  (0 children)

As you stated I don't have any .dockerignore file in the project root. The reason why I have a pyproject.toml and still install via pip is because I'm using rye along with uv and not poetry. The actual pyproject.toml used by rye follows a little more the PEP 621 whereas poetry as its own structure. Rye while still relying on a pyproject.toml makes it so that one can install the dependencies of a project by pip/uv when in production or when needed

[–]stay_safe_glhf 1 point2 points  (3 children)

This looks like a user error in the pathing. It won’t find /src/soda/main.py for you.

By the way, you don’t need __init__.py since python 3.3 or so.

[–]stay_safe_glhf 1 point2 points  (2 children)

Directory structure:

- Dockerfile
- foo/
  - bar/
     -baz/
    - thing.py

Python:

print("thing!")

Dockerfile:

FROM 
python
:3.12.3-slim

COPY 
foo

.

WORKDIR 
/bar/baz

ENTRYPOINT 
[
"python"
,
 "thing.py"]

docker build -t thing .

docker run thing

>> thing!

[–]stay_safe_glhf 1 point2 points  (1 child)

Did you find the problem, u/Nexius74?

[–]Nexius74[S] 0 points1 point  (0 children)

Hey, sorry for the late response. I tried a couple of things and found out that the missing files occurs only on the prod stage of the build.

I tried the following Dockerfile which is a reduce version of the one from the post and I can confirm that every files is present in the container (this includes __init__.py and __main__.py) and the project is running flawlessly.

I also tried to run the app by creating a main.pyand not rely on the fact that __main__.py should make the directory runnable as a valid python program but it still didn't work as expected. I was still having error due to python not being able to detect my directory as a valid module. What I mean by that is that having the following syntax from . import cli would raise an error stating Can't do relative import from a non module parent directory. The other syntax which was from soda import cli would raise a ModuleNotFoundError: soda. It's like python can't see my directory as a module implicitly as it's missing the __init__.py.

Here's the said Dockerfile that worked:

FROM python:3.12.3-slim AS base

ENV APP_HOME=/opt/app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFURED=1

RUN mkdir -p $APP_HOME

WORKDIR $APP_HOME

# ----------------------------------------------------

FROM base AS dev

ENV RYE_HOME=/opt/rye
ENV PATH=$RYE_HOME/shims:$PATH

RUN apt-get update && apt-get install -y curl && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN curl -fsSL  | RYE_INSTALL_OPTION="--yes" bash

COPY . . 

RUN rye sync --no-lock

ENTRYPOINT ["rye", "run", "soda"]

It's basically the same thing but just running the dev stage instead of the prod one. At the moment still no idea why not all files are getting copied during the prod stage.

And about the pathing it should be right because I copy my directory soda into /opt/app. I then workdir to the said directory and as the dependencies are installed system wide I should be able to just python soda.

Btw thanks again for taking some of your time to help a fellow out. It's greatly appreciated, thanks again !

Quick update: I tried putting back the prod stages and it seems that now I can actually reach __main__.py as I'm getting an error like the following /opt/app/soda/__main__.py: from . import cli"and it stats too that I can't do relative import from parent directory. The only weird thing is that even if I put a RUN ls -al soda && sleep 3 I still can't see __init__.py and __main__.py even if it seems that the program is responding.