django - 在 docker build 中运行 "/usr/local/bin/gunicorn"说 "stat/usr/local/bin/gunicorn: no such file or directory"

标签 django python-3.x docker docker-compose gunicorn

从顶层 map 目录,我可以安装 gunicorn 扩展...

(venv) localhost:maps davea$ pip3 install gunicorn
Collecting gunicorn
  Downloading gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 1.2 MB/s 
Requirement already satisfied: setuptools>=3.0 in ./web/venv/lib/python3.7/site-packages (from gunicorn) (45.1.0)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4

下面是我的 docker-compose.yml 文件
version: '3'

services:
  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    environment:
      DEBUG: 'true'
    command: /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000

  apache:
    restart: always
    build: ./apache/
    ports:
      - "80:80"
    #volumes:
    #  - web-static:/www/static
    links:
      - web:web

  mysql:
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: 'maps_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'chicommons'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3406:3406"
    volumes:
      - my-db:/var/lib/mysql

volumes:
  my-db:

然后我有 web/Dockerfile 如下...
FROM python:3.7-slim

RUN apt-get update && apt-get install

RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc \
    && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip
RUN mkdir -p /app/

WORKDIR /app/

RUN pip3 freeze > requirements.txt
COPY requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt

COPY . /app/

但是,当我构建/启动我的 docker 实例时,我被告知它找不到我的“gunicorn”命令......
(venv) localhost:maps davea$ docker-compose up
Starting maps_web_1 ... 
Starting maps_web_1 ... error

ERROR: for maps_web_1  Cannot start service web: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/usr/local/bin/gunicorn\": stat /usr/local/bin/gunicorn: no such file or directory": unknown

ERROR: for web  Cannot start service web: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/usr/local/bin/gunicorn\": stat /usr/local/bin/gunicorn: no such file or directory": unknown
ERROR: Encountered errors while bringing up the project.

最佳答案

您的 Docker 容器是一个完全隔离的环境。您在主机上安装的任何内容在容器内均不可见;容器内发生的任何事情都无法在主机上访问。有一些方法可以跨越这个边界(使用 docker run -v 绑定(bind)挂载),但在 docker build 期间这是不可能的。阶段。

在本例中,您的本地源代码树有一个 requirements.txt列出创建容器时需要安装的包的文件。 (RUN pip freeze 行无效;COPY 在它从本地源代码树复制后的行上。)将依赖项添加到 requirements.txt 就足够了。文件

gunicorn

在您的开发环境中,您可以重新运行pip install -r requirements.txt更新安装在虚拟环境中的软件包。当你重新运行 docker build ,在 requirements.txt 中有这一行文件将导致它在构建包时被安装。

您可以稍微清理一下 Dockerfile。生成的 Dockerfile 将是具有 C 依赖项的 Python 包的一个非常典型的文件:

# Start from a totally clean environment with Python installed,
# but no non-system libraries and nothing from your host system.
FROM python:3.7-slim

# Install C dependencies.
# It's important to do apt-get update and install in the
# same command.  It's more efficient to only do it once.
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      gcc \
      libmariadb-dev \
      libmariadb-dev-compat

# Update pip
RUN python -m pip install --upgrade pip

# Create the application directory and point there
# (WORKDIR will implicitly create it)
WORKDIR /app/

# Install all of the Python dependencies.  These are
# listed, one to a line, in the requirements.txt file,
# possibly with version constraints.  Having this as
# a separate block allows Docker to not repeat it if
# only your application code changes.
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

# Copy in the rest of the application.
COPY . .

# Specify what port your application uses, and the
# default command to use when launching the container.
EXPOSE 8000
CMD /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000

关于django - 在 docker build 中运行 "/usr/local/bin/gunicorn"说 "stat/usr/local/bin/gunicorn: no such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60179769/

相关文章:

python - Django 表单不显示,只有按钮(新)

python - Heroku 上的 Django 不允许我创建 super 用户

python - 无法在 Komodo 中创建和运行新的 python 文件

python - EOF 错误 python 3?

docker - 仅在服务尚未启动时启动服务 - docker-compose

mysql - Django 的 unique_together 功能的性能成本是多少

django - 如何以时区感知方式在 Django 表单中使用 DateField?

python - 根据函数中参数的数量返回不同的值

docker - 两台主机上的Docker和Weave无法互相ping通

docker - 在 Windows Docker 构建中从 Chocolatey.org 诊断下载超时