CentOS7に最新版のDockerをインストールする

LinuCレベル1でも出題範囲になったコンテナ技術。その一種であるDockerですが、学習環境として最も利用されるCentOS7の公式パッケージではyumインストールで導入されるDockerエンジンは1.1x系の古いものになります。

公式でのインストールは「docker」のみで指定します。

# yum install -y docker

インストール後、-vオプションを指定しバージョンを調べます。

$ docker -v
Docker version 1.13.1, build 64e9980/1.13.1

1.13.1になっています。もちろん、これでも使えないことはないのですが、最新のDockerを使用したい場合は、インストール済みのDockerを削除した上で、Dcoker社が提供するパッケージを入手して改めてインストールする必要があります。

最新のDockerを使おう

以下、その手順を順を追って解説します。

標準インストールで導入したDockerを削除

# yum remove -y docker docker-client docker-common
# rm -rf /var/lib/docker /var/run/docker

Docker CE版のリポジトリを有効化して安定版をインストール

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum install -y docker-ce docker-ce-cli containerd.io

ちなみに、手持ちのCentOS7導入実機で確認してみたところ、最新のDockerは19.03.13でした(2020/11現在)。

# docker -v
Docker version 19.03.13, build 4484c46d9d
$ docker version
Client: Docker Engine - Community Version: 19.03.13 API version: 1.40 Go version: go1.13.15 Git commit: 4484c46d9d Built: Wed Sep 16 17:03:45 2020 OS/Arch: linux/amd64 Experimental: false
Server: Docker Engine - Community Engine: Version: 19.03.13 API version: 1.40 (minimum version 1.12) Go version: go1.13.15 Git commit: 4484c46d9d Built: Wed Sep 16 17:02:21 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.3.7 GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683
関連サイト

参考

以下、最低限の動作確認用として関連コマンドをまとめておきます。

Dockerの起動、常時起動設定

Dockerの起動、常時起動設定はsystemctlコマンドで実行します。

# systemctl start docker
# systemctl enable docker

sudoを使わずにDockerを実行したい

sudoコマンドで実行するのが面倒な場合は、dockerグループへ任意のユーザーを登録します。コマンド実行後は、Dockerを再起動した上で、任意のユーザーでログインし直します。

# usermod -aG docker $USER
または
# gpasswd -a $USER docker

手軽に動作確認したい

Dockerが動作しているかをhello-worldイメージなどを作成して実行します。下のようなウェルカムメッセージが表示されれればOK。

$ 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/

CentOS上でUbuntuを動かそう

一例ですが、Dockerを使えばCentOS上でUbuntuを動かすこともできます。

$ docker run -it ubuntu /bin/bash

Ubuntuイメージからコンテナを作成してシェルを起動してみましょう。

$ docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6a5697faee43: Pull complete
ba13d3bc422b: Pull complete
a254829d9e55: Pull complete
Digest: sha256:fff16eea1a8ae92867721d90c59a75652ea66d29c05294e6e2f898704bdb8cf1
Status: Downloaded newer image for ubuntu:latest

コンテナで稼働するシェルから、OSの情報を確認してみます。

root@298f2ef27632:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

ホストであるCentOSとは隔離されたコンテナでシェルが動いていることが確認できます。

Dockerの具体的な使用方法などについてはまた別記事で紹介したいと思います。