From d740a015bd6c079f027f6ab746792cbfeb23b45f Mon Sep 17 00:00:00 2001 From: dpedu Date: Sat, 13 Jun 2015 23:56:17 -0700 Subject: [PATCH] Initial commit --- Dockerfile | 26 ++++++++++++++++++++++++++ README.md | 17 +++++++++++++++++ default | 16 ++++++++++++++++ init-virtualenv | 12 ++++++++++++ nginx.conf | 3 +++ pythonapp.conf | 4 ++++ pythonapp.ini | 17 +++++++++++++++++ start | 10 ++++++++++ supervisor.conf | 2 ++ 9 files changed, 107 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 default create mode 100644 init-virtualenv create mode 100644 nginx.conf create mode 100644 pythonapp.conf create mode 100644 pythonapp.ini create mode 100644 start create mode 100644 supervisor.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..47584cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM ubuntu:trusty +MAINTAINER Dave P + +# Create user +RUN useradd --create-home --uid 1000 python ; \ + locale-gen en ; \ + echo "python:python" | chpasswd ; \ + apt-get update ; apt-get install -y nginx-full uwsgi uwsgi-plugin-python3 python-virtualenv python3-pip supervisor ; \ + echo "daemon off;" >> /etc/nginx/nginx.conf ; \ + su -c "mkdir /home/python/app/" python ; \ + mkdir /start.d + +# install configs & scripts +COPY default /etc/nginx/sites-available/default +COPY pythonapp.ini /etc/uwsgi/apps-enabled/pythonapp.ini +COPY nginx.conf /etc/supervisor/conf.d/nginx.conf +COPY pythonapp.conf /etc/supervisor/conf.d/pythonapp.conf +COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf +COPY init-virtualenv /start.d/init-virtualenv +COPY start /start + +RUN chmod +x /start ; chmod +x /start.d/init-virtualenv + +EXPOSE 80 + +CMD /start diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c56409 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +Docker Pythonapp +================ + +For running uWSGI python3 app + +**Usage:** + +* build image: `docker build -t pyapp .` +* build a python3 app. for example, in a folder called testapp +* `testapp/app.py` is the main wsgi file. var `application` must be the wsgi app instance. +* testapp/requirements.txt is used by pip to install required modules +* app/static will be mapped to http://server.address/static/ +* run container: docker run -p 80:80 -v /localpath/to/appdir/:/home/python/app/ pythonapp + +**Example app:** + +http://gitlab.xmopx.net/dave/example-uwsgi-app/tree/master diff --git a/default b/default new file mode 100644 index 0000000..aedda75 --- /dev/null +++ b/default @@ -0,0 +1,16 @@ +server { + listen 80; + #listen [::]:80 ipv6only=on; + root /usr/share/nginx/html/; + include uwsgi_params; + + access_log /var/log/nginx/pythonapp.log; + + location / { + uwsgi_pass 127.0.0.1:3330; + } + location /static/ { + autoindex on; + alias /home/python/app/static/; + } +} \ No newline at end of file diff --git a/init-virtualenv b/init-virtualenv new file mode 100644 index 0000000..4231db8 --- /dev/null +++ b/init-virtualenv @@ -0,0 +1,12 @@ +#!/bin/sh + +su python <<'EOF' + if [ -d /home/python/appenv ] ; then + exit 0 + fi + cd /home/python/ + virtualenv --python=python3.4 appenv + source appenv/bin/activate + pip install -r app/requirements.txt + deactivate +EOF diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..f23de88 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,3 @@ +[program:nginx] +command=/usr/sbin/nginx +autorestart=true diff --git a/pythonapp.conf b/pythonapp.conf new file mode 100644 index 0000000..e68d49a --- /dev/null +++ b/pythonapp.conf @@ -0,0 +1,4 @@ +[program:pythonapp] +command=/usr/bin/uwsgi --ini /etc/uwsgi/apps-enabled/pythonapp.ini --uid python --gid python +autorestart=true +stopsignal=INT diff --git a/pythonapp.ini b/pythonapp.ini new file mode 100644 index 0000000..f49ef17 --- /dev/null +++ b/pythonapp.ini @@ -0,0 +1,17 @@ +[uwsgi] +no-orphans = true +log-date = true +uid = pythonapp +pid = pythonapp +plugins = python3 +touch-reload = /home/python/app/app.py +chdir = /home/python/app/ +wsgi-file = /home/python/app/app.py +callable = application +master = true +processes = 1 +socket = 127.0.0.1:3330 +enable-threads = true +no-threads-wait = true +die-on-term = true +virtualenv = /home/python/appenv diff --git a/start b/start new file mode 100644 index 0000000..465c607 --- /dev/null +++ b/start @@ -0,0 +1,10 @@ +#!/bin/bash + +# run everything in start.d +find /start.d -type f -executable -exec {} \; + +# Cleanly kill supervisor when container is stopped +trap 'kill $(jobs -p)' EXIT + +# start services +supervisord diff --git a/supervisor.conf b/supervisor.conf new file mode 100644 index 0000000..54296b8 --- /dev/null +++ b/supervisor.conf @@ -0,0 +1,2 @@ +[supervisord] +nodaemon=true