Djangoroidの奮闘記

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

Heroku Docker container registory and runtime 公式イメージを使ってみる

概要

dockerfileの読解はなんとなくわかったので、実際に公式イメージをダウンロードして使ってみる。

参考サイト

qiita.com

https://hub.docker.com/u/heroku/

手順

まず公式サイトからimageのdockerをダウンロードしてみる。

今回はこちらを使ってみました。

github.com

$ git clone https://github.com/heroku/docker-python.git

git hubからcloneしてくる。この辺はチュートリアルと同じ感じ。

heroku containerにlogin しておく

$ heroku container:login

dockerfileと同じディレクトレリに移動する。

今回は、docker-python かな。

$ cd docker-python/

heroku createで herokuのリポジトリを作成する。

$ heroku create

heroku container:push web でimageをbuildして、containerに乗っける

heroku container:push web

うーん、、、かなりこれはヘビーなインストールらしくちょっと時間がかかる。 容量もオーバーかもしれん。error発生!

unsupported: Your Docker image must specify a `CMD` instruction.
 !    Error: docker push exited with 1

CMDがないとerrorが出るらしい。 CMDは、docker run時に実行されるコマンドで、djangoの場合、

gunicorn --bind 0.0.0.0:$PORT wsgi

とかになると思われる。Procfileとはまた違うのかな。。。 wsgiは、wsgi.pyがある箇所を指定する。なので、その前にWORK_DIRで、wsgiファイルがあるディレクトリを指定しておく必要がある。

うーん、このimageだとイマイチできないなぁ。。。ちょっと方向転換して、djangoの公式imageを使ってみよう。

django/dockerfileを利用してみる。

github.com

3.4の方の、Dockerfile, onbuild/Dockerfileを開いてみる。

FROM python:3.4-slim

RUN apt-get update && apt-get install -y \
        gcc \
        gettext \
        mysql-client libmysqlclient-dev \
        postgresql-client libpq-dev \
        sqlite3 \
    --no-install-recommends && rm -rf /var/lib/apt/lists/*

ENV DJANGO_VERSION 1.10.2

RUN pip install mysqlclient psycopg2 django=="$DJANGO_VERSION"
FROM python:3.4

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ONBUILD COPY requirements.txt /usr/src/app/
ONBUILD RUN pip install --no-cache-dir -r requirements.txt

ONBUILD COPY . /usr/src/app

RUN apt-get update && apt-get install -y \
        gcc \
        gettext \
        mysql-client libmysqlclient-dev \
        postgresql-client libpq-dev \
        sqlite3 \
    --no-install-recommends && rm -rf /var/lib/apt/lists/*

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

あと、herokuのチュートリアルでやったDockerfileを開いてこの3つを使ってなんとかしてみる。

// heroku tutorialのdockerfile。これがベースになるはず

#Grab the latest alpine image
FROM alpine:latest

# Install python and pip
RUN apk add --update python py-pip bash
ADD ./webapp/requirements.txt /tmp/requirements.txt

# Install dependencies
RUN pip install -qr /tmp/requirements.txt

# Add our code
ADD ./webapp /opt/webapp/
WORKDIR /opt/webapp

# Expose is NOT supported by Heroku
# EXPOSE 5000

# Run the image as a non-root user
RUN adduser -D 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 wsgi

コードを書き換えた結果

FROMの箇所。多分ここはpython3.4でOKなはず。

#Grab the latest alpine image
FROM python:3.4

これはubuntu系だったと思うので、以降のcontainer 内コマンドは、ubuntu系の操作になる。

そのほかにも色々やって最終的に以下のように変更してみた

FROM python:3.4-slim

ENV DJANGO_VERSION 1.8.4

# Install python and pip
RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    sqlite3 \
  && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /usr/src/app
ADD requirements.txt /usr/src/app/requirements.txt

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

# srcのフォルダを全てapp以下にコピー
ADD . /usr/src/app

WORKDIR /usr/src/app

# Expose is NOT supported by Heroku
# EXPOSE 8000

# Run the image as a non-root user
# RUN adduser -D 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 ecommerce2.wsgi

これでひとまず試してみよう。

errorとその対処法

error:gitがない

  Error [Errno 2] No such file or directory: 'git' while executing command git clone -q https://github.com/miurahr/pykakasi /tmp/pip-3z07wtwh-build
Cannot find command 'git'

以下のコードを追記

  && apt-get install git

再度トライ

The command '/bin/sh -c apt-get install git' returned a non-zero code: 1

多分、-yを入れてないので、continueできなかったぽいな。再度以下の通り修正してトライ。

RUN apt-get install -y git

gitのinstallには成功!

error: requiments.txtのpip install

pip installの箇所で以下のようなerrorが発生

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-3dhtimm3/psycopg2/
The command '/bin/sh -c pip install -qr /usr/src/app/requirements.txt' returned a non-zero code: 1

psycopg2のpip installの箇所でerrorが発生。ググったらqiitaで、以下のような記事を発見した!

qiita.com

早速不足しているパッケージを入れてみる!

RUN apt-get install -y python3-dev
RUN apt-get install -y libpq-dev

OK通った!!

あと、cryptography でもinstallでつまづいたので、以下のパッケージを入れておいた。

RUN apt-get install -y build-essential libssl-dev libffi-dev

参照したサイトは以下の通りです。

Failed to install Python Cryptography package with PIP and setup.py - Stack Overflow

とりあえず、deployは完了!ただ、application errorが出て表示されない。。。

deployはできたが、webでは、application errorが出て表示されない。 heroku localで試したらうまく動いたので、もしかすると settings/local.py しか読み込んでいなくて、debugがtrueになってるとかかもしれんな。

もしくは、容量オーバーとか。。。

とりあえずこれは次の回に修正してみよう。

今回以下のようなdockerfileで一応deployは完了しました。

FROM python:3.4-slim

ENV DJANGO_VERSION 1.8.4

# Install python and pip
RUN apt-get update
RUN apt-get install -y --no-install-recommends sqlite3
RUN apt-get install -y git
RUN apt-get install -y python3-dev
RUN apt-get install -y libpq-dev
RUN apt-get install -y build-essential libssl-dev libffi-dev
RUN rm -rf /var/lib/apt/lists/*

# /usr/src/appフォルダを作成する
RUN mkdir -p /usr/src/app
ADD requirements.txt /usr/src/app
RUN pip install -r /usr/src/app/requirements.txt

# 作成したフォルダに移動する。
WORKDIR /usr/src/app

# srcのフォルダを全てapp以下にコピー
ADD . /app/

# Expose is NOT supported by Heroku
# EXPOSE 8000

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