python-3.x - 当我在 Docker 中运行代码时,我收到一个 Django 错误 [Errno 2]。在本地运行时,一切正常。为什么?

标签 python-3.x django docker ffmpeg ffprobe

我不知道发生了什么事。由 Django 运行的脚本可以正常工作,但不能通过 Docker 和 Django。返回一个错误:
Pic Errno 2 No such file or directory
下面是有错误的函数代码和 Dockerfile 的代码。
'''

def mediainfo(filepath):
    
原始代码:
    prober = get_prober_name()
    command_args = [
        "-v", "quiet",
        "-show_format",
        "-show_streams",
        filepath
    ]

    command = [prober, '-of', 'old'] + command_args

修改后的代码:
    command = f"ffprobe -v error -show_format -show_streams -select_streams v:0 {filepath}"

其余功能:
    res = Popen(command, stdout=PIPE)
    output = res.communicate()[0].decode("utf-8")

    if res.returncode != 0:
        output = Popen(command, stdout=PIPE).communicate()[0].decode("utf-8")

    rgx = re.compile(r"(?:(?P<inner_dict>.*?):)?(?P<key>.*?)\=(?P<value>.*?)$")
    info = {}

    if sys.platform == 'win32':
        output = output.replace("\r", "")

    for line in output.split("\n"):
        # print(line)
        mobj = rgx.match(line)

        if mobj:
            # print(mobj.groups())
            inner_dict, key, value = mobj.groups()

            if inner_dict:
                try:
                    info[inner_dict]
                except KeyError:
                    info[inner_dict] = {}
                info[inner_dict][key] = value
            else:
                info[key] = value

    return info
'''
Dockerfile 的代码
'''
FROM python:3.7 as base

EXPOSE 80

WORKDIR /app
COPY . /app


ENV PYTHONDONTWRITEBYTECODE=1

ENV PYTHONUNBUFFERED=1

RUN pip install --upgrade pip
RUN echo 'deb http://deb.debian.org/debian buster-backports main contrib non-free' >> /etc/apt/sources.list
RUN apt-get update

RUN apt-get -y install ffmpeg
RUN apt-get update

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

FROM base as prod

ENTRYPOINT ["python","manage.py","runserver","0.0.0.0:80"]
'''

最佳答案

丢失的文件实际上是否在容器中?例如,当容器运行时,您可以像这样检查:docker exec <your-container-name> ls /app/data/<blacked-out-part-from-your-screenshot>我问的原因是因为你有 COPY . /app在您的 Dockerfile 中,但没有提及卷或绑定(bind)挂载或任何关于您如何运行容器的内容。如果这就是将数据移动到容器中的全部内容,那么该副本会在镜像构建时发生一次,并且不会与源目录同步。
通常您不会将数据复制到 图片 ,但与 共享数据容器 使用 volumesbind mounts用于持久性和应用程序代码。
如果我对丢失文件的猜测是正确的,并且您确实需要它在图像中,而不是在容器中,请在没有缓存的情况下重建图像。例如:docker build --no-cache <rest-of-the-command-here> .一般来说,这是一个不好的做法。而且你的 Dockerfile 不太好layered任何一个。如果可以的话,使用卷/挂载。

关于python-3.x - 当我在 Docker 中运行代码时,我收到一个 Django 错误 [Errno 2]。在本地运行时,一切正常。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66103805/

相关文章:

python 使用 append 优化嵌套的 for 循环

python-3.x - Altair 离线渲染

python - 返回具有多个键的字典中的最大值

python - Django 外键模型不保存引用

django - 编程错误 : column cannot be cast automatically to type double precision

python - 如何在django中使用加密密码登录

docker - 从docker-compose运行的Nginx返回 “host not found in upstream”

python - `del` 语句和自由变量

git - Docker Jenkins 和 Sonar 设置

docker - 如何使用 Docker 防止 EventStore 中的 url 重写?