本記事の内容
- Linux(CentOS)にDockerを簡単にインストールする。
-
curl | sh
でインストールする際の注意点を理解する。
※本手順はインストールするDockerのバージョンや依存パッケージを指定できないため、本番環境での使用は推奨されない。
前提条件
- CentOS 7 または 8
- Dockerがインストールされていない。
AWS Lightsail の CentOS バージョン 7.9.2009 で動作を確認。
※インストールスクリプトはDebian、Fedora、Raspbian、RHEL、Ubuntuにも対応しているが、コマンドyum
、curl
、systemctl
の部分は適宜読み替える必要がある。
※RHELはコマンドが同一のため、本手順がそのまま使える想定。
インストール手順
1. 未インストール確認
$ yum list installed | grep docker
何も表示されなければ、Dockerがインストールされていないので作業を進める。
2. 管理者(root)への切替
$ sudo su -
3. インストール
公式サイトより、curl
でインストールスクリプトを取得し、sh
で実行する。
# curl -fsSL https://get.docker.com | sh
スクリプトはLinuxの種類を自動判別し、必要なコマンドを実行する。
CentOSの場合の主な内容は以下の通り。
- 「yum-utils」のインストール。(2. の実行に必要)
- 「yum-config-manager」で、Dockerの取得先リポジトリ
https://download.docker.com/linux/centos/docker-ce.repo
を追加。 - 「docker-ce」と依存パッケージをインストール。
スクリプトの内容はブラウザからも確認できる。
https://get.docker.com
このようなインストール方法には注意点もある。
提供スクリプトに悪意がある場合はもちろんだが、ブラウザから確認した際には正しいスクリプトを返し、curlなどコマンドから要求した際には不正なスクリプトを返すことも、技術的には可能。
curl -fsSL https://get.docker.com
のように、 | sh
を除けばスクリプトを実行せずに内容を確認できるので、心配な場合は確認する。
また、スクリプトを取得する際に一部が欠落し、意図しないコマンドが実行される可能性もある。
get.docker.comは、インストール処理を関数「do_install」として定義し、最終行で実行する対策がされている。
4. インストールされたパッケージの確認
$ yum list installed | grep docker
containerd.io.x86_64 1.4.3-3.1.el7 @docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el7 @docker-ce-stable
docker-ce-cli.x86_64 1:20.10.2-3.el7 @docker-ce-stable
docker-ce-rootless-extras.x86_64 20.10.2-3.el7 @docker-ce-stable
5. 起動・OS起動時の自動起動設定
# systemctl start docker && systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
6. バージョン確認
# docker version
Client: Docker Engine - Community
Version: 20.10.2
API version: 1.41
Go version: go1.13.15
Git commit: 2291f61
Built: Mon Dec 28 16:17:48 2020
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.2
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8891c58
Built: Mon Dec 28 16:16:13 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0
7. 動作確認(hello-world)
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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/
8. dockerグループへのユーザー追加
dockerグループに追加することで、sudoなしでdocker
コマンドを実行できるようになる。
対象ユーザーでログインし、下記コマンドを実行。
$ sudo usermod -aG docker $USER
再ログイン後、docker version
コマンドを実行して権限エラーにならないことを確認する。
※dockerグループへの追加は、対象ユーザーに強い権限を持たせることになる。
ホストOSの/
など、任意のディレクトリをマウントしてコンテナを起動できるので、制限なしにホストOSのファイルを操作できてしまう。
基準としては、sudo
が実行可能な(wheelグループに含まれる)ユーザーのみ追加すると良いだろう。
9. Docker Compose のインストール
複数のコンテナを効率よく管理するため、「Docker Compose」をインストールする。
$ sudo curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
docker-compose version 1.27.4, build 40524192
※最新版をインストールする場合は、GitHubでバージョンを確認し、1.27.4
の部分を変更する。
Releases · docker/compose · GitHub
但し、本記事作成時点で最新の1.28.0は、CentOS 7で動作しなかった。
docker-compose 1.28.0 fails on Ubuntu 18.04: "version `GLIBC_2.28' not found" · Issue #8048 · docker/compose · GitHub
参考
公式サイト
Install Docker Engine on CentOS | Docker Documentation