docker uwsgi container 和本地 nginx 无法连通,返回 502

2019-08-24 09:57:46 +08:00
 lbfeng

本来是想 nginx 和 uwsgi 各自放一个 container. 通过 volume 映射 unix socket.但目前 uwsgi container 和本地的 nginx 都跑不通。。。同样的 uwsgi.ini 如果在本地直接运行则没有任何问题。

nginx 一直是 61 connection refused

[error] 12033#0: *1 connect() to unix:/tmp/uwsgi.sock failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "localhost"

socket 在本地的映射(container volume 映射和本地直接运行 uwsgi 完全一样)

srw-rw-r-- 1 username wheel 0B Aug 23 21:43 uwsgi.sock

uwsgi.ini

[uwsgi]
module = app:app
strict = true

master = true
processes = 4

socket = /tmp/uwsgi.sock
chmod-socket = 664
vacuum = true

enable-threads = false
single-interpreter = true
need-app = true
die-on-term = true
max-requests = 1000
max-worker-lifetime = 3600
reload-on-rss = 2048
worker-reload-mercy = 60

app . py

from flask import Flask

def create_app():
    app = Flask(__name__)

    @app.route('/')
    def hello():
        return '<h1>Hello There!</h1>'

    return app

app = create_app()

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Dockerfile

FROM python:3.7-slim

RUN apt-get update && apt-get -y install python3-dev build-essential \
libpcre3 libpcre3-dev

COPY . /src
WORKDIR /src

RUN pip install flask uwsgi

ENTRYPOINT ["./run.sh"]

run . sh

#!/usr/bin/env bash

uwsgi --ini uwsgi.ini

docker-compose

version: "3.5"
services:
  uwsgi:
    build: app/
    restart: on-failure
    volumes:
      - /tmp:/tmp/

webserver.conf

server {
    listen 80;
    server_name localhost;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }
}

1409 次点击
所在节点    问与答
4 条回复
lhx2008
2019-08-24 10:00:04 +08:00
通过 volume 共享 socket ?这个想法一般人可想不到。
lhx2008
2019-08-24 10:02:08 +08:00
可惜 socket 应该只是记录了一些内存里面的信息,并不是用来传递数据的载体
lbfeng
2019-08-24 10:08:08 +08:00
@lhx2008 原文在这:blog . myhro . info/2017/01/benchmarking-ip-and-unix-domain-sockets-for-real。

unix socket 比 tcp socket 更高效。

补充一下:
我刚才到 uwsgi 的 container 里看了下。

uwsgi.sock 在 container 里是这样的。
srw-rw-r-- 1 root root 0 Aug 24 02:00 uwsgi.sock

会不会和 user, group 有关??
julyclyde
2019-08-24 22:56:41 +08:00
这就是你们“啥都喜欢用 docker 解决”引发的问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/594676

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX