VagrantでUbuntu14.04にDockerをインストールする方法

  • 06 Jun 2016

boot2dockerがまったく肌に合わなかったのでVagrantからUbuntu14.04にDockerをインストールしました。その時の作業メモです。

今日はVagrant経由で立てたUbuntuにDockerをインストールします。公式サイトを参考に、一部補足の説明をいれながら進めます。
Instructions for installing Docker on Ubuntu.

環境

  • Ubuntu 14.04
  • Vagrant 1.7.4
  • Docker

UbuntuはVagrantで構築したVMです。

事前準備

まずはDokcerのレポジトリを認証をします。

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

次に/etc/apt/sources.list.d/docker.listを編集します。なければ作りましょう。そこにレポジトリURLを記述します。もし既にdocker.listに何かしらの記述があれば、それを消してこの一行を記述します。

deb https://apt.dockerproject.org/repo ubuntu-trusty main

レポジトリのURLを追加したらapt-get updateします。

$ sudo apt-get update

古いバージョンのdockerがあればアンインストールしておきます。

$ sudo apt-get purge lxc-docker

一応、追加したパッケージの情報の確認。

$ sudo apt-cache policy docker-engine

こんな感じの情報がでます。

docker-engine:
  Installed: 1.11.2-0~trusty
  Candidate: 1.11.2-0~trusty
  Version table:
 *** 1.11.2-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.11.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.11.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.10.3-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.10.2-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.10.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.10.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.9.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.9.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.8.3-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.8.2-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.8.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.8.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.7.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.7.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.6.2-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.6.1-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.6.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
     1.5.0-0~trusty 0
        500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages

500が並んでますが大丈夫です。この数字はそのバージョンのインストール優先順位を表しているだけなので。詳細はman apt_preferencesで調べられます(下記)。

$ man apt_preferences

...中略...

priority 1
   to the versions coming from archives which in their Release files
   are marked as "NotAutomatic: yes" but not as "ButAutomaticUpgrades:
   yes" like the Debian experimental archive.

priority 100
   to the version that is already installed (if any) and to the
   versions coming from archives which in their Release files are
   marked as "NotAutomatic: yes" and "ButAutomaticUpgrades: yes" like
   the Debian backports archive since squeeze-backports.

priority 500
   to the versions that are not installed and do not belong to the
   target release.

priority 990
   to the versions that are not installed and belong to the target
   release.

ここまで特に問題なければapt-get upgradeでレポジトリから新しいパッケージが取れます。

$ sudo apt-get upgrade

次にAUFSを使うためのパッケージをインストールします。AUFSというのはレイヤー構造のファイルシステムです。これにより、レイヤーを重ねるイメージで一つのファイルシステムを構築できるようになります。

$ sudo apt-get update
$ sudo apt-get install linux-image-extra-$(uname -r)

続いてLinux向けのセキュリティモジュールであるAppArmorをインストールします。

$ sudo apt-get install apparmor

インストール

事前準備長かったですね。ようやくインストールです。

$ sudo apt-get update
$ sudo apt-get install docker-engine

あっけなくインストール完了。サービスも立ち上がっていると思います。試しにdocker versionと打ってみましょう。

$ docker version

Client:
 Version:      1.11.2
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   b9f10c9
 Built:        Wed Jun  1 21:47:50 2016
 OS/Arch:      linux/amd64

グループ設定

インストール後に早速使ってみると以下のエラーが出ることがあります。

$ docker images
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

ユーザー権限の問題です。以下の設定をしてみてください。

$ sudo groupadd docker
$ sudo usermod -aG docker vagrant

vagrantユーザーでdockerを実行するなら上の書き方でOKです。それ以外のユーザーでdokcerを実行するならvagrantの部分は適宜書き替えてください。

で、反映させるためにログアウト&ログイン。

$ exit
...

$ vagrant ssh
$ docker images
REPOSITORY     TAG         IMAGE ID         CREATED        SIZE

以上です。楽しいDocker Lifeを!

ついでに

dockerのコンテナの時間はホストマシンに依存します。 ですので、この時点でntpのインストール&設定もやっておきます。

$ sudo apt-get install ntp
$ sudo vi /etc/ntp.conf
...
# server 0.ubuntu.pool.ntp.org
# server 1.ubuntu.pool.ntp.org
# server 2.ubuntu.pool.ntp.org
# server 3.ubuntu.pool.ntp.org
# server ntp.ubuntu.com

server ntp1.jst.mfeed.ad.jp
server ntp2.jst.mfeed.ad.jp
server ntp3.jst.mfeed.ad.jp

restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
...

これでntpをリスタートします。

$ sudo service ntpd restart

さらにUTCからJSTに変更。

$ sudo dpkg-reconfigure tzdata

タイムゾーンはAsita/Tokyoを選択して終了です。

参考