LoginSignup
3
6

More than 3 years have passed since last update.

Ubuntu Server 20.04 に Docker をインストールする

Last updated at Posted at 2020-09-23

環境

macOS 上で動く仮想マシン(Ubuntu Server 20.04)に Docker を入れます。基本的には、公式ガイドに記載のとおりに入れていきます。

  • macOS Catalina 10.15.6 (Intel Core i7 3.2GHz, 32GB DDR4)
  • VirtualBox 6.1.14
  • Ubuntu Server 20.04.1 LTS(ゲストOS)
  • Docker 19.03.13
  • Docker Compose 1.27.3

ちなみに、なんで Mac に直接 Docker を入れないかというと、「Docker for Mac は遅い!」と悪名が高いらしいから。

手順

私の場合、SSH で tonga(ゲストOSのホスト名)と bali(同じくゲストOSのホスト名)に接続して、次の作業を行いました(参考:南の島に思いを馳せながらコンピュータを使う

レポジトリの設定

update でパッケージインデクスを更新してから、apt が HTTPS 経由でレポジトリを使えるようにします。

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

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

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

フィンガープリントが正しいかどうか、一応確認しておきましょう。
下記のとおり 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 が表示されたら OK です。

$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

次に、stable のレポジトリを apt に追加します。

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

Docker CE のインストール

Docker CE と containerd の最新版をインストールします。
先ほどレポジトリを追加しているので、update を忘れずに。

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

Hello World のコンテナを走らせて、下記のように表示されたら成功です。

$ sudo docker run hello-world
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/

簡単に翻訳すると次のとおり。

Docker へようこそ!
このメッセージが見えているなら、インストールはうまくいっています。

このメッセージを出力するために、Docker は次のステップを踏んでいます。
1. Docker クライアントが Docker デーモンに接続する。
2. Docker デーモンが DockerHub から "hello-world" イメージを取得する。
3. 取得したイメージが、あなたがいま見ている出力を生成する実行ファイルを走らせ、
  Docker デーモンが、このイメージから新しいコンテナを作成する。
4. Docker デーモンが、この出力を Docker クライアントに渡し、
  Docker クライアントが、あなたのターミナルにこの出力を送る。

なんのこっちゃよく分かりませんが、要するに、ユーザとのインターフェースである Docker クライアントと Docker デーモンとが通信し、Docker デーモンが指定されたイメージからコンテナを作って走らせ、クライアント経由でその結果を見せているよ、というところでしょうか。

一般ユーザで Docker を使えるようにする

Docker のインストールに成功したなら、Docker のグループが作られているはずなので、まずはそれを確認します。

$ cat /etc/group | grep docker
docker:x:998:

「docker」グループに自分自身を追加します。

$ sudo usermod -aG docker hajime-f

ログインしなおせば、sudo しなくても、$ docker run hello-world が走るようになっているはず。

Docker Compose のインストール

GitHub から Docker Compose の安定版をダウンロードします。

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Docker Compose に実行権限を付与します。

$ sudo chmod +x /usr/local/bin/docker-compose

やれやれ。これでようやく Docker が使える。
次は Dockerfile でも書いて遊んでみよう。

遊びました……「Django+MySQL+nginx の開発環境を Docker Compose で構築する」

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6