具有一个nginx和一个uwsgi容器的Docker,该容器共享一个UNIX套接字

具有一个nginx和一个uwsgi容器的Docker,该容器共享一个UNIX套接字,nginx,flask,docker,uwsgi,docker-compose,Nginx,Flask,Docker,Uwsgi,Docker Compose,首先,对不起,这是一堵文字墙 我正试图让我的Flask应用程序以Docker的心态“每个容器一个服务”启动并运行,所以我真的希望在一个容器中运行Nginx,在一个容器中运行uWSGI,这样它们是独立的,并且如果我想的话,很容易更新 这是我的相关文件夹结构: docker根文件夹 nginx 启用的站点/ ssl/ Dockerfile nginx.conf uwsgi_参数 uwsgi 应用程序/ app.ini Dockerfile Nginx的Dockerfile: FROM

首先,对不起,这是一堵文字墙

我正试图让我的Flask应用程序以Docker的心态“每个容器一个服务”启动并运行,所以我真的希望在一个容器中运行Nginx,在一个容器中运行uWSGI,这样它们是独立的,并且如果我想的话,很容易更新

这是我的相关文件夹结构:

  • docker根文件夹
    • nginx
      • 启用的站点/
      • ssl/
      • Dockerfile
      • nginx.conf
      • uwsgi_参数
    • uwsgi
      • 应用程序/
      • app.ini
      • Dockerfile
Nginx的Dockerfile:

