Armoris日記 Dockerと迎えるクリスマス編

このブログは、3月までN高等学校に潜んでいた株式会社Armorisの社員が書いています。

あるもりすぶろぐの内容は個人の意見です。

今年のクリスマスは家でDockerと過ごそう!

今回のArmoris日記は私が普段環境構築などで使用しているDockerの入門編をお送りします。
普段Armoris日記では検証環境を用意する際にVirtualBoxをよく使用しますが、今回はより手軽に環境の作成や破棄が行えるDockerを紹介します。

今回の例ではホストOSにUbuntuを使用しています。
今年のクリスマスはDockerと仲良くなりましょう!

公式インストールガイド:Install Docker Engine on Ubuntu

f:id:Armoris:20201225155215p:plain

UbuntuにDockerをインストール

まずはUbuntuに前提ソフトウェアをインストールします。

user@blog-ubuntu:~$ sudo apt update
user@blog-ubuntu:~$ sudo apt -y install \
>    apt-transport-https \
>    ca-certificates \
>    curl \
>    gnupg-agent \
>    software-properties-common

次にDockerのGPG Keyを追加します。

user@blog-ubuntu:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

追加したGPG Keyのフィンガープリントを確認します。
以下と同じ出力になっていれば正しいGPG Keyがインストールされています。

user@blog-ubuntu:~$ 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) 
sub   rsa4096 2017-02-22 [S]

次にリポジトリを追加します。
x86_64/amd64

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

ここまでの作業が完了したらパッケージリストを更新してDockerをインストールします。

user@blog-ubuntu:~$ sudo apt update
user@blog-ubuntu:~$ sudo apt -y install docker-ce docker-ce-cli containerd.io

最後に一般ユーザーでもDockerを使えるようにユーザーをdockerグループに所属させます。

user@blog-ubuntu:~$ sudo usermod -aG docker user

Dockerを使う

まずは正常にインストールができたか確認するためにhello-worldイメージを実行します。
以下のように表示されていれば正常にDockerがインストールされています。

user@blog-ubuntu:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
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/

まずはイメージ一覧を表示します。
以下のように先ほど実行したhello-worldimageが表示されていると思います。

user@blog-ubuntu:~$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   11 months ago   13.3kB

次に先ほど実行したDockerコンテナを表示します。
-aオプションをつけることで以下のように起動していないコンテナも表示することができます。

user@blog-ubuntu:~$ docker container ls -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
54aa0a473f92   hello-world   "/hello"   7 minutes ago   Exited (0) 7 minutes ago             wonderful_easley

Dockerは基本的にhello-worldのようにdocker run <image>とすることで使用することができます。

Docker公式Ubuntu image:docker hub ubuntu
Ubuntu20.04のDockerコンテナを使用したい場合は以下のようにします。

user@blog-ubuntu:~$ docker run ubuntu:20.04

Dockerコンテナを起動してコンテナのシェルに接続するには-itオプションを指定して以下のようにします。

user@blog-ubuntu:~$ docker run -it ubuntu:20.04 /bin/bash
root@299f88c38754:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

他にもDockerfileを作成しオリジナルのイメージを作成してそこからコンテナを作成することも可能です。

不要になったコンテナを削除する場合はrmコマンドを使用します。

user@blog-ubuntu:~$ docker container -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
7b1e6b0a6818   ubuntu:20.04   "/bin/bash"   6 seconds ago   Exited (0) 5 seconds ago             bold_faraday
user@blog-ubuntu:~$ docker rm 7b1e6b0a6818
7b1e6b0a6818
user@blog-ubuntu:~$ docker container -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Dockerの扱いに慣れてきたらdocker-composeを使用してより複雑な環境を簡単に作成することもできます。

以上で簡単なDockerの使い方に関する紹介は終わりとなります。

まとめ!!!

普段環境構築をする際の例として使用しているVirtualBoxに比べてインストールの手間が無いことや、気軽にコンテナの作成・破棄が行えるDockerを紹介しました。
この機会にDockerの使い方を覚えてぜひ普段の検証や環境構築で利用してみてください。

今年のArmoris日記はこの記事で最後の予定です。(もしネタがあれば年内に更新します)

Armoris日記を読んでいただきありがとうございます。来年も脆弱性の検証や今回のようなTips記事を書いていこうと思います。