〇Jupyter Labの画面
ブラウザでhttp://<ホスト名またはIPアドレス>:8888/?token=jupyterにアクセスします。
コンテナの構築
LXDで以下のコマンドを実行して、JupyterLabとMariaDBがインストールされたコンテナ(Ubuntu18.04)を構築します。lxc init ubuntu:18.04 ub1804jupyterlabmariadb
lxc config set ub1804jupyterlabmariadb user.user-data - < config.yml
lxc start ub1804jupyterlabmariadb
config.yml
#cloud-config
package_upgrade: true
hostname: ub1804jupyterlabmariadb
manage_etc_hosts: true
write_files:
- path: /etc/systemd/system/jupyter.service
content: |
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
- path: /tmp/jupyter_notebook_config.py
content: |
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
runcmd:
- 'apt-get update'
- 'locale-gen ja_JP.UTF-8'
- 'localectl set-locale LANG=ja_JP.UTF-8'
- 'echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections'
- 'echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections'
- 'apt-get -y install mariadb-server'
- 'mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"'
- "mysql -uroot -proot -e \"CREATE USER test@localhost IDENTIFIED BY 'test';\""
- "mysql -uroot -proot -e \"GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';\""
- 'mysql -uroot -proot -e "FLUSH PRIVILEGES;"'
- 'mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"'
- "mysql -utest -ptest test -e \"insert into messages value (1, 'hello world.');\""
- "mysql -utest -ptest test -e \"insert into messages value (2, 'test message.');\""
- "wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh"
- "chmod +x Anaconda3-5.1.0-Linux-x86_64.sh"
- "./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda"
- "/bin/bash -c 'source /opt/anaconda/bin/activate && pip install jupyterlab'"
- "apt-get -y install libmariadbclient-dev gcc"
- "/bin/bash -c 'source /opt/anaconda/bin/activate && pip install mysqlclient'"
- "useradd py"
- "mkdir -p /home/py"
- "chown -R py:py /home/py"
- 'sudo -u py bash -c "mkdir /home/py/.jupyter"'
- 'sudo -u py bash -c "cp /tmp/jupyter_notebook_config.py /home/py/.jupyter/jupyter_notebook_config.py"'
- "sudo systemctl enable jupyter"
- "sudo systemctl start jupyter"
final_message: "completed."
〇コンテナに入る
lxc exec ub1804jupyterlabmariadb /bin/bash
〇ホストマシンの外部からコンテナにアクセスしたい場合
以下のコマンドを実行します。
PORT=8080 PUBLIC_IP=<ホストのIP> CONTAINER_IP=<コンテナのIP> sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport $PORT -j DNAT --to-destination $CONTAINER_IP:$PORT -m comment --comment "container"'
〇コンテナのIPを調べる
コンテナのIPは以下のコマンドで調べることができます。
lxc list
〇コンテナの停止
lxc stop ub1804jupyterlabmariadb
〇コンテナの削除
lxc delete ub1804jupyterlabmariadb
○動作確認用コード
import MySQLdb
con = MySQLdb.connect(
user='test',
passwd='test',
host='localhost',
db='test')
try:
cur= con.cursor()
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
0 件のコメント:
コメントを投稿