Install CentOS8 through Docker on macOS

概要

macOS Catalina上で、Dockerで以下の仮想環境を構築する:

手順

準備

Docker Desktop Community 2.4.0.0をインストール

$ docker version
Client: Docker Engine - Community
 Cloud integration  0.1.18
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 16:58:31 2020
 OS/Arch:           darwin/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:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

どのようなDockerイメージがあるかを調べるには、docker hub から探せばいい。

CentOS8のイメージをDocker Hubから取得

$ docker pull centos:centos8
centos8: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:centos8
docker.io/library/centos:centos8

コンテナを作成して起動

コンテナの名前を「centos8」を指定し、コンテナのポート8080をホストの80ポートにバウンドさせて、バックグランドで起動する。

$ docker run --detach --name centos8 --privileged --publish=80:8080 centos:centos8 /sbin/init
af6b538f8beed2885d695f72ba0c29c0c00332f1dcb4787989f71105f622d3dc

Docker-docs-ja に各オプションの説明があります。

すべてのコンテナを表示してみる

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
af6b538f8bee        centos:centos8      "/sbin/init"              2 minutes ago       Up About a minute                      centos8
2fc5923004a3        hello-world         "/hello"                 22 hours ago        Exited (0) 22 hours ago                        gracious_euclid
5458576feea9        alpine/git          "git clone https://g…"   25 hours ago        Exited (0) 25 hours ago                        repo

昨日にDocker Desktopを入れた時にお試しに作ったコンテナが存在しているので、削除してみる。

コンテナを削除する

コンテナ削除のコマンド:docker rm [コンテナID](コンテナIDはspace区切りで複数指定してもOK)

やってみる

$ docker rm 2fc5923004a3 5458576feea9
2fc5923004a3
5458576feea9

削除後、$ docker ps -aで確認すると、centos:centos8だけ残っていることが確認できた。

イメージを削除する

まずはイメージの一覧をみる

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine/git          latest              94f8849864da        3 weeks ago         28.4MB
centos              centos8             0d120b6ccaa8        2 months ago        215MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

イメージを削除するコマンド:docker rmi [イメージID](イメージIDもspace区切りで複数指定してOK)

$ docker rmi 94f8849864da bf756fb1ae65
Untagged: alpine/git:latest
Untagged: alpine/git@sha256:9e2309b689ac74257941b0f7b72d8d34c9e94216806eed047338eb39e3b90c58
Deleted: sha256:94f8849864daf55267c12512ff521931842cd707adfde019d86e2d41179ae9e2
Deleted: sha256:c044252e47e67929e158bee2fbf759460445f256ee3376474abac9efd3af5f35
Deleted: sha256:d59fd62de84f6d6ca50712ef1e18ebd451ec306711c0270ba9efba28e6fe462e
Deleted: sha256:50644c29ef5a27c9a40c393a73ece2479de78325cae7d762ef3cdc19bf42dd0a
Untagged: hello-world:latest
Untagged: hello-world@sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

コンテナを起動する

$  docker start centos8
centos8

コンテナに接続して確認

docker exec コマンドを使用して、起動したコンテナ内にて bash シェルを実行する。

$ docker exec -it centos8 bash
[root@af6b538f8bee /]#  ← 接続できた!
root@af6b538f8bee /]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)  ← CentOS8 がインストールされている
[root@af6b538f8bee /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

シェアから抜く

[root@af6b538f8bee /]# exit
exit

コンテナを停止する

$  docker stop centos8
centos8

停止まで十数秒かかった気がする。

コンテナに入れたいものを入れる

CentOS8になってから、システムが利用する python のバージョンも python3 系に上がっているため、phthon2 で書かれている yum が廃止せざるを得なくなった。 また、もともと yum にはいろいろ欠陥があり、dnf はそれを改善されるようになったこともあるらしい。

vim

# dnf install vim
~~(省略)~~
# # vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 11 2019 19:08:24)

nginx

# dnf install nginx
~~(省略)~~
Complete!

デフォルトでは80/tcpで開放されているため、コンテナ起動時にバウンドさせていた8080/tcpに変更する。

# ls -l  /etc/nginx/conf.d/default.conf ← ファイルが存在しないことを確認した
# cat << EOF > /etc/nginx/conf.d/default.conf
server {
    listen       8080;
    server_name  localhost;
}
EOF

nginxをコンテナ接続時に起動ようにする

# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

nginxを起動させる

# systemctl start nginx
# ps auxww | grep nginx
root         226  0.0  0.1 103328  2184 ?        Ss   12:31   0:00 nginx: master process /usr/sbin/nginx
nginx        227  0.0  0.3 122628  8004 ?        S    12:31   0:00 nginx: worker process
nginx        228  0.0  0.3 122628  8004 ?        S    12:31   0:00 nginx: worker process
root         230  0.0  0.0   9180   992 pts/0    S+   12:31   0:00 grep --color=auto nginx

コンテナ内でnginxサーバにアクセスしてみる

# curl -D - -s  -o /dev/null http://localhost
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sun, 18 Oct 2020 12:49:29 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
Connection: keep-alive
ETag: "5d9bab28-fd9"
Accept-Ranges: bytes

ホストOS(macOS)にてブラウザからコンテナのngnixサーバにアクセスしてみる http://localhost

画像

ついでに、index.html をいじってみる

# cd /usr/share/nginx/html
# mv index.html index.html.org
# cat << EOF > index.html
<html>
<body>
Hello, World!
</body>
</html>
EOF
# curl -i http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sun, 18 Oct 2020 15:11:37 GMT
Content-Type: text/html
Content-Length: 44
Last-Modified: Sun, 18 Oct 2020 15:11:06 GMT
Connection: keep-alive
ETag: "5f8c5b0a-2c"
Accept-Ranges: bytes

<html>
<body>
Hello, World!
</body>
</html>

python3

python3系だけあればいいかな。

# dnf install  python3 python3-pip
CentOS-8 - AppStream                                    5.0 kB/s | 4.3 kB     00:00
~~(省略)~~
Installed:
  platform-python-pip-9.0.3-16.el8.noarch
  python3-pip-9.0.3-16.el8.noarch
  python3-setuptools-39.2.0-5.el8.noarch
  python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64

Complete!
#
# python3 -V
Python 3.6.8

pip3でnumpy、matplotlib、scrapy、requestをインストールする。

# pip3 install numpy matplotlib scrapy requests
(省略)
# python3
Python 3.6.8 (default, Apr 16 2020, 01:36:27)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import scrapy
>>> import requests
>>> exit()

無事にインストールできたことが分かる。

mac -> ssh -> dockerできるようにする

docker側の設定

  • openssh-server をインストール
# dnf install openssh-server
...省略...
Installed:
  openssh-8.0p1-5.el8.x86_64                       openssh-server-8.0p1-5.el8.x86_64

Complete!
  • サービスを起動
# systemctl start sshd.service
  • sshd.serviceのステータスがアクティブ状態になっていることを確認
# systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-01-05 21:26:45 JST; 1min 49s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
...省略...
 105 21:26:45 af6b538f8bee systemd[1]: Starting OpenSSH server daemon...
...省略...
 105 21:26:45 af6b538f8bee systemd[1]: Started OpenSSH server daemon.
  • sshクライアントをインストール
# dnf install openssh-clients
...省略...
Installed:
  libedit-3.1-23.20170329cvs.el8.x86_64                 openssh-clients-8.0p1-5.el8.x86_64

Complete!

To be continured.

参考資料