【问题标题】:When I run code in Docker I get a Django error [Errno 2]. When running locally everything works. Why?当我在 Docker 中运行代码时,我收到一个 Django 错误 [Errno 2]。在本地运行时,一切正常。为什么?
【发布时间】:2021-05-12 04:30:13
【问题描述】:

我不知道发生了什么。由 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"]

'''

【问题讨论】:

    标签: python-3.x django docker ffmpeg ffprobe


    【解决方案1】:

    丢失的文件实际上在容器中吗?你可以在容器运行时检查这样的例子:docker exec &lt;your-container-name&gt; ls /app/data/&lt;blacked-out-part-from-your-screenshot&gt;

    我问的原因是因为您的 Dockerfile 中有 COPY . /app,但没有提及卷或绑定挂载或任何关于您如何运行容器的内容。如果这就是将数据移动到容器中的全部内容,那么该副本会在映像构建时发生一次,并且不会与源目录同步。

    通常您不会将数据复制到 image 中,而是使用 volumesbind mountscontainer 共享数据以获取持久性和应用代码。

    如果我对丢失文件的猜测是正确的,并且您确实需要它在图像中,而不是在容器中,请在没有缓存的情况下重建图像。例如:docker build --no-cache &lt;rest-of-the-command-here&gt;。一般来说,这是一个不好的做法。而且你的 Dockerfile 也不是很好layered。如果可以,请使用卷/挂载。

    【讨论】:

    • 亲爱的@napovi,问题是在 docker 运行时添加了文件。 docker 工作时网站打开;允许我添加一个文件。但是当我想使用这个文件时,就会出现给定的错误。如果我对“mediainfo 函数”发表评论,则整个脚本效果很好。感谢您的链接,我将改进我的代码。祝你有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2021-05-04
    • 1970-01-01
    • 2018-01-21
    • 2016-06-30
    相关资源
    最近更新 更多