首页 > 解决方案 > Gunicorn 无法连接到 Alpine 上的 sock 文件

问题描述

关于这个主题有很多问题,但没有一个对我有帮助。

我正在尝试将 Gunicorn 连接到,/tmp/gunicorn.sock但我一直在获取operation not permitted. 我的gunicorn.conf.py样子:

import multiprocessing

# bind = '127.0.0.1:5000'
bind = 'unix:/tmp/gunicorn.sock'

backlog = 2048
preload_app = True
max_requests = 2048
max_requests_jitter = 128

workers = multiprocessing.cpu_count() * 2 + 1
worker_connections = 1000
timeout = 60
keepalive = 2

errorlog = '-'
loglevel = 'debug'
accesslog = '-'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'


def when_ready(server):
    open('/tmp/app-initialized', 'w').close()

我的日志是:

[2018-08-03 02:34:40 +0000] [116] [INFO] Starting gunicorn 19.9.0
[2018-08-03 02:34:40 +0000] [116] [DEBUG] connection to /tmp/gunicorn.sock failed: [Errno 1] Operation not permitted
[2018-08-03 02:34:40 +0000] [116] [ERROR] Retrying in 1 second.
[2018-08-03 02:34:41 +0000] [116] [DEBUG] connection to /tmp/gunicorn.sock failed: [Errno 1] Operation not permitted
[2018-08-03 02:34:41 +0000] [116] [ERROR] Retrying in 1 second.
[2018-08-03 02:34:42 +0000] [116] [DEBUG] connection to /tmp/gunicorn.sock failed: [Errno 1] Operation not permitted
[2018-08-03 02:34:42 +0000] [116] [ERROR] Retrying in 1 second.
[2018-08-03 02:34:43 +0000] [116] [DEBUG] connection to /tmp/gunicorn.sock failed: [Errno 1] Operation not permitted
[2018-08-03 02:34:43 +0000] [116] [ERROR] Retrying in 1 second.
[2018-08-03 02:34:44 +0000] [116] [DEBUG] connection to /tmp/gunicorn.sock failed: [Errno 1] Operation not permitted
[2018-08-03 02:34:44 +0000] [116] [ERROR] Retrying in 1 second.
[2018-08-03 02:34:45 +0000] [116] [ERROR] Can't connect to /tmp/gunicorn.sock

这看起来像是用户的权限错误,但这不应该是一个问题,因为gunicorn运行为root

/opt/app # ps aux | grep gunicorn
  123 root      0:00 grep gunicorn

我还尝试创建一个用户和一个组,并通过执行更改文件夹addgroup -S appgroup && adduser -S appuser -G appgroup的权限(因为未创建文件)。/tmp/chown appuser:appgroup /tmp/gunicorn.sock

操作系统中的内容正在执行root,但我仍然收到此错误。我应该如何才能通过gunicorn.sock文件运行它?

更新

My `Dockerfile`:

FROM python:3.6.6-alpine3.8

# Update, install the required packages and clean downloaded package
RUN apk update && \
    apk upgrade && \
    apk add postgresql-dev nginx supervisor && \
    rm -rf /var/cache/apk/*

# Copy files
...

# setup all the configfiles
COPY config/nginx.conf /etc/nginx/nginx.conf
COPY config/nginx-app.conf /etc/nginx/sites-available/default
COPY config/supervisor-app.conf /etc/supervisor/conf.d/

# Install requirements
...

EXPOSE 8113

CMD ["supervisord", "-n", "-c", "/opt/app/config/supervisor-app.conf"]

标签: pythongunicorn

解决方案


感谢https://github.com/benoitc/gunicorn/issues/1849的人们。

要即时创建套接字,您可能必须将它们放入/run/ie中,bind='unix:/run/gunicron.sock'然后您可以使用 Nginx 作为反向代理并通过给定的 sock 文件提供服务。

但是为什么/run/

根据维基百科

运行时变量数据:关于自上次启动以来正在运行的系统的信息,例如,当前登录的用户和正在运行的守护进程。此目录下的文件必须在启动过程开始时删除或截断;但在提供此目录作为临时文件系统 (tmpfs) 的系统上,这不是必需的。

有关更多信息,请参阅https://unix.stackexchange.com/questions/13972/what-is-this-new-run-filesystem


推荐阅读