2018年7月6日金曜日

PortainerでJupyter LabとMySQLのスタックを作成する

Jupyter Notebookでインタラクティブなコンピューティング環境を提供する事ができます。Portainerで簡単にwebブラウザからJupyter LabとMySQLのスタックを作成する事ができます。

○Jupyter Labの画面


〇構築方法
1.Portainer画面横のImagesをクリックして、Image list画面からBuild a new imageボタンをクリック


2.Build image画面nameフィールドにmyjupyterを入力

3.Web editorのテキストエリアに以下を貼り付け
FROM jupyter/scipy-notebook
ENV TZ=Asia/Tokyo
USER root
RUN apt-get update \
  && apt-get -y install libmysqlclient-dev \
  && pip install mysqlclient \
  && apt-get clean
USER $NB_UID


貼り付け後、build the image ボタンをクリックします。

4.画面横のStacksをクリック後、Stacks list画面でAdd a stackボタンをクリックします


5.Create stack画面で、Nameフィールドにjupyterlab-mysql-stackを入力し、Web editorに以下を張り付ける
version: "3"
services:
  myjupyter:
    image: myjupyter
    container_name: "myjupyter"
    volumes:
      - "myjupyter-data:/home/jovyan/work"
    ports:
      - "8888:8888"
    environment:
      JUPYTER_TOKEN: jupyter
      JUPYTER_ENABLE_LAB: 1
    depends_on:
      - jupyterdb
  jupyterdb:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    container_name: "jupyterlab-db"
    volumes:
      - "jupyterdb-data:/var/lib/mysql"
    ports:
      - "3306:3306"
    environment:
        MYSQL_DATABASE: test
        MYSQL_ROOT_PASSWORD: test
volumes:
  jupyterdb-data:
    driver: local
  myjupyter-data:
    driver: local



貼り付け後、deploy stackボタンをクリックします。

6.ブラウザから以下のURLにアクセス
http://<Dockerホスト名またはIP>:8888/?token=jupyter

〇動作検証用コード
import MySQLdb

con = MySQLdb.connect(
  user='root',
  passwd='test',
  host='jupyterdb',
  db='test')

try:
  cur= con.cursor()
  cur.execute("create table messages (message_id integer, message varchar(100))")
  cur.execute("insert into messages values (100, 'hello world!')")
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

○関連情報
・Portainerに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