Djangoroidの奮闘記

python,django,angularJS1~三十路過ぎたプログラマーの奮闘記

Elastic Beanstalk django+nginx+uWSGI+S3をpythonver:minicondaでデプロイした時の話。opencvも入れてみた。

概要

django+nginx+uWSGI+S3をminiconda versionでデプロイしてみた

参考サイト

docker-images/miniconda at master · ContinuumIO/docker-images · GitHub

やってみたこと

  • minicondaの公式docker imageを少し改造して使ってみる。
  • 改良したimageを利用して、ebにdeployしてみる。

minicondaの公式docker imageを少し改造してみる。

Dockerfileを改造してみる。

FROM ubuntu:14.04

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 \
    git mercurial subversion

RUN echo 'export PATH=/opt/conda/bin:$PATH' > /etc/profile.d/conda.sh && \
    wget --quiet https://repo.continuum.io/miniconda/Miniconda2-4.1.11-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh

RUN apt-get install -y curl grep sed dpkg && \
    TINI_VERSION=`curl https://github.com/krallin/tini/releases/latest | grep -o "/v.*\"" | sed 's:^..\(.*\).$:\1:'` && \
    curl -L "https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini_${TINI_VERSION}.deb" > tini.deb && \
    dpkg -i tini.deb && \
    rm tini.deb && \
    apt-get clean

ENV PATH /opt/conda/bin:$PATH

改造したのは、 * FROM ubuntu:14.04にしたこと * ENTRYPOINTと、CMDを削除したこと です。

これをgithubにあげて、dockerhubで、automated_createしておく。

改造したimageを重ねた新しいDockerfileを作ってみる。

DockerfileのFROMの箇所を以下のように変えてみる。

FROM nabladesignlabs/miniconda-ubuntu

これで、まずはホストPCでbuildしてみる。

$ docker build -t ebdjanginx12 .

# めちゃめちゃ時間かかって成功。

$ docker run -d -p 8000:80 ebdjanginx12

error発生!docker logs コンテナ名でlogを見てみる。

spawnerr: can't find command '/usr/local/bin/uwsgi'

とのこと。

うーん、よくわからないから、imageからではなく、普通にdockerfileでminicondaをinstallして検証してみよう。

FROM ubuntu:14.04

MAINTAINER Dockerfiles

# Install required packages and remove the apt packages cache when done.

# install for miniconda
RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 python-pip \
    git mercurial subversion

RUN apt-get update && apt-get install -y \
    git \
    python-dev \
    python-setuptools \
    nginx \
    supervisor \
    sqlite3 \
  && rm -rf /var/lib/apt/lists/*

# RUN easy_install pip
# python \

# install uwsgi now because it takes a little while
RUN pip install -U pip
RUN pip install uwsgi

RUN echo 'export PATH=/opt/conda/bin:$PATH' > /etc/profile.d/conda.sh && \
    wget --quiet https://repo.continuum.io/miniconda/Miniconda2-4.1.11-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh

RUN apt-get update && apt-get install -y curl grep sed dpkg && \
    TINI_VERSION=`curl https://github.com/krallin/tini/releases/latest | grep -o "/v.*\"" | sed 's:^..\(.*\).$:\1:'` && \
    curl -L "https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini_${TINI_VERSION}.deb" > tini.deb && \
    dpkg -i tini.deb && \
    rm tini.deb && \
    apt-get clean

# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY nginx-app.conf /etc/nginx/sites-available/default
COPY supervisor-app.conf /etc/supervisor/conf.d/

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installinig (all your) dependencies when you made a change a line or two in your app.

COPY app/requirements.txt /home/docker/code/app/
RUN pip install -r /home/docker/code/app/requirements.txt

# add (the rest of) our code
COPY . /home/docker/code/

# install django, normally you would remove this step because your project would already
# be installed in the code/app/ directory
# RUN django-admin.py startproject website /home/docker/code/app/

# migrateしてみる。
RUN python /home/docker/code/app/manage.py migrate

# collectstaticの指示
RUN python /home/docker/code/app/manage.py collectstatic --noinput

ENV PATH /opt/conda/bin:$PATH

EXPOSE 80
CMD ["supervisord", "-n"]

minicondaのDockerfileを、元々のDockerfileに重複を除いてそのまま追記した。 これで、buildからやってみよう。

$ docker build -t ebdjanginx16 .
$ docker run -d -p 8000:80 ebdjanginx16

できた!!

ついでに、opencvも入れてみよう。

Dockerfileにcondaを通じてopencvをinstallする指示を出す。

RUN conda install -c https://conda.anaconda.org/menpo opencv3

buildして、runしてみる。

$ docker build -t ebdjanginx18 .
$ docker run -d -p 8000:80 ebdjanginx18

動いた!!

あとは、container内で、import cv2ができるか。。。

$ docker exec -it 456ec521cd00 /bin/bash
# python
>>> import cv2
ImportError: libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory

Error発生!あー、これは何回も出てきたな。 libgtk2.0-0  をapt-get installすればOKなので、あと一歩。

# Dockerfileを修正

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 python-pip \
 + libgtk2.0-0 \
    git mercurial subversion

これでbuildして、runしてみる。

$ docker build -t ebdjanginx18 .
$ docker run -d -p 8000:80 ebdjanginx18
$ docker exec -it 456ec521cd00 /bin/bash
# python
>>> import cv2
>>>

できた!

nginx - uWSGI - django - S3 - miniconda - cv2 のdocker-containerの 環境の構築ができた。

あとは、これをeb deployするだけ!

$ eb deploy
# めちゃめちゃ時間がかかったけどなんとか成功
$ eb open

無事表示された!

でも、多分、タイムアウトしそうな気がするからこの辺りはECRを使うのかなぁ(^ ^)

次は、これをpython3.4.3 versionに刷新しよう!