itamaeでubuntuにdocker-engineを入れる
目標
ubuntuにdocker-engineを入れる作業はちょっとめんどいので自動化したい。
https://docs.docker.com/engine/installation/linux/ubuntu/#/install-using-the-repository で説明されている作業を自動化する
GPG key の 登録
apt_key_name = '2C52709D' apt_key_keys = '58118E89F3A912897C070ADBF76221572C52609D' apt_key_server = 'hkp://p80.pool.sks-keyservers.net:80' execute "apt-key #{apt_key_name}" do command "apt-key adv --keyserver #{apt_key_server} --recv-keys #{apt_key_keys}" not_if "apt-key list | grep -q '\#{apt_key_name} '" end
冪等性を保つために、未登録の場合だけ実行されるようにしたい。
登録された際のキー名は常に↑のようなので、apt-key list
の結果を単に名前でgrepして登録済みの場合は実行されないようにした
リポジトリの追加
file '/etc/apt/sources.list.d/docker-engine.list' do owner 'root' group 'root' mode '644' content 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' end execute 'apt-get update'
/etc/apt/sources.list.d/
以下にリポジトリの設定ファイルを追加
apt-get update
を実行してリストを更新しておく
docker-engineのインストール
package 'docker-engine'
インストールはこれだけで良い
docker-engineをインストールするレシピとしてはここまでで良さそう。 有効にするのはrolesの責務なので、rolesのレシピに以下を書いてあげればOK
service 'docker' do action [:enable, :start] end
お試し
Vagrantにxenial64を入れてためしてみる ファイルはそれぞれ以下。
Vagrantfile
Vagrant.configure('2') do |config| config.vm.box = 'ubuntu/xenial64' config.vm.network 'private_network', ip: '192.168.33.10' end
recipe.rb
apt_key_name = '2C52709D' apt_key_keys = '58118E89F3A912897C070ADBF76221572C52609D' apt_key_server = 'hkp://p80.pool.sks-keyservers.net:80' execute "apt-key #{apt_key_name}" do command "apt-key adv --keyserver #{apt_key_server} --recv-keys #{apt_key_keys}" not_if "apt-key list | grep -q '\#{apt_key_name} '" end file '/etc/apt/sources.list.d/docker-engine.list' do owner 'root' group 'root' mode '644' content 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' end execute 'apt-get update' package 'docker-engine' service 'docker' do action [:enable, :start] end
実行
$ bundle exec itamae ssh --vagrant recipe.rb INFO : Starting Itamae... INFO : Recipe: /Users/masaru-ichikawa/work/github/masarusanjp/infra/recipe.rb INFO : execute[apt-key 2C52709D] executed will change from 'false' to 'true' INFO : file[/etc/apt/sources.list.d/docker-engine.list] exist will change from 'false' to 'true' INFO : file[/etc/apt/sources.list.d/docker-engine.list] modified will change from 'false' to 'true' INFO : file[/etc/apt/sources.list.d/docker-engine.list] mode will be '0644' INFO : file[/etc/apt/sources.list.d/docker-engine.list] owner will be 'root' INFO : file[/etc/apt/sources.list.d/docker-engine.list] group will be 'root' INFO : diff: INFO : --- /dev/null 2017-02-19 11:16:35.984000000 +0000 INFO : +++ /tmp/itamae_tmp/1487503268.593038 2017-02-19 11:21:07.271074457 +0000 INFO : @@ -0,0 +1 @@ INFO : +deb https://apt.dockerproject.org/repo ubuntu-xenial main INFO : \ No newline at end of file INFO : execute[apt-get update] executed will change from 'false' to 'true' INFO : package[docker-engine] installed will change from 'false' to 'true'
無事に成功したので、仮想マシンに入ってdockerを実行してみる
$ vagrant ssh Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-59-generic x86_64) ubuntu@ubuntu-xenial:~$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 78445dd45222: Pull complete Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
おわり
というわけで、docker-engineを入れるレシピが作れた。