我不知道发生了什么事。由 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)挂载或任何关于您如何运行容器的内容。如果这就是将数据移动到容器中的全部内容,那么该副本会在镜像构建时发生一次,并且不会与源目录同步。
通常您不会将数据复制到 图片 ,但与 共享数据容器 使用 volumes或 bind 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/