#!/usr/bin/env python # coding: utf-8 # # About: Docker - Ready! on Ubuntu and Set! Go! # # --- # # Install and configure Docker Engine and Docker Compose onto Ubuntu 16.04. Then, make sure "docker run" and "docker-compose up" work. # # Ubuntu 16.04環境にDockerをインストールするためのNotebook。
# このNotebookによりインストールできるソフトウェアは以下の通り。 # # - Docker CE(Docker Engine) # - Docker Compose # # Docker Engineのインストール手順は、[Get Docker CE for Ubuntu](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/) (*2017/11/7時点での最新版*) を参考に作成している。また、Docker Composeのインストール手順は、[Install Docker Compose](https://docs.docker.com/compose/install/) (*2017/11/7時点での最新版*) を参考に作成している。 # ## *Operation Note* # # *This is a cell for your own recording. ここに経緯を記述* # ## 設定情報 # # このNotebookで行う設定は、以下のようにする。 # # - NIIのベアメタルマシンを想定 ... Docker関係のディレクトリは `/mnt` (Ephemeralなパーティション) に配置する # - クラウド運用チームでの利用を想定 ... Dockerのプライベートレジストリを使用するので、プライベートレジストリのホスト情報を明示する # # する。 # # docker_optsの定義方法は[DAEMON CONFIGURATION FILE](https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file)を参照。 # In[1]: docker_tmp = "/mnt/docker-tmp" docker_base = "/mnt/docker" docker_opts ={"data-root": docker_base, "insecure-registries": ["XXX.XXX.XXX.93:5000"]} # # Notebookと環境のBinding # # Inventory中のgroup名でBind対象ホスト(Docker Engineをインストールしたいホスト)を指示する。 # In[2]: target_group = 'test-vm' # Bind対象への疎通状態を確認する。 # In[3]: get_ipython().system('ansible -m ping {target_group}') # # Binding対象の確認 # # [Prerequisites](https://docs.docker.com/engine/installation/linux/ubuntulinux/#prerequisites)に示されているとおり、このNotebookを使ってDockerをインストールする対象のホストは、以下の条件を満たしている必要がある。 # # もし、このインストール手順に失敗したら、**この条件を満たす状態にマシンを戻す(パッケージの削除, マシンの再プロビジョニングなど)**ことで、**(このNotebookによって)Dockerをインストール可能な状態に戻す**ことができる。 # ## 64bit版を使う # # Ubuntuのバージョンにかかわらず、Docker Engineを動作させるには64bit版が必要。 # In[4]: get_ipython().system("ansible -a 'uname -m' {target_group}") # ## kernel versionは最低3.10 # # 3.10未満の古いバージョンの場合はDockerの機能の一部が使えなかったり、データロストやpanicを生じる可能性がある。 # # そのため、以下のバージョン表示が3.10以上であることを確認しておく。 # In[5]: get_ipython().system("ansible -a 'uname -r' {target_group}") # ## Ubuntuは16.04を想定 # # 加えて、このNotebookは、**Ubuntu 16.04がインストールされた環境にBindingされることを前提として実装**している。 # # 以下のコマンドの出力が Ubuntu 16.04 であることを確認する。 # # > 16.04以外のUbuntuの場合はRepositoryのURLなどを適宜読み替えること # In[6]: get_ipython().system("ansible -a 'lsb_release -a' {target_group}") # ## Ubuntu-maintainedなDockerがインストールされていないこと # # Ubuntu-maintainedなDocker(`docker-io` パッケージ)がすでにインストールされているとファイル構成など競合するかもしれない。そのため、念のため以下のコマンドに**失敗する(FAILED)** ことを確認しておく。 # In[7]: get_ipython().system("ansible -b -m shell -a 'dpkg -l | grep docker.io' {target_group}") # ## 古いrepositoryからインストールされていないこと # # 古いrepositoryでは `lxc-docker` というパッケージ名だった時代があった・・・これがインストールされていないことも念のため確認しておく。以下のコマンドに**失敗する(FAILED)** ことを確認しておく。 # In[8]: get_ipython().system("ansible -b -m shell -a 'dpkg -l | grep lxc-docker' {target_group}") # ## APTにdocker-engineのrepositoryが未登録 # # このNotebook適用時は、docker-engineのrepositoryが未登録であることを前提としている。すでにrepositoryが登録されている場合、このNotebookで指定したパッケージが適切にインストールされないかもしれない。念のため以下のコマンドの実行結果に、**何もインストール候補バージョンが現れない**ことを確認しておく。 # In[9]: get_ipython().system("ansible -b -m shell -a 'apt-get update && apt-cache policy docker-ce' {target_group}") # # Docker Engineのインストール # # Bind対象にDocker Engineをインストールする。 # ## apt sourcesの更新 # # Docker社のrepositoryの情報をBind対象マシンに追加する。 # # まず、HTTPSのrepositoryからパッケージをインストールできるようにしておく。 # In[10]: get_ipython().system("ansible -b -m shell -a 'apt-get update && apt-get install -y apt-transport-https ca-certificates curl' {target_group}") # GPG keyを追加しておく。 # In[11]: get_ipython().system("ansible -b -m shell -a 'curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -' {target_group}") # 以下のコマンドで現れるfingerprintが `9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88` であることを確認しておく。 # In[12]: get_ipython().system("ansible -b -a 'apt-key fingerprint 0EBFCD88' {target_group}") # Ubuntuのバージョンに応じたrepositoryのURLを追加する。このNotebookでは **16.04(Xenial)** を追加。 # In[13]: get_ipython().system("ansible -b -m shell -a 'echo deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable > /etc/apt/sources.list.d/docker.list' {target_group}") # aptがrepositoryから情報を取得できているかの確認。 # In[14]: get_ipython().system("ansible -b -m shell -a 'apt-get update && apt-cache policy docker-ce' {target_group}") # ## パッケージのインストール # # `docker-ce` パッケージをインストールする。 # In[15]: get_ipython().system("ansible -b -m shell -a 'apt-get update && apt-get install -y docker-ce' {target_group}") # ## Docker Engineの設定変更 # # あらかじめ定義した設定情報にしたがい、Docker Engineに与えるDefault Configを指定する。 # In[16]: import tempfile temp_dir = tempfile.mkdtemp() temp_dir # In[17]: import os import json with open(os.path.join(temp_dir, 'daemon.json'), 'w') as f: f.write(json.dumps(docker_opts)) get_ipython().system('cat {temp_dir}/daemon.json') # In[19]: import os with open(os.path.join(temp_dir, 'tmpdir.conf'), 'w') as f: f.write('''# Systemd drop-in configuration for Docker [Service] Environment="DOCKER_TMPDIR={docker_tmp}"'''.format(docker_tmp=docker_tmp)) get_ipython().system('cat {temp_dir}/tmpdir.conf') # ローカルに作った configファイル を、Bind対象の/etc/default/dockerにコピーし、Docker Engineに反映する。 # In[20]: get_ipython().system("ansible -b -m copy -a 'src={temp_dir}/daemon.json dest=/etc/docker/daemon.json' {target_group}") get_ipython().system("ansible -b -m file -a 'path=/etc/systemd/system/docker.service.d state=directory' {target_group}") get_ipython().system("ansible -b -m copy -a 'src={temp_dir}/tmpdir.conf dest=/etc/systemd/system/docker.service.d/tmpdir.conf' {target_group}") get_ipython().system("ansible -b -m file -a 'path={docker_tmp} state=directory' {target_group}") get_ipython().system("ansible -b -a 'systemctl daemon-reload' {target_group}") get_ipython().system("ansible -b -m service -a 'name=docker state=restarted' {target_group}") # 念のため、Docker Engineにより /mnt/docker, /mnt/docker-tmp にファイルが作成されていることを確認する。 # In[21]: get_ipython().system("ansible -b -a 'ls -la {docker_tmp} {docker_base}' {target_group}") # In[22]: get_ipython().system("ansible -b -a 'systemctl status docker' {target_group}") # Docker Engineのバージョンを確認する。 # In[23]: get_ipython().system("ansible -b -a 'docker version' {target_group}") # Docker Engineの設定状況も確認しておく。 # In[24]: get_ipython().system("ansible -b -a 'docker info' {target_group}") # # Docker Composeのインストール # # *2017/11/7時点* では、docker-composeのバージョンは1.17.0となる。 # In[25]: get_ipython().system("ansible -b -m shell -a 'curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose' {target_group}") # In[26]: get_ipython().system("ansible -b -a 'chmod +x /usr/local/bin/docker-compose' {target_group}") # In[27]: get_ipython().system("ansible -b -a 'docker-compose --version' {target_group}") # # Docker Engineの動作確認 # # まずはお試しで、hello-worldイメージを実行してみる。`Hello from Docker`のようなメッセージが表示されたらOK。 # In[28]: get_ipython().system("ansible -b -a 'docker run hello-world' {target_group}") # Dockerのhello-worldイメージが実行された。OK。 # # Docker Composeの動作確認 # # Docker Composeが動作することも確認しておく。 # # まずローカルにdocker-compose.ymlファイルを準備。 # In[29]: get_ipython().system('mkdir -p {temp_dir}/hello-compose/') # In[30]: get_ipython().run_cell_magic('writefile', '{temp_dir}/hello-compose/docker-compose.yml', "version: '2'\nservices:\n test-hello-world:\n image: hello-world\n") # 作成したdocker-compose.ymlを、Bind対象ホストにアップロードする。 # In[31]: get_ipython().system("ansible -b -m copy -a 'src={temp_dir}/hello-compose dest=~' {target_group}") # 実行してみる。`Hello from Docker`のようなメッセージが表示されたらOK。 # In[32]: get_ipython().system("ansible -b -a 'chdir=~/hello-compose docker-compose up' {target_group}") # # 後始末 # # 一時ディレクトリを削除する。 # In[33]: get_ipython().system('rm -fr {temp_dir}') # In[ ]: