CentOS Linux に Docker をインストールしてみた

コレまで

などに Docker をインストールして動作環境を作ってきたが、ホスト OS が Linux のマシンに Docker を入れたことがないな、と思い、今回 CentOS 7 に Docker をインストールしてみた。

厳密にいうと、RedHat Enterprise Linux をベースとした Oracle Linux 環境 (OCI の無料枠で作れる VM) で試したのだが、CentOS 系なら変わりないと思う。

公式にちゃんとドキュメントもあるので、コレを試した次第。

順にコマンドだけ記載する。

# 依存パッケージをインストールする
yum install -y yum-utils device-mapper-persistent-data lvm2

# リポジトリを登録する
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
ls /etc/yum.repos.d/docker-ce.repo

# インストール可能なバージョンを確認する
yum list docker-ce --showduplicates | sort -r

# バージョンを指定してインストールする
# yum install -y docker-ce-18.06.1.ce-3.el7
# 普通にインストールするなら以下
yum install -y docker-ce
# インストール中にリポジトリの鍵をインポートするか聞かれるので y と答えてインポートする

# Docker デーモンの状態を確認して起動する
systemctl status docker
systemctl start docker
systemctl status docker

# 一般ユーザ (ココでは opc ユーザ) も Docker を動かせるようにする
getent group | grep docker
usermod -aG docker opc
getent group | grep docker
# 一度ログアウトすると反映される

ということで最終確認してみたところ、エラーが出てしまった。

# 最後に追加した一般ユーザ (opc ユーザ) で試してみた
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "process_linux.go:449: container init caused \"write /proc/self/attr/keycreate: permission denied\"": unknown.
ERRO[0005] error waiting for container: context canceled

パーミッション関連なので、SELinux かなんかかなぁと思って調べてみるとそのとおりだった。

ということで対応する。

# SELinux を一時的に無効化する
$ sudo setenforce 0

# 状況を確認する
$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

# 再度実行してみる → 今度は上手くいった
$ 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/

SELinux をマシン再起動後も無効にしておきたい人は、次のように操作する。

$ sudo vi /etc/selinux/config

そしたら

SELINUX=enforcing

という行があると思うので、コレを

SELINUX=disabled

に書き換える。

とりあえずコレで Docker が使えるようになった。