简体   繁体   English

Dockerfile:无法打开文件'./main.py':[Errno 2]没有这样的文件或目录

[英]Dockerfile: can't open file './main.py': [Errno 2] No such file or directory

I get this error: python: can't open file '/src/main.py': [Errno 2] No such file or directory when I try to run a container with an image that was build with the following docker file:我收到此错误: python: can't open file '/src/main.py': [Errno 2] No such file or directory

FROM python:3.9-slim AS compile

RUN python -m venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH"

WORKDIR /my-app

COPY requirements.txt .
RUN pip install -r requirements.txt

ADD src/ ./src
RUN pip install .

FROM python:3.9-slim AS build

COPY --from=compile/opt/venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH"

CMD ["python", "/src/main.py"]

I tried this as well and it still gives me the same type of error about not finding the main.py: i tried./src/main/py, /src/main.py, /src/main.py, ./main.py.我也试过这个,但它仍然给我同样类型的关于找不到 main.py 的错误:我试过./src/main/py、/src/main.py、/src/main.py、./main .py。 I tried everything, I'm starting to suspect the error is elsewhere我尝试了一切,我开始怀疑错误在其他地方

/src/main.py is absolute path from the system root. /src/main.py是系统根目录的绝对路径。

In order to be relative to you current directory use ./src/main.py为了与您当前目录相关,请使用./src/main.py

I would simplify your docker file as below:我会简化你的 docker 文件如下:

    # base image
    FROM amazonlinux:1
    
    # Set the working directory
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    COPY . /app
    
    # Install requirements
    RUN pip install -r requirements.txt
    
    # Define environment variable
    ENV PYTHONPATH "${PYTHONPATH}:/app"

    # Run main.py when the container launches
    ENTRYPOINT ["python", "-u", "src/main.py"]

The problem is you have multistaged build (2x FROM) and you only add them to the first stage.问题是您有多阶段构建(2x FROM)并且您只将它们添加到第一阶段。


FROM python:3.9-slim AS compile

[..]

ADD src/ ./src
ADD setup.py .
RUN pip install .

FROM python:3.9-slim AS build

[..]

You can fix this with a second COPY --from= statement in the 2. stage.您可以在 2. 阶段使用第二个COPY --from=语句来解决此问题。 Additionally your CMD is wrong.此外,您的 CMD 是错误的。 Either give it the fullpath or start a relative path with a.要么给它完整路径,要么用 a 开始一个相对路径。 the dir-/filename ( /my-app/src/main.py , ./src/main.py , src/main.py ).目录/文件名( /my-app/src/main.py./src/main.pysrc/main.py )。

FROM python:3.9-slim AS compile

RUN python -m venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH"

WORKDIR /my-app

COPY requirements.txt .
RUN pip install -r requirements.txt

ADD src/ ./src
ADD setup.py .
RUN pip install .

FROM python:3.9-slim AS build

COPY --from=compile/opt/venv /opt/venv

COPY --from=compile/my-app /my-app                  # ADDED

WORKDIR /my-app                                     # ADDED

ENV PATH="/opt/venv/bin:$PATH"

CMD ["python", "/my-app/src/main.py"]                      # FIXED

Lastly, you're only setting the workdir in the stage you throw away, but that's only relevant if you don't give the cmd the full path or you need a specific workdir.最后,您只是在丢弃的阶段设置工作目录,但这仅在您没有为 cmd 提供完整路径或者您需要特定的工作目录时才有意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法打开文件 'main.py': [Errno 2] 没有那个文件或目录 - Can't open file 'main.py': [Errno 2] No such file or directory python:无法打开文件'main.py':[Errno 2]没有这样的文件或目录-docker - python: can't open file 'main.py': [Errno 2] No such file or directory - docker Dockerfile-python:无法打开文件'/usr/app/client.py':[Errno 2]没有这样的文件或目录 - Dockerfile - python: can't open file '/usr/app/client.py': [Errno 2] No such file or directory python3: 无法打开文件 'model_main_tf2.py': [Errno 2] 没有那个文件或目录 - python3: can't open file 'model_main_tf2.py': [Errno 2] No such file or directory 无法打开文件'file.py':[Errno 2] 没有这样的文件或目录 - Can't open file 'file.py': [Errno 2] No such file or directory 无法打开文件 'manage.py': [Errno 2] 没有那个文件或目录 - Can't open file 'manage.py': [Errno 2] No such file or directory 无法打开文件'menu.py':[Errno 2]没有这样的文件或目录 - Can't open file 'menu.py': [Errno 2] No such file or directory 无法打开文件 'app.py': [Errno 2] 没有那个文件或目录 - can't open file 'app.py': [Errno 2] No such file or directory 无法打开文件'.manage.py':[Errno 2] 没有这样的文件或目录 - can't open file '.manage.py': [Errno 2] No such file or directory 无法打开文件“audioAnalysis.py”:[Errno 2] 没有这样的文件或目录 - Can't open file 'audioAnalysis.py': [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM