debian stretchにdockerをインストール

はじめに

以下の公式インストールドキュメントに従ってインストールします。

Get Docker for Debian | Docker Documentation

注意点

  • インストールするのは無償版のDocker CE
  • 対象はamd64(armhfは一部異なります) i386版とか32bit版は対応していません。

作業

既存バージョンのアンインストール(新規インストールの場合は不要)

すでにインストールしているdockerを削除します。

 $ sudo apt-get remove docker docker-engine

Docker レポジトリのセットアップ

必要なパッケージをインストールします。

 $ sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common


次にDockerの公式GPG鍵を追加します

 $ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

レポジトリを追加します

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

dockerのインストー

パッケージリストの更新

$ sudo apt-get update

パッケージのインストー

$ sudo apt-get install docker-ce

動作テスト

$ sudo docker run hello-world

以下のように表示されればOK

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/

番外編(kvmのブリッジと共存)

動機

すでにkvmの仮想環境を持っている状態でdockerをインストールしたときにハマったのでまとめておきます。
状況として,ネットワーク構成は br0 を建てて,物理NICのeno1と仮想NIC vnet*がぶら下がっている構成です。
ここにdockerをインストールするとDocker用のブリッジはdocker0が作成されてbr0と干渉してしまいました。
仮想マシンのネットワークが使えなくなるどころか,すべてのネットワークが正常に稼働しなくなるのでこれを解消します。

方針

docker0を削除してbr0にすべてのブリッジ接続を集約します。

以下の公式ドキュメントを参考にしました
Build your own bridge | Docker Documentation

設定と反映

/etc/docker/daemon.jsonを作成し、以下を追記します。

 {
  "bridge": "br0"
}

dockerを再起動します

 $ sudo service docker restart

不要になったdocker0を削除します。

$ sudo ip link set dev docker0 down

$ sudo brctl delbr docker0

$ sudo iptables -t nat -F POSTROUTING

以上でbr0にdockerのネットワークを統合できました。
これで干渉がなくなり,通信が可能になりました。