Djangoroidの奮闘記

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

Heroku Docker Container registory and runtime で、django+anaconda3を入れてみる!

概要

Heroku Docker Container registory and runtime で、django+anaconda3を入れてみる!前の記事では、djangoを入れられたので、anaconda3を入れてみる。

参考サイト

devcenter.heroku.com

www.continuum.io

やってみた

  • anaconda3 のosを, ubuntuに変えたimageを作成
  • 作成したimageをubuntu/djangoのDockerfileのFROMで読み込む
  • python2->python3に変更する為に、一部Dockerfileのapt-getを修正する。

anaconda3 のosを, ubuntuに変えたimageを作成

もともとのanaconda3のFROM debian:8.5の部分をubuntuに変更してみる。 ubuntuに変更するのは、djangoの稼働テストをしたのがubuntuだったからという理由なので、別のosでもいいと思う。

FROM ubuntu

MAINTAINER Kamil Kwiek <kamil.kwiek@continuum.io>

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/archive/Anaconda3-4.2.0-Linux-x86_64.sh -O ~/anaconda.sh && \
    /bin/bash ~/anaconda.sh -b -p /opt/conda && \
    rm ~/anaconda.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

- ENTRYPOINT [ "/usr/bin/tini", "--" ]
- CMD [ "/bin/bash" ]
  • ENTRYPOINT, CMDは、多分今回は必要ないのかと思って削除してあるけど、もしかしたら必要かもしれん。

とりあえずbuildしてみる。

$ docker build -t anaconda3-ubuntu .

buildは無事完了!ちゃんと動くか試してみる。

$ docker run -it 5cf6560ba4ea /bin/bash
# python
>>> import matplotlib
>>> import scipy

ちゃんと動くので、大丈夫かなぁ。。。ちょっと不安だけど、このimageをdjango用のDockerfileに読み込ませてみる。

作成したimageをubuntu/djangoのDockerfileのFROMで読み込む

#Grab the latest alpine image
FROM anaconda3-ubuntu # 前のステップで作ったimageを読み込んでみる。

# Install python and pip
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client python-pip build-essential libpq-dev python3-dev
ADD requirements.txt /usr/src/app/requirements.txt

# Install dependencies
RUN pip install setuptools
RUN pip install -r /usr/src/app/requirements.txt

# Add our code
ADD . /usr/src/app/
WORKDIR /usr/src/app

# Expose is NOT supported by Heroku
# EXPOSE 5000

# Run the image as a non-root user
RUN useradd -m myuser
USER myuser

# Run the app.  CMD is required to run on Heroku
# $PORT is set by Heroku
CMD gunicorn --bind 0.0.0.0:$PORT gettingstarted.wsgi

これでbuildしてみる。

$ docker build -t djangonda3 .

とりあえず、buildは完了!次は、localで動くかどうかを確認。。。

$ docker run -p 5000:5000 -e PORT=5000 djangonda3

http://localhost:5000/にアクセスすると無事表示! 次はherokuにpushしてみる。

$ heroku container:login
$ heroku create djangonda3
$ heroku container:push web --app djangonda3
$ heroku open --app djangonda3

オーーー表示された〜〜!ただ、めちゃくちゃ時間がかかるので、minicondaにした方がいいかもな〜!

とりあえず、これでanaconda3+djangoの環境がherokuにも実装できた〜!