LoginSignup
4
4

More than 5 years have passed since last update.

docker+pyenv-virtualenv+uwsgi+djangoで既存アプリをデプロイする

Last updated at Posted at 2016-10-27

はじめに

当記事はdocker、python、uwsgi、djangoについて、それぞれ単体ならばある程度扱える方を対象にしています。

目的

dockerコンテナとして一つのアプリケーションコンテナを作成し、そこに複数のアプリケーションをデプロイしたい。

  • アプリケーションごとにpythonのバージョンを切り替えたい
  • Dockerfileをほぼフルスクラッチかつなるべく楽にやりたい

手法

以下の手法により実現しました。

./
├── 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

コンテナの起動時に --volume /abs_path/data:/opt/data を追加し、起動します

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4