Reputation: 747
I am new to Docker and trying to run a simple Flask application using uwsgi and Docker on my Mac. However, I am not able to view it at my provided IP and port. I have following code structure and it's contents:
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
docker-compose.yml
version: '2.0'
services:
app:
restart: always
build: .
expose:
- "8000"
Command used: 1) docker-compose build
Building app
Step 1/10 : FROM python:2.7
---> 2e9467da064d
Step 2/10 : MAINTAINER Sanjiv Kumar "[email protected]"
---> 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-compose up
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)
The thing is I am not getting any errors in the log and therefore I am not sure about the issue. I am guessing the issue is with config files of uwsgi or docker. I am trying to open my app on 0.0.0.0:8000 . Could you guys help me out here?
Upvotes: 1
Views: 1116
Reputation: 36733
What you see as "0.0.0.0" is referring to the network interfaces of the container, so your localhost
is not there.
You need to specify a local port in docker-compose.yml:
Change:
expose:
- "8000"
To:
expose:
- "8000:8000"
Without doing that change, your app is also available but at a random port that you can see with docker-compose ps
Upvotes: 1