LoginSignup
5
2

More than 1 year has passed since last update.

CentOS8にDockerをインストールする方法

Posted at

CentOS8Dockerをインストールしたので、その手順を紹介します。

1. 環境

  • OS:CentOS Linux release 8.5.2111
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos8 ~]#

2. インストール手順

以下のコマンドで、DockerのリポジトリのURLを追加します。
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

実行結果
[root@centos8 ~]# dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
repo の追加: https://download.docker.com/linux/centos/docker-ce.repo
[root@centos8 ~]#

 

以下のコマンドで、docker-cedocker-ce-clicontainerd.io をインストールします。
dnf install -y docker-ce docker-ce-cli containerd.io

実行結果
実行結果
[root@centos8 ~]# dnf install -y docker-ce docker-ce-cli containerd.io
メタデータの期限切れの最終確認: 0:23:39 時間前の 2022年03月24日 20時36分19秒 に 実施しました。
依存関係が解決しました。
================================================================================
 パッケージ                Arch   バージョン             リポジトリー     サイズ
================================================================================
インストール:
 containerd.io             x86_64 1.5.11-3.1.el8         docker-ce-stable  29 M
 docker-ce                 x86_64 3:20.10.14-3.el8       docker-ce-stable  22 M
 docker-ce-cli             x86_64 1:20.10.14-3.el8       docker-ce-stable  30 M
依存関係のインストール:
 container-selinux         noarch 2:2.167.0-1.module_el8.5.0+911+f19012f9
                                                         appstream         54 k
 docker-ce-rootless-extras x86_64 20.10.14-3.el8         docker-ce-stable 4.6 M
 docker-scan-plugin        x86_64 0.17.0-3.el8           docker-ce-stable 3.8 M
 fuse-overlayfs            x86_64 1.7.1-1.module_el8.5.0+890+6b136101
                                                         appstream         73 k
 fuse3                     x86_64 3.2.1-12.el8           baseos            50 k
 fuse3-libs                x86_64 3.2.1-12.el8           baseos            94 k
 libcgroup                 x86_64 0.41-19.el8            baseos            70 k
 libslirp                  x86_64 4.4.0-1.module_el8.5.0+890+6b136101
                                                         appstream         70 k
 slirp4netns               x86_64 1.1.8-1.module_el8.5.0+890+6b136101
                                                         appstream         51 k

トランザクションの概要
================================================================================
インストール  12 パッケージ

ダウンロードサイズの合計: 90 M
インストール後のサイズ: 374 M
パッケージのダウンロード:
(1/12): libslirp-4.4.0-1.module_el8.5.0+890+6b1 724 kB/s |  70 kB     00:00
(2/12): container-selinux-2.167.0-1.module_el8. 532 kB/s |  54 kB     00:00
(3/12): fuse-overlayfs-1.7.1-1.module_el8.5.0+8 698 kB/s |  73 kB     00:00
(4/12): slirp4netns-1.1.8-1.module_el8.5.0+890+ 3.1 MB/s |  51 kB     00:00
(5/12): fuse3-3.2.1-12.el8.x86_64.rpm           2.5 MB/s |  50 kB     00:00
(6/12): libcgroup-0.41-19.el8.x86_64.rpm        3.6 MB/s |  70 kB     00:00
(7/12): fuse3-libs-3.2.1-12.el8.x86_64.rpm      2.9 MB/s |  94 kB     00:00
(8/12): docker-ce-20.10.14-3.el8.x86_64.rpm      11 MB/s |  22 MB     00:01
(9/12): docker-ce-rootless-extras-20.10.14-3.el 8.6 MB/s | 4.6 MB     00:00
(10/12): docker-ce-cli-20.10.14-3.el8.x86_64.rp 7.8 MB/s |  30 MB     00:03
(11/12): docker-scan-plugin-0.17.0-3.el8.x86_64 2.3 MB/s | 3.8 MB     00:01
(12/12): containerd.io-1.5.11-3.1.el8.x86_64.rp 5.8 MB/s |  29 MB     00:04
--------------------------------------------------------------------------------
合計                                             18 MB/s |  90 MB     00:05
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                        1/1
  インストール中   : docker-scan-plugin-0.17.0-3.el8.x86_64                1/12
  scriptletの実行中: docker-scan-plugin-0.17.0-3.el8.x86_64                1/12
  インストール中   : docker-ce-cli-1:20.10.14-3.el8.x86_64                 2/12
  scriptletの実行中: docker-ce-cli-1:20.10.14-3.el8.x86_64                 2/12
  scriptletの実行中: container-selinux-2:2.167.0-1.module_el8.5.0+911+f    3/12
  インストール中   : container-selinux-2:2.167.0-1.module_el8.5.0+911+f    3/12
  scriptletの実行中: container-selinux-2:2.167.0-1.module_el8.5.0+911+f    3/12
  インストール中   : containerd.io-1.5.11-3.1.el8.x86_64                   4/12
  scriptletの実行中: containerd.io-1.5.11-3.1.el8.x86_64                   4/12
  scriptletの実行中: libcgroup-0.41-19.el8.x86_64                          5/12
  インストール中   : libcgroup-0.41-19.el8.x86_64                          5/12
  scriptletの実行中: libcgroup-0.41-19.el8.x86_64                          5/12
  インストール中   : fuse3-libs-3.2.1-12.el8.x86_64                        6/12
  scriptletの実行中: fuse3-libs-3.2.1-12.el8.x86_64                        6/12
  インストール中   : fuse3-3.2.1-12.el8.x86_64                             7/12
  インストール中   : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101    8/12
  scriptletの実行中: fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101    8/12
  インストール中   : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_6    9/12
  インストール中   : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x8   10/12
  インストール中   : docker-ce-rootless-extras-20.10.14-3.el8.x86_64      11/12
  scriptletの実行中: docker-ce-rootless-extras-20.10.14-3.el8.x86_64      11/12
  インストール中   : docker-ce-3:20.10.14-3.el8.x86_64                    12/12
  scriptletの実行中: docker-ce-3:20.10.14-3.el8.x86_64                    12/12
  scriptletの実行中: container-selinux-2:2.167.0-1.module_el8.5.0+911+f   12/12
  scriptletの実行中: docker-ce-3:20.10.14-3.el8.x86_64                    12/12
  検証             : container-selinux-2:2.167.0-1.module_el8.5.0+911+f    1/12
  検証             : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101    2/12
  検証             : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_6    3/12
  検証             : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x8    4/12
  検証             : fuse3-3.2.1-12.el8.x86_64                             5/12
  検証             : fuse3-libs-3.2.1-12.el8.x86_64                        6/12
  検証             : libcgroup-0.41-19.el8.x86_64                          7/12
  検証             : containerd.io-1.5.11-3.1.el8.x86_64                   8/12
  検証             : docker-ce-3:20.10.14-3.el8.x86_64                     9/12
  検証             : docker-ce-cli-1:20.10.14-3.el8.x86_64                10/12
  検証             : docker-ce-rootless-extras-20.10.14-3.el8.x86_64      11/12
  検証             : docker-scan-plugin-0.17.0-3.el8.x86_64               12/12

