You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Podman构建容器时无法找到指定路径下的app.py问题排查

解决Podman构建容器时COPY app /报错“no such file or directory”的问题

问题重现

执行构建命令:

podman build -t default-service-fastpai:latest .

出现报错:

STEP 6/8: COPY app /
Error: building at STEP "COPY app /": checking on sources under "Documents/Projects/Pred_default/app": copier: stat: "/app": no such file or directory

目录结构:

└── 📁Pred_default
    └── 📁app
        └── Containerfile
        └── app.py
        └── app_test.py
        └── lgb_credit_model.pkl
        └── lgb_es_model.pkl
        └── measure_response.py
        └── requirements.txt
    └── .gitignore
    └── LICENSE.md
    └── README.md
    └── credit_predict.ipynb
    └── requirements.txt

Containerfile内容:

FROM python:3.10-slim

RUN mkdir /app
# Set the working directory
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt requirements.txt

# Install the required packages
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Copy the application code into the container
COPY app /
# COPY ["lgb_credit_model.pkl", "lgb_es_model.pkl" "app.py", "./"] 

# Expose the app port
EXPOSE 80

# Run command
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]

错误原因

从报错路径Documents/Projects/Pred_default/app可以看出,你是在Pred_default/app目录下执行的构建命令,此时构建上下文是app目录本身。而Containerfile里的COPY app /是要求Podman从构建上下文(当前app目录)里找名为app的子目录,但当前目录里只有app.py等文件,没有app子目录,所以报错。

另外,RUN mkdir /app是多余的,WORKDIR指令会自动创建不存在的目录。

解决方案

提供两种可行方案,选其一即可:

方案1:在Pred_default根目录执行构建(推荐)

这种方式更符合常规项目结构,方便管理配置文件。

  1. 调整Containerfile位置或指定构建路径:
    • 要么把app目录里的Containerfile移动到Pred_default根目录;
    • 要么执行构建时指定Containerfile路径:
      podman build -t default-service-fastpai:latest -f app/Containerfile .
      
  2. 修改Containerfile中的COPY指令:
    因为此时构建上下文是Pred_default根目录,需要从app子目录读取文件:
    FROM python:3.10-slim
    
    # WORKDIR会自动创建/app目录,无需手动mkdir
    WORKDIR /app
    
    # 复制app目录下的requirements.txt到容器当前目录
    COPY app/requirements.txt requirements.txt
    
    RUN pip install --upgrade pip
    RUN pip install -r requirements.txt
    
    # 复制app目录下所有内容到容器当前目录(/app)
    COPY app/ ./
    
    EXPOSE 80
    
    CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]
    

方案2:继续在Pred_default/app目录执行构建

如果不想改变目录结构,直接修改Containerfile的COPY指令:

FROM python:3.10-slim

WORKDIR /app

# 当前构建上下文是app目录,直接复制当前目录的requirements.txt
COPY requirements.txt requirements.txt

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

# 复制当前目录所有内容到容器的/app目录(即WORKDIR指定的路径)
COPY . ./

EXPOSE 80

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]

然后重新执行构建命令即可。

内容的提问来源于stack exchange,提问作者spoolito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 15:02:39