Docker下使用gunicorn部署Django,gunicorn启动失败

error日志

[2021-09-28 20:41:48 +0800] [1766] [INFO] Worker exiting (pid: 1766)
[2021-09-28 12:41:48 +0000] [1791] [INFO] Booting worker with pid: 1791
[2021-09-28 20:41:48 +0800] [1765] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
    super().init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 142, in init_process
    self.run()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 195, in run
    self.notify()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 75, in notify
    self.tmp.notify()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/workertmp.py", line 46, in notify
    os.fchmod(self._tmp.fileno(), self.spinner)
FileNotFoundError: [Errno 2] No such file or directory

使用配置文件和直接命令行执行均出现此错误

gunicorn.config.py

# -*- coding: utf-8 -*-
import multiprocessing
from pathlib import Path

BASE_DIR = Path(__file__).resolve(strict=True).parent

debug = False

# ============================================================
# gunicorn要切换到的目的工作目录
# ============================================================

chdir = str(BASE_DIR)

# ============================================================
# server socket相关
# ============================================================

# 指定绑定的ip和端口
bind = "0.0.0.0:8080"

# 服务器中排队等待的最大连接数,建议值64-2048,超过2048时client连接会得到一个error
backlog = 2048

# ============================================================
# 调试相关
# ============================================================

# 当代码有修改时,自动重启workers,适用于开发环境
reload = False

# 以守护进程形式来运行Gunicorn进程,其实就是将这个服务放到后台去运行
daemon = False

# ============================================================
# worker进程相关
# ============================================================

# 用于处理工作的进程数
workers = multiprocessing.cpu_count() * 2 + 1

# worker进程的工作方式,有sync、eventlet、gevent、tornado、gthread, 默认是sync,
# django使用gevent容易造成阻塞, 使用gthread的方式好一些
worker_class = 'gthread'

# 指定每个工作进程开启的线程数
threads = multiprocessing.cpu_count() * 2

# 访问超时时间
timeout = 30

# 接收到restart信号后,worker可以在graceful_timeout时间内,继续处理完当前requests
graceful_timeout = 60

# server端保持连接时间
keepalive = 30

# ============================================================
# 日志相关
# ============================================================

"""日志文件格式,其每个选项的含义如下:
h          remote address
l          '-'
u          currently '-', may be user name in future releases
t          date of the request
r          status line (e.g. ``GET / HTTP/1.1``)
s          status
b          response length or '-'
f          referer
a          user agent
T          request time in seconds
D          request time in microseconds
L          request time in decimal seconds
p          process ID
"""
access_log_format = '%(t)s %(h)s "%(r)s" %(s)s %(b)s "%(f)s" "%(L)s"'

errorlog = '/tmp/gunicorn.error.log' #发生错误时log的路径

accesslog = '/tmp/gunicorn.access.log' #正常时的log路径

# 错误日志输出等级,访问日志的输出等级无法设置
loglevel = "info"

# ============================================================
# 进程名相关
# ============================================================

# 设置进程名称,默认是gunicorn
proc_name = 'gunicorn_room'


进入docker容器直接命令行执行还是同样错误

gunicorn drf_admin.wsgi -b 0.0.0.0:8080

环境

Django==2.2.20
gunicorn==20.1.0

No such file or directory是找不到文件,你看看docker的文件怎样挂载


python - Gunicorn on Docker Errors out on Mac - Stack Overflow I'm getting the following error only on Mac. It works fine on a Linux host: Output: FileNotFoundError: [Errno 2] No such file or directory server_1 | [2019-10-22 22:48:39 +0000] [397] [INFO] W... https://stackoverflow.com/questions/58513420/gunicorn-on-docker-errors-out-on-mac

已解决,使用gunicorn不能挂载tmp目录,把日志放到别的目录就行了!
太他妈坑了