使用VSCode+Docker开发Python应用遇文件找不到错误求助
问题:Docker运行Python应用时出现FileNotFoundError
运行容器时出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/app/spotify-2-mp3\main.py'
我的Dockerfile内容:
FROM python # Keeps Python from generating .pyc files in the container ENV PYTHONDONTWRITEBYTECODE=1 # Turns off buffering for easier container logging ENV PYTHONUNBUFFERED=1 WORKDIR /app # Install pip requirements COPY . . RUN python -m pip install -r requirements.txt # Creates a non-root user with an explicit UID and adds permission to access the /app folder RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app USER appuser # During debugging, this entry point will be overridden. CMD ["python", "spotify-2-mp3/main.py"]
目录结构:

解决方案:
从目录结构可见,main.py直接位于项目根目录下,并非spotify-2-mp3子文件夹内。Dockerfile中的COPY . .已将项目根目录的所有文件复制到容器的/app目录,因此/app下直接存在main.py,无需额外的子目录路径。
修改Dockerfile里的CMD指令,将路径改为直接指向main.py:
CMD ["python", "main.py"]
重新构建镜像并运行容器即可解决该错误。
内容的提问来源于stack exchange,提问作者Davide Bianchi
相关产品推荐
相关产品推荐

