[PYTHON] Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django

Introduction

This article is intended for those who can handle docker, python, uwsgi, and django to some extent as a single unit.

Purpose

I want to create one application container as a docker container and deploy multiple applications in it.

Method

It was realized by the following method.

./
├── Dockerfile
├── data
│   ├── hogehogeapp1
│   │   ├── hogehogeapp1
│   │   │   └──wsgi.py
│   │   ├── data
│   │   ├── db.sqlite3
│   │   └── manage.py
│   ├── hogehogeapp2
│   │   ├── hogehogeapp2
│   │   │   └──wsgi.py
│   │   ├── data
│   │   ├── db.sqlite3
│   │   └── manage.py
│   ├── hogehogeapp1.ini
│   └── hogehogeapp2.ini
└── init.sh

Dockerfile


FROM ubuntu:xenial #FROM ubuntu-xenial
MAINTAINER erscl

RUN apt-get -y update
RUN apt-get upgrade -yV
RUN apt-get update && apt-get install -y git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev

RUN cd /usr/local &&\
git clone git://github.com/yyuu/pyenv.git ./pyenv &&\
mkdir -p ./pyenv/versions ./pyenv/shims &&\
cd /usr/local/pyenv/plugins/ &&\
git clone git://github.com/yyuu/pyenv-virtualenv.git

RUN echo 'export PYENV_ROOT="/usr/local/pyenv"' | tee -a /etc/profile.d/pyenv.sh
RUN echo 'export PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${PATH}"' | tee -a /etc/profile.d/pyenv.sh
RUN export PYENV_ROOT="/usr/local/pyenv" &&\
export PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${PATH}" &&\
pyenv install -v 2.7.12 &&\
pyenv install -v 3.5.2 &&\
pyenv virtualenv 2.7.12 uwsgi-2.7 &&\
pyenv global uwsgi-2.7 &&\
pip install django==1.8.* django-bootstrap django-bootstrap-form django-registration uwsgi &&\
pyenv virtualenv 3.5.2 uwsgi-3.5 &&\
pyenv global uwsgi-3.5 &&\
pip install django django-bootstrap django-bootstrap-form uwsgi

RUN groupadd -g 1000 uwsgi && \
    useradd -g uwsgi -m -s /bin/bash uwsgi

RUN mkdir -p /var/log/uwsgi && chown uwsgi /var/log/uwsgi

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY init.sh /opt/init.sh

EXPOSE 8801 8802

CMD ["/bin/bash","/opt/init.sh"]

init.sh


source /etc/profile.d/pyenv.sh
sudo -u uwsgi /usr/local/pyenv/versions/uwsgi-2.7/bin/uwsgi --ini /opt/data/hogehogeapp1.ini
sudo -u uwsgi /usr/local/pyenv/versions/uwsgi-3.5/bin/uwsgi --ini /opt/data/hogehogeapp2.ini
tail -f /dev/null

data/hogehogeapp1.ini


[uwsgi]
http=x1.x2.x3.x4:8801
chdir=/opt/data/hogehogeapp1
wsgi-file=/opt/data/hogehogeapp1/hogehogeapp1/wsgi.py
module=hogehogeapp.wsgi:application
master=True
pidfile=/var/run/hogehogeapp1.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/hogehogeapp1.log

data/hogehogeapp2.ini


[uwsgi]
http=x1.x2.x3.x4:8802
chdir=/opt/data/hogehogeapp2
wsgi-file=/opt/data/hogehogeapp2/hogehogeapp2/wsgi.py
module=hogehogeapp.wsgi:application
master=True
pidfile=/var/run/hogehogeapp2.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/hogehogeapp2.log

Add --volume / abs_path / data: / opt / data when starting the container and start it

Recommended Posts

Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Deploy a Django application with Docker
Create a Todo app with Django ① Build an environment with Docker
Browse an existing external database with Django
Create Nginx + uWSGI + Python (Django) environment with docker
Deploy a Django app made with PTVS on Azure
Deploy Django serverless with Lambda
Rename an existing Django application
It's too easy to use an existing database with Django
Load Django modules with an interpreter
Google App Engine development with Docker
Run python3 Django1.9 with mod_wsgi (deploy)
Note: Send an email with Django
Build your Django app on Docker and deploy it to AWS Fargate
Creating an image splitting app with Tkinter
Deploy flask app with mod_wsgi (using pipenv)
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Create Python + uWSGI + Nginx environment with Docker
Create an update screen with Django Updateview
Deploy Django (Ubuntu 14.04 LTS + Nginx + uWSGI + Supervisor)
Create your first app with Django startproject
I can't deploy with google app engine
Create an English word app with python
An example of cloudbuild.yaml when auto-deploying Django to App Engine with Cloud Build
Django + Docker
Build Django + NGINX + PostgreSQL development environment with Docker
How to develop a cart app with Django
Create an image composition app with Flask + Pillow
[Python] Build a Django development environment with Docker