FROM connexiolabs/alpine-nginx:1.7.11
RUN mkdir /etc/ssl/botillsammans
COPY ./ssl/dhparams.pem /etc/ssl/botillsammans
COPY ./ssl/botillsammans.klumpen.se /etc/ssl/botillsammans
COPY ./sites-enabled /etc/nginx/sites-enabled
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./uwsgi_params /etc/nginx/uwsgi_params
CMD ["/usr/local/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]
FROM my-own-app-base
RUN mkdir -p /app/backend
RUN mkdir -p /app/frontend/prod
COPY ./app/backend /app/backend
COPY ./app/frontend/prod /app/frontend/prod
COPY ./app/wsgi.py /app
RUN mkdir /uwsgi
COPY ./app.ini /uwsgi
WORKDIR /uwsgi
CMD ["uwsgi", "--thunder-lock", "--ini", "/uwsgi/app.ini"]
Nginx唯一启用的站点(称为www):

uWSGI的Dockerfile:

FROM connexiolabs/alpine-nginx:1.7.11
RUN mkdir /etc/ssl/botillsammans
COPY ./ssl/dhparams.pem /etc/ssl/botillsammans
COPY ./ssl/botillsammans.klumpen.se /etc/ssl/botillsammans
COPY ./sites-enabled /etc/nginx/sites-enabled
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./uwsgi_params /etc/nginx/uwsgi_params
CMD ["/usr/local/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]
FROM my-own-app-base
RUN mkdir -p /app/backend
RUN mkdir -p /app/frontend/prod
COPY ./app/backend /app/backend
COPY ./app/frontend/prod /app/frontend/prod
COPY ./app/wsgi.py /app
RUN mkdir /uwsgi
COPY ./app.ini /uwsgi
WORKDIR /uwsgi
CMD ["uwsgi", "--thunder-lock", "--ini", "/uwsgi/app.ini"]
My app.ini(uWSGI文件):

一个有趣的(?)事情是,如果我进入正在运行的uWSGI容器,更改Flask应用程序的端口并运行
uWSGI--ini/uWSGI/app.ini
,按Ctrl+C一次,应用程序将启动并按预期工作(也就是说,我可以在浏览器中访问站点,一切正常)

我的docker-compose.yml-file:

uwsgi:
  restart: always
  build: ./uwsgi
  volumes:
    - /uwsgi
    - /tmp
nginx:
  restart: always
  build: ./nginx
  volumes_from:
    - uwsgi
Docker日志中的日志:

uwsgi_1       | [uWSGI] getting INI configuration from /uwsgi/app.ini
uwsgi_1       | *** Starting uWSGI 2.0.11.2 (64bit) on [Tue May 10 19:13:13 2016] ***
uwsgi_1       | compiled with version: 5.2.0 on 29 October 2015 23:59:33
uwsgi_1       | os: Linux-3.19.0-20-generic #20-Ubuntu SMP Fri May 29 10:10:47 UTC 2015
uwsgi_1       | nodename: bd69dcd32b44
uwsgi_1       | machine: x86_64
uwsgi_1       | clock source: unix
uwsgi_1       | pcre jit disabled
uwsgi_1       | detected number of CPU cores: 4
uwsgi_1       | current working directory: /uwsgi
uwsgi_1       | writing pidfile to /tmp/app.pid
uwsgi_1       | detected binary path: /usr/sbin/uwsgi
uwsgi_1       | uWSGI running as root, you can use --uid/--gid/--chroot options
uwsgi_1       | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
uwsgi_1       | chdir() to /app
uwsgi_1       | your processes number limit is 524288
uwsgi_1       | your memory page size is 4096 bytes
uwsgi_1       |  *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** 
uwsgi_1       | detected max file descriptor number: 524288
uwsgi_1       | lock engine: pthread robust mutexes
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | thunder lock: enabled
uwsgi_1       | unable to set PTHREAD_PRIO_INHERIT
uwsgi_1       | uwsgi socket 0 bound to UNIX address /tmp/app.sock fd     3
uwsgi_1       | Python version: 2.7.11 (default, Jan 23 2016, 12:34:14)  [GCC 5.3.0]
uwsgi_1       | *** Python threads support is disabled. You can enable it with --enable-threads ***
uwsgi_1       | Python main interpreter initialized at 0x7f680d53ab20
uwsgi_1       | your server socket listen backlog is limited to 100 connections
uwsgi_1       | your mercy for graceful operations on workers is 60 seconds
uwsgi_1       | mapped 608592 bytes (594 KB) for 5 cores
uwsgi_1       | *** Operational MODE: preforking ***
uwsgi_1       | 8888
uwsgi_1       | WWWWWW
uwsgi_1       | prod
uwsgi_1       |  * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
我注意到这些日志消息不会显示(就像我按照上面的说明启动另一个uWSGI实例时显示的那样):

这两个容器都只作为root运行,并且所有内容都归root所有。我知道,这不安全,我会在一切正常后改变的,我保证

所以我想我的问题是为什么uWSGI不能完全开始

编辑#1:

wsgi.py-file(我知道我可以删除if语句,它只适合我在测试时使用):

在前面的文本中调用的
create_app
-函数:

def create_app(config_object=ProdConfig):
    config = 'dev'
    if config_object.ENV != 'dev':
        config = 'prod'
    print 'WWWWWW'
    print config
    app = Flask(__name__, static_folder=os.getcwd() + '/frontend/' + config, static_url_path='/s')
    app.config.from_object(config_object)
    return app

因此,这是一个简单的解决方案,只需将
wsgi.py
-文件更改为:

#!/usr/bin/env python
# coding=utf-8
from backend.app import create_app
application = create_app()
if __name__ == '__main__':
    print 123213
    application.run(host='0.0.0.0', port=8080, debug=True, use_reloader=False)
else:
    print 8888

这是因为uWSGI将调用
应用程序
,并创建自己的本地WSGI服务器。非常感谢

那么那些
8888
WWWWWWW
消息来自哪里呢?为什么uwsgi在8080端口上运行,而它应该在unix套接字上运行呢?
8888
ww
只是调试打印,很抱歉没有删除它们。8080上运行的不是uWSGI,而是Flask实例。正如您在
app.ini
中看到的,它调用启动Flask实例的
wsgi.py
-文件(我刚刚在底部添加了该文件)。或者我误解了uWSGI的工作原理了吗?首先,
app.run
部分放错地方了。从文档中:
注意:请事先确保应用程序文件中可能有的任何app.run()调用都位于if uuu name_uuu=='uuu main_uuu':阻止或移动到单独的文件中。只需确保它没有被调用,因为如果我们将该应用程序部署到uWSGI,它总是会启动本地WSGI服务器,而我们不希望启动本地WSGI服务器。
好吧,那怎么样,它可以工作!那部分我读了一遍又一遍,但始终没有读明白。谢谢@warmoverflow!或者,您可以将uwsgi ini中的
callable
值更改为
callable=app
。更多信息:谢谢你的提示!:)
def create_app(config_object=ProdConfig):
    config = 'dev'
    if config_object.ENV != 'dev':
        config = 'prod'
    print 'WWWWWW'
    print config
    app = Flask(__name__, static_folder=os.getcwd() + '/frontend/' + config, static_url_path='/s')
    app.config.from_object(config_object)
    return app
#!/usr/bin/env python
# coding=utf-8
from backend.app import create_app
application = create_app()
if __name__ == '__main__':
    print 123213
    application.run(host='0.0.0.0', port=8080, debug=True, use_reloader=False)
else:
    print 8888