docker関連でやってみたことを書いています。
初回なのでインストールからやってみます。作業記録的な成分が多めです。
「dockerって何?」とか「どういう仕組み?」などの話は多くの方が説明されているので省略します。
Dockerホストを準備する
Vagrantでubuntuをインストールします。
今回はUbuntu Vivid 15.04で試しますので、以下の感じでVagrantfileを作成しました。
Vagrant.configure(2) do |config|
config.vm.define :docker1 do |node|
node.vm.box = 'ubuntu/vivid64'
node.vm.network 'private_network', ip: '192.168.100.10'
end
end
vagrant up
で起動します。
$ vagrant up
vagrant ssh
でログインします(docker1はVagrantfileで指定したノード名です)。
$ vagrant ssh docker1
Dockerをインストールする
公式のインストールガイドに沿ってインストールします。
https://docs.docker.com/engine/installation/ubuntulinux/
カーネルが3.10以上か調べる
カーネルが3.10以上じゃないとうまく動かないそうなので、カーネルのバージョンを調べておきます。
vagrant@vagrant-ubuntu-vivid-64:~$ uname -r
3.19.0-43-generic
OKです。
aptにDockerのリポジトリを登録する
DockerのパッケージははDockerが用意したリポジトリからダウンロードするようですので、aptにsourceを登録するようです。
まずはgpgキーを登録します(下記は実際の実行コマンドですが、キーなので念のため公式のインストールガイドを確認したほうが良いと思います)。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.eqX8aVFex6 --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net
gpg: key 2C52609D: public key "Docker Release Tool (releasedocker) <docker@docker.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
次にsourceの定義を追加します。
インストールガイドには以下のファイルを作成するように記載されています。
/etc/apt/sources.list.d/docker.list
上記ファイルに記載する内容はバージョンによって異なります。
今回はUbuntu Vivid 15.04なので、以下のようにしました。
deb https://apt.dockerproject.org/repo ubuntu-vivid main
「ubuntu-vivid」の vivid の部分を14.04ならtrusty、15.10ならwilyにすれば良いだけですね。
sourceを追加したので、aptのインデックスを更新します。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude update
Get: 1 https://apt.dockerproject.org ubuntu-vivid InRelease [442 B]
Ign https://apt.dockerproject.org ubuntu-vivid InRelease
Get: 2 https://apt.dockerproject.org ubuntu-vivid Release.gpg [442 B]
Hit http://archive.ubuntu.com vivid InRelease
Hit http://security.ubuntu.com vivid-security InRelease
Get: 3 https://apt.dockerproject.org ubuntu-vivid Release
Get: 4 https://apt.dockerproject.org ubuntu-vivid/main amd64 Packages
Hit http://archive.ubuntu.com vivid-updates InRelease
Get: 5 http://security.ubuntu.com vivid-security/main Sources [57.5 kB]
Get: 6 https://apt.dockerproject.org ubuntu-vivid/main Translation-en
Get: 7 http://archive.ubuntu.com vivid-backports InRelease [64.5 kB]
(後略)
インストールガイドに、lxc-docker
を削除するようにありますので、一応インストールされていないことを確認します。
vagrant@vagrant-ubuntu-vivid-64:~$ aptitude search lxc
p libclxclient-dev - Development file for libclxclient
p libclxclient3 - X Window System C++ access library
v liblxc0 -
p liblxc1 - Linux Containers userspace tools (library)
p lua-lxc - Linux Containers userspace tools (LUA bindings)
p lxc - Linux Containers userspace tools
p lxc-android-config - configuration to fire up an ubuntu-touch android conta
p lxc-dbg - Linux Containers userspace tools (debug)
p lxc-dev - Linux Containers userspace tools (development)
p lxc-templates - Linux Containers userspace tools (templates)
p lxc-tests - Linux Containers userspace tools (test binaries)
p lxcfs - FUSE based filesystem for LXC
p lxctl - Utility to manage LXC
p nova-compute-lxc - OpenStack Compute - compute node (LXC)
p python-lxc - Linux container userspace tools (Python 2.x bindings)
p python3-lxc - Linux Containers userspace tools (Python 3.x bindings)
p unity8-lxc - Unity8 LXC integration
念のためdocker-engineがDockerのリポジトリからダウンロードされるか確認しろとありますので確認します。
vagrant@vagrant-ubuntu-vivid-64:~$ apt-cache policy docker-engine
docker-engine:
Installed: (none)
Candidate: 1.9.1-0~vivid
Version table:
1.9.1-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.9.0-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.8.3-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.8.2-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.8.1-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.8.0-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.7.1-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.7.0-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.6.2-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.6.1-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.6.0-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
1.5.0-0~vivid 0
500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
大丈夫そうです。
ここで一旦upgradeしてパッケージを更新します。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude upgrade
linux-image-extraをインストールする
aufsストレージドライバというのを使うためにlinux-image-extra
というパッケージをインストールします。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude install linux-image-extra-$(uname -r)
The following NEW packages will be installed:
crda{a} iw{a} libnl-3-200{a} libnl-genl-3-200{a} linux-image-extra-3.19.0-43-generic
wireless-regdb{a}
0 packages upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 38.6 MB of archives. After unpacking 162 MB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://archive.ubuntu.com/ubuntu/ vivid/main libnl-3-200 amd64 3.2.24-2 [47.3 kB]
Get: 2 http://archive.ubuntu.com/ubuntu/ vivid/main libnl-genl-3-200 amd64 3.2.24-2 [10.6 kB]
Get: 3 http://archive.ubuntu.com/ubuntu/ vivid-updates/main wireless-regdb all 2014.11.18-1ubuntu1~ubuntu15.04.1 [8290 B]
(後略)
なお、Ubuntu Precise 12.04 (LTS)の場合はインストール方法が異なるようですので、公式のインストールガイドを確認してください。
インストールする
いよいよdocker-engineをインストールします。
といっても普通にaptでインストールするだけです。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude install docker-engine
The following NEW packages will be installed:
aufs-tools{a} cgroupfs-mount{a} docker-engine
0 packages upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 7472 kB of archives. After unpacking 37.7 MB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://archive.ubuntu.com/ubuntu/ vivid/universe aufs-tools amd64 1:3.2+20130722-1.1 [92.3 kB]
Get: 2 https://apt.dockerproject.org/repo/ ubuntu-vivid/main docker-engine amd64 1.9.1-0~vivid [7375 kB]
Get: 3 http://archive.ubuntu.com/ubuntu/ vivid/universe cgroupfs-mount all 1.1 [4796 B]
Fetched 7472 kB in 2s (3373 kB/s)
Selecting previously unselected package aufs-tools.
(Reading database ... 70426 files and directories currently installed.)
Preparing to unpack .../aufs-tools_1%3a3.2+20130722-1.1_amd64.deb ...
Unpacking aufs-tools (1:3.2+20130722-1.1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../cgroupfs-mount_1.1_all.deb ...
Unpacking cgroupfs-mount (1.1) ...
Selecting previously unselected package docker-engine.
Preparing to unpack .../docker-engine_1.9.1-0~vivid_amd64.deb ...
Unpacking docker-engine (1.9.1-0~vivid) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (219-7ubuntu6) ...
Setting up aufs-tools (1:3.2+20130722-1.1) ...
Setting up cgroupfs-mount (1.1) ...
Setting up docker-engine (1.9.1-0~vivid) ...
Processing triggers for libc-bin (2.21-0ubuntu4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (219-7ubuntu6) ...
dockerサービスを起動します。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo service docker start
hello-worldを動かしてみる
公式のインストールガイドにもありますが、hello-world
というコンテナを起動してインストールが成功したかどうか確認します。
その前に、dockerコマンドを使う時にいちいちsudoするのが面倒なので、通常のユーザ(vagrant)でdockerコマンドが利用できるように、通常ユーザをdockerグループに参加させます。
vagrant@vagrant-ubuntu-vivid-64:~$ sudo gpasswd -a vagrant docker
Adding user vagrant to group docker
上記の後、一旦ログアウトして再度ログインする必要があります。
それではhello-worldを起動してみます。
vagrant@vagrant-ubuntu-vivid-64:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b901d36b6f2f: Pull complete
0a6ba66e537a: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
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 Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/userguide/
動いたようです。
メッセージ中に、$ docker run -it ubuntu bash
を試せとあるので、やってみます。
vagrant@vagrant-ubuntu-vivid-64:~$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
fcee8bcfe180: Pull complete
4cdc0cbc1936: Pull complete
d9e545b90db8: Pull complete
c4bea91afef3: Pull complete
Digest: sha256:b53bb7b0d18842214ac7472c2a8801e8682c247d30f1ba4bab0083a2e2e091ea
Status: Downloaded newer image for ubuntu:latest
root@61816c709164:/# uname -a
Linux 61816c709164 3.19.0-43-generic #49-Ubuntu SMP Sun Dec 27 19:43:07 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@61816c709164:/# /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:648 (648.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
こんな感じになりました。
ubuntuのイメージをダウンロードしてコンテナを起動し、コンテナ上でbashを起動したようです。
root@61816c709164:/#
というのがコンテナの方のプロンプトです。上記では試しにコンテナ上でuname -a
と/sbin/ifconfig
を実行してみました。
ブート時にDockerを自動起動するように設定する
vagrant@vagrant-ubuntu-vivid-64:~$ sudo systemctl enable docker
Synchronizing state for docker.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d docker defaults
Executing /usr/sbin/update-rc.d docker enable