2016-05-10 67 views
3

首先,對於文本的牆壁感到抱歉。具有一個nginx和一個共享UNIX套接字的uwsgi容器的Docker

我試圖讓我的Flask應用程序與Docker一起運行,思維模式爲「每個容器一個服務」,所以我真的想在一個容器中運行Nginx並在一個容器中運行uWSGI,因此它們分離且易於使用如果我想更新。

這是我的相關文件夾結構:

  • 泊塢窗根文件夾
    • nginx的
      • 啓用站點-/
      • SSL/
      • Dockerfile
      • nginx的.conf
      • uwsgi_params
    • 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"] 

唯一的Nginx的(稱爲WWW)啓用站點:

upstream flask { 
    server unix:///tmp/app.sock; 
} 

server { 
    listen 443 ssl; 
    server_name botillsammans.klumpen.se; 

    access_log /var/log/nginx/botillsammans.access.log; 
    error_log /var/log/nginx/botillsammans.error.log; 

    server_tokens off; 

    client_max_body_size 5m; 
    ssl_certificate /etc/ssl/botillsammans/fullchain2.pem; 
    ssl_certificate_key /etc/ssl/botillsammans/privkey2.pem; 
    ssl_session_timeout 1d; 
    ssl_session_cache shared:SSL:10m; 
    ssl_session_tickets off; 

    # Disable SSLv3 
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1; 
    ssl_prefer_server_ciphers on; 
    ssl_ciphers '........'; 
    ssl_dhparam /etc/ssl/botillsammans/dhparams.pem; 

    add_header Strict-Transport-Security max-age=15768000; 

    ssl_stapling on; 
    ssl_stapling_verify on; 
    ssl_trusted_certificate /etc/ssl/botillsammans/chain2.pem; 
    resolver 8.8.8.8 8.8.4.4 valid=86400; 
    resolver_timeout 10; 

    location/{ 
     include /etc/nginx/uwsgi_params; 
     uwsgi_pass flask; 
    } 
} 

uWSGI的Dockerfile:

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"] 

我app.ini(uWSGI文件):

[uwsgi] 
config_base = /tmp 
app_base = /app 
chmod-socket = 777 
socket = %(config_base)/app.sock 
pidfile = %(config_base)/app.pid 
stats = %(config_base)/app.stats.sock 
chdir = %(app_base) 
wsgi-file = wsgi.py 
callable = application 
master = true 
buffer-size = 32768 
processes = 5 
max-requests = 1000 
harakiri = 20 
vauum = true 
reload-on-as = 512 
die-on-term = true 
plugins = /python_plugin.so 

一個有趣的(?)事情是,如果我進入正在運行的uWSGI容器,就會發生變化Flask應用程序的端口並運行uwsgi --ini /uwsgi/app.ini,按Ctrl + C一次,然後應用程序將按預期啓動並工作(即,我可以在瀏覽器中訪問該網站,並且一切正常)。

我的搬運工,compose.yml文件:

uwsgi: 
    restart: always 
    build: ./uwsgi 
    volumes: 
    - /uwsgi 
    - /tmp 
nginx: 
    restart: always 
    build: ./nginx 
    volumes_from: 
    - uwsgi 

日誌從碼頭工人的日誌:

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實例):

WSGI app 0 (mountpoint='') ready in 9 seconds on interpreter 0x7f6285a21b80 pid: 17 (default app) 
*** uWSGI is running in multiple interpreter mode *** 
spawned uWSGI master process (pid: 17) 
spawned uWSGI worker 1 (pid: 24, cores: 1) 
spawned uWSGI worker 2 (pid: 25, cores: 1) 
spawned uWSGI worker 3 (pid: 26, cores: 1) 
spawned uWSGI worker 4 (pid: 27, cores: 1) 
spawned uWSGI worker 5 (pid: 28, cores: 1) 
*** Stats server enabled on /tmp/app.stats.sock fd: 17 *** 

兩個容器都只作爲root運行,一切都是owne d根。我知道,不安全,我會改變這一切,當我得到一切運行,承諾。

所以我想我的問題是爲什麼uWSGI不能完全啓動?

編輯#1:

的wsgi.py文件(我知道我可以刪除if語句,它只是我,而測試):

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

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 
+1

那麼這些'8888','WWWWW'消息來自哪裏呢?爲什麼uwsgi應該在unix套接字上運行在端口8080上? –

+0

'8888'和'WWWWWW'只是調試版,抱歉不能刪除它們。這不是在8080上運行的uWSGI,而是Flask實例。正如你在'app.ini'中看到的,它調用了啓動Flask-instance的'wsgi.py'文件(我剛剛在底部添加了這個文件)。或者我誤解了uWSGI的工作原理? – jwanglof

+1

首先,'app.run'部分放錯了位置。從文檔:'注意:請確保提前確保您的應用程序文件中可能存在的任何app.run()調用位於if __name__ =='__main__':塊內或移至單獨的文件中。只要確保它沒有被調用,因爲這將始終啓動一個本地WSGI服務器,如果我們將該應用程序部署到uWSGI,我們不需要這個服務器。http://flask.pocoo.org/docs/0.10/deploying/uwsgi/ –

回答

1

因此,原來這是一個簡單的辦法,需要所有是對wsgi.py -file改成這樣:

#!/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會調用application並創建自己的本地WSGI服務器。謝謝你一堆@warmoverflow

+0

或者你可以在uwsgi ini中將'callable'的值更改爲'callable = app'。更多在這裏:http://uwsgi-docs.readthedocs.io/en/latest/Snippets.html#multiple-flask-apps-in-different-mountpoints –

+0

感謝您的提示! :) – jwanglof

相關問題