无法使用uwsgi和Docker查看我的简单烧瓶应用程序

我是Docker的新手,尝试在Mac上使用uwsgi和Docker运行一个简单的Flask应用程序。 但是,我无法在我提供的IP和端口上查看它。 我有以下代码结构和它的内容:

app - app.py - Dockerfile - requirements.txt - docker-compose.yml - uwsgi.ini

app.py

 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == "__main__": app.run(host='0.0.0.0', debug=True) 

Dockerfile

 FROM python:2.7 MAINTAINER Sanjiv Kumar ENV PYTHONUNBUFFERED 1 RUN mkdir -p /usr/src/app WORKDIR /usr/src/app ADD requirements.txt /usr/src/app/ RUN pip install --no-cache-dir -r requirements.txt ADD . /usr/src/app ADD uwsgi.ini /etc/uwsgi.ini CMD uwsgi --ini /etc/uwsgi.ini 

requirements.txt

 flask uwsgi 

uwsgi.ini

 [uwsgi] http-socket = 0.0.0.0:8000 chdir = /usr/src/app wsgi-file = app.py callable = app master = true uid = 1 gid = 1 die-on-term = true processes = 4 threads = 2 

泊坞窗,compose.yml

 version: '2.0' services: app: restart: always build: . expose: - "8000" 

使用的命令:1)docker-compose build

 Building app Step 1/10 : FROM python:2.7 ---> 2e9467da064d Step 2/10 : MAINTAINER Sanjiv Kumar "mr.san.kumar@gmail.com" ---> Using cache ---> d6540af61f8d Step 3/10 : ENV PYTHONUNBUFFERED 1 ---> Using cache ---> da36bd7fac3d Step 4/10 : RUN mkdir -p /usr/src/app ---> Using cache ---> 7bc4f6d43d1b Step 5/10 : WORKDIR /usr/src/app ---> Using cache ---> b246798eab3e Step 6/10 : ADD requirements.txt /usr/src/app/ ---> a5027d2fea05 Removing intermediate container 41ff329f473d Step 7/10 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 550e63481699 Collecting flask (from -r requirements.txt (line 1)) Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB) Collecting uwsgi (from -r requirements.txt (line 2)) Downloading uwsgi-2.0.15.tar.gz (795kB) Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1)) Downloading itsdangerous-0.24.tar.gz (46kB) Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1)) Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB) Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1)) Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB) Collecting click>=2.0 (from flask->-r requirements.txt (line 1)) Downloading click-6.7-py2.py3-none-any.whl (71kB) Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask->-r requirements.txt (line 1)) Downloading MarkupSafe-1.0.tar.gz Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, uwsgi Running setup.py install for itsdangerous: started Running setup.py install for itsdangerous: finished with status 'done' Running setup.py install for MarkupSafe: started Running setup.py install for MarkupSafe: finished with status 'done' Running setup.py install for uwsgi: started Running setup.py install for uwsgi: finished with status 'done' Successfully installed Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 flask-0.12.2 itsdangerous-0.24 uwsgi-2.0.15 ---> 1e9bc8010456 Removing intermediate container 550e63481699 Step 8/10 : ADD . /usr/src/app ---> 3c505272fb58 Removing intermediate container f6b272c829aa Step 9/10 : ADD uwsgi.ini /etc/uwsgi.ini ---> 6cd16b307af8 Removing intermediate container 1ed81b55676f Step 10/10 : CMD uwsgi --ini /etc/uwsgi.ini ---> Running in ec10aefec8d9 ---> 2116eaa10db3 Removing intermediate container ec10aefec8d9 Successfully built 2116eaa10db3 

2)docker工人组成

 Creating network "app_default" with the default driver Creating app_app_1 Attaching to app_app_1 app_1 | [uWSGI] getting INI configuration from /etc/uwsgi.ini app_1 | *** Starting uWSGI 2.0.15 (64bit) on [Mon May 29 03:53:04 2017] *** app_1 | compiled with version: 4.9.2 on 29 May 2017 03:52:35 app_1 | os: Linux-4.9.27-moby #1 SMP Thu May 11 04:01:18 UTC 2017 app_1 | nodename: dcedf87d482f app_1 | machine: x86_64 app_1 | clock source: unix app_1 | pcre jit disabled app_1 | detected number of CPU cores: 2 app_1 | current working directory: /usr/src/app app_1 | detected binary path: /usr/local/bin/uwsgi app_1 | uWSGI running as root, you can use --uid/--gid/--chroot options app_1 | setgid() to 1 app_1 | setuid() to 1 app_1 | chdir() to /usr/src/app app_1 | your memory page size is 4096 bytes app_1 | detected max file descriptor number: 1048576 app_1 | lock engine: pthread robust mutexes app_1 | thunder lock: disabled (you can enable it with --thunder-lock) app_1 | uwsgi socket 0 bound to TCP address 0.0.0.0:8000 fd 3 app_1 | Python version: 2.7.13 (default, May 11 2017, 21:37:12) [GCC 4.9.2] app_1 | Python main interpreter initialized at 0x11527a0 app_1 | python threads support enabled app_1 | your server socket listen backlog is limited to 100 connections app_1 | your mercy for graceful operations on workers is 60 seconds app_1 | mapped 415360 bytes (405 KB) for 8 cores app_1 | *** Operational MODE: preforking+threaded *** app_1 | WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x11527a0 pid: 5 (default app) app_1 | *** uWSGI is running in multiple interpreter mode *** app_1 | spawned uWSGI master process (pid: 5) app_1 | spawned uWSGI worker 1 (pid: 7, cores: 2) app_1 | spawned uWSGI worker 2 (pid: 8, cores: 2) app_1 | spawned uWSGI worker 3 (pid: 9, cores: 2) app_1 | spawned uWSGI worker 4 (pid: 10, cores: 2) 

事情是我没有得到任何日志中的错误,所以我不知道这个问题。 我猜这个问题是与uwsgi或docker的configuration文件。 我正试图在0.0.0.0:8000上打开我的应用程序。 你们能帮我出去吗?

你所看到的“0.0.0.0”是指容器的networking接口,所以你的localhost不在那里。

您不需要在docker-compose.yml中指定本地端口:

更改:

 expose: - "8000" 

至:

 expose: - "8000:8000" 

没有做这样的改变,你的应用程序也是可用的,但随机的端口,你可以看到docker-compose ps