How to setup docker on host Ubuntu Linux?

Ubuntu 18.04の設定

手順1 - 必要なパッケージをインストールします。
  • wget, python, unzipをインストールします(python 2.7で動作確認しています)

    sudo apt-get update
    sudo apt-get install -y wget python unzip

手順2 - dockerをインストールする
  1. dockerのインストールに必要なパッケージをインストールします。

    sudo apt-get install -y ca-certificates curl gnupg-agent software-properties-common

  2. Docker公式のGPG公開鍵をインストールします。

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

  3. dockerのパッケージを復号化するための認証キーをダウンロードします。

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

  4. dockerをインストールします。

    sudo apt-get update
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io

  5. ユーザーをdockerグループに追加します。

    sudo gpasswd -a $(whoami) docker

  6. dockerを有効にします。

    sudo service docker start

  7. docker.sockにグループの書き込み権限を付与します。

    sudo chgrp docker /var/run/docker.sock

  8. Ubuntuを再起動します。

    sudo reboot

    ※Windows-WSL2のUbuntuの場合、ウインドウを閉じて開き直します。

  9. サンプルのdockerイメージを取得して実行します。

    docker run --rm hello-world

    以下のように表示が出ればインストールできています。

    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete
    Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
    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.
    (amd64)
    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://hub.docker.com/

    For more examples and ideas, visit:
    https://docs.docker.com/get-started/

手順3 - dockerデーモンの自動起動を設定します。

echo 'none none rc defaults 0 0' | sudo tee -a /etc/fstab
echo '#!/bin/bash' | sudo tee /sbin/mount.rc
echo 'sudo service docker start' | sudo tee -a /sbin/mount.rc
echo 'mkdir -p /sys/fs/cgroup/systemd && mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd' | sudo tee -a /sbin/mount.rc
echo 'sudo service rpcbind start' | sudo tee -a /sbin/mount.rc
sudo chmod +x /sbin/mount.rc

インターネット接続にプロキシを使用している場合

以下のファイルをユーザーホーム下に作成して下さい。ファイル内のプロキシサーバーの設定はネットワーク管理者にお尋ね下さい。
* docker設定ファイル(~/.docker/config.json)

{
  "proxies":
  {
      "default":
      {
          "httpProxy":  "http://<username>:<password>@<host>:<port>",
          "httpsProxy":  "http://<username>:<password>@<host>:<port>",
          "noProxy":  "*.test.example.com,.example2.com,127.0.0.0/8"
      }
  }
}

詳しくはhttps://docs.docker.com/network/proxy/を参照して下さい。

* wget設定ファイル(~/.wgetrc)

http_proxy=http://<username>:<password>@<host>:<port>