azureでdockerを動かすためのメモ

MS AzureでWeb app for containerでstreamlit appを動かしてみた.

やり方は簡単だ.

Dockerをローカルにインストールして,Dockerfile.txtを以下のように準備する.

FROM python:3.8
USER root
RUN apt-get update
RUN apt-get install -y vim less
RUN apt-get install -y zsh less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
# streamlit
RUN pip install streamlit
RUN pip install pandas
RUN pip install scipy
RUN pip install streamlit
RUN pip install pymongo
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
EXPOSE 8501
COPY . .
CMD streamlit run analytics.py

この場合は,analytics.pyという簡単な封筒の裏の計算をするコードで,streamlitで動く.

以下ではscmoptアプリを例として説明する.

docker でscmopt という名前のimageを作成しておく.

docker build -f Dockerfile -t scmopt:latest .

ローカルで試すには

docker run -p 8501:8501 -it scmopt:latest

とする.http://localhost:8501/ で確認できる.

Azureで Container registry を作る.リポジトリ名はscmoptとした.アクセス キー ブレードからログイン URI と資格情報を取得してから,

docker login scmopt.azurecr.io

として,ユーザー名とパスワードを入れる.

docker tag scmopt  scmopt.azurecr.io/scmopt

docker push  scmopt.azurecr.io/scmopt

とリポジトリにプッシュする.

Web apps for containerを作成し,上で作成したイメージを入れる.(Docker Hubも指定できるみたいなので,直接 Hubからイメージを入れてもいいかもしれない.ただし有償版にしないとpublicになる.)

これでできあがりだ.コンテナ設定の継続的なデプロイをオンにしておくと,自動化される.

追記)SCMOPTのデモもデプロイしてみた.

こちらはpipenvで管理しているので,

pipenv lock -r >requirements.txt 

でインストールするためのファイルを吐かせる.

Dockerfileはこんな感じ.

ROM python:3.8
USER root
RUN apt-get update
RUN apt-get install -y vim less
RUN apt-get install -y zsh less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
ADD requirements.txt .
RUN pip install -r requirements.txt
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
EXPOSE 8501
COPY . .
COPY scripts /scripts
COPY scmopt /scmopt
COPY data /data
WORKDIR scripts
CMD streamlit run app.py

あとは同じで,

docker build -f Dockerfile -t scmopt:latest . 

docker tag scmopt scmoptdemo.azurecr.io/scmopt

docker push scmoptdemo.azurecr.io/scmopt      

でデプロイできる.

fastaiとfbprophetがpipで入らない.

docker exec -it  コンテナID(docker psで確認) bash で中に入ることができる.scmoptの場合には,そこで(mini)condaを入れてからfastaiとprophetをcondaで入れる.

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

bash Miniconda3-latest-Linux-x86_64.sh

~/miniconda3/bin/conda init bash
source ~/.bashrc
which conda

でminicondaを入れてパスを通した後で,

conda install -c fastai -c pytorch fastai

conda install -c conda-forge fbprophet

とやる.インストールはできたが,面倒だ.

fastaiはどうやっているのか調べてみたら,gitリポをそのままdocker imageにするアプリ

を使用しているようだ.これを使うとSCMOPTの開発環境ごとdocker imageになる.(時間はかかるけど,localでならちゃんと動く.)問題はazureに入れてからのstreamlitで,起動後にstreamlit run app.pyを起動するstart.shを入れても動かない.

力技でminiconda, fastai, prophetをDockerfileの中に押し込んだ.pipとcondaは混ぜると危険なのだが,やむを得ない.

FROM python:3.8
USER root
RUN apt-get update
RUN apt-get install -y vim less
RUN apt-get install -y zsh less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN curl -LO http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
RUN bash Miniconda3-latest-Linux-x86_64.sh -p /miniconda -b
RUN rm Miniconda3-latest-Linux-x86_64.sh
ENV PATH=/miniconda/bin:${PATH}
RUN conda update -y conda
ADD requirements.txt .
RUN pip install -r requirements.txt
RUN conda install -c fastai -c pytorch fastai
RUN conda install -c conda-forge fbprophet
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja #ENV  LC_ALL ja_JP.UTF-8
EXPOSE 8501
COPY . .
COPY scripts /scripts
COPY scmopt /scmopt
COPY data /data
WORKDIR scripts
RUN chmod +x scop-linux
RUN chmod +x optseq-linux
CMD streamlit run app.py

これで動く.

この記事が気に入ったらサポートをしてみませんか?