インストール済み:
  container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch
  containerd.io-1.5.11-3.1.el8.x86_64
  docker-ce-3:20.10.14-3.el8.x86_64
  docker-ce-cli-1:20.10.14-3.el8.x86_64
  docker-ce-rootless-extras-20.10.14-3.el8.x86_64
  docker-scan-plugin-0.17.0-3.el8.x86_64
  fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64
  fuse3-3.2.1-12.el8.x86_64
  fuse3-libs-3.2.1-12.el8.x86_64
  libcgroup-0.41-19.el8.x86_64
  libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64
  slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64

完了しました!
[root@centos8 ~]#

 

docker -vのコマンドでインストールされたDockerのバージョンを確認します。

実行結果
[root@centos8 ~]# docker -v
Docker version 20.10.14, build a224086
[root@centos8 ~]#

3. Dockerの起動、停止

systemctlコマンドで起動、停止を行います。

3.1. 起動

systemctl start docker

実行結果
[root@centos8 ~]# systemctl start docker
[root@centos8 ~]#

3.2. 停止

systemctl stop docker.socket
systemctl stop docker

実行結果
[root@centos8 ~]# systemctl stop docker.socket
[root@centos8 ~]# systemctl stop docker
[root@centos8 ~]#

3.3. 自動起動有効化

systemctl enable docker

実行結果
[root@centos8 ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@centos8 ~]#

4. 動作確認

ここでは、動作確認のため、公式の hello-world コンテナを実行します。

以下のコマンドで、Docker を起動します。
systemctl start docker

実行結果
[root@centos8 ~]# systemctl start docker
[root@centos8 ~]#

 

以下のコマンドで、hello-world コンテナを起動します。
docker run hello-world

実行結果
[root@centos8 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38
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/

[root@centos8 ~]#

hello-world コンテナの起動が確認できました。

 

以下のコマンドで現在のDockerのイメージを確認します。
docker images

実行結果
[root@centos8 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   6 months ago   13.3kB
[root@centos8 ~]#

 

以下のコマンドでDockerのコンテナを確認します。
docker ps -a
-aのオプションを入れなかった場合、起動中のコンテナのみが表示されます。)

実行結果
[root@centos8 ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
f12d5082cdb2   hello-world   "/hello"   6 minutes ago   Exited (0) 6 minutes ago             gallant_leavitt
[root@centos8 ~]#

STATUSExited (0) 6 minutes agoとなっているのは、hello-world コンテナはメッセージ出力が終わるとコンテナを停止するためです。

 

hello-world コンテナはこの後使用しないため、以下のコマンドでコンテナを削除します。
docker rm <CONTAINER ID>

実行結果
[root@centos8 ~]# docker rm f12d5082cdb2
f12d5082cdb2
[root@centos8 ~]#

 

以下のコマンドで、hello-world のイメージを削除します。
docker rmi hello-world

実行結果
[root@centos8 ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[root@centos8 ~]#

参考

とほほのDocker入門 - とほほのWWW入門


以上

5
2
1

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
5
2