我正在尝试为一个小型python网络项目编写Dockerfile,并且依赖项存在问题。我一直在互联网上进行搜索,它说Librosa库要求libsndfile能够正常工作,所以我尝试使用apt-get install libsndfile1进行安装(我也尝试过libsndfile-dev,...)。但是,它似乎无法解决我的问题。

这是我的Dockerfile的样子:

FROM python:3.6-buster as build

ENV STATIC_URL /static
ENV STATIC_PATH /var/www/app/static

WORKDIR /var/www/

RUN python -m venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .

RUN pip install -r requirements.txt

RUN pip install gunicorn

RUN apt-get update -y && apt-get install -y --no-install-recommends build-essential gcc \
                                        libsndfile1

FROM python:3.6-buster AS run

COPY --from=build /opt/venv /opt/venv

COPY . .

ENV PATH="/opt/venv/bin:$PATH"

RUN gunicorn -b :5000 --access-logfile - --error-logfile - app:app

但是,当我尝试构建并运行它时,发生了此错误:
[2020-04-15 17:30:02 +0000] [7] [INFO] Starting gunicorn 20.0.4
[2020-04-15 17:30:02 +0000] [7] [INFO] Listening at: http://0.0.0.0:5000 (7)
[2020-04-15 17:30:02 +0000] [7] [INFO] Using worker: sync
[2020-04-15 17:30:02 +0000] [10] [INFO] Booting worker with pid: 10
[2020-04-15 17:30:03 +0000] [10] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/opt/venv/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app.py", line 12, in <module>
    from emotion_model.test import load_model, inference_segment
  File "/emotion_model/test.py", line 9, in <module>
    import librosa
  File "/opt/venv/lib/python3.6/site-packages/librosa/__init__.py", line 12, in <module>
    from . import core
  File "/opt/venv/lib/python3.6/site-packages/librosa/core/__init__.py", line 126, in <module>
    from .audio import *  # pylint: disable=wildcard-import
  File "/opt/venv/lib/python3.6/site-packages/librosa/core/audio.py", line 10, in <module>
    import soundfile as sf
  File "/opt/venv/lib/python3.6/site-packages/soundfile.py", line 142, in <module>
    raise OSError('sndfile library not found')
OSError: sndfile library not found
[2020-04-15 17:30:03 +0000] [10] [INFO] Worker exiting (pid: 10)
[2020-04-15 17:30:03 +0000] [7] [INFO] Shutting down: Master
[2020-04-15 17:30:03 +0000] [7] [INFO] Reason: Worker failed to boot.

最佳答案

对于那些来此职位以寻求解决方案的人。我的解决方法是在此部分之后放置libsndfile的安装:

FROM python:3.6-buster AS run

COPY --from=build /opt/venv /opt/venv

COPY . .

ENV PATH="/opt/venv/bin:$PATH"

可能是:
FROM python:3.6-buster AS run

COPY --from=build /opt/venv /opt/venv

COPY . .

ENV PATH="/opt/venv/bin:$PATH"

RUN apt-get update -y && apt-get install -y --no-install-recommends build-essential gcc \
                                        libsndfile1

RUN gunicorn -b :5000 --access-logfile - --error-logfile - app:app

关于python - Librosa在Docker中提出了OSError ('sndfile library not found'),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61235346/

10-13 07:44