LoginSignup
15

More than 3 years have passed since last update.

CentOS8にDockerをインストール。名前解決できなかったのが解消した。

Last updated at Posted at 2020-02-08

はじめに

さっき書いた ESXi6.7にCentOS8を最小構成で構築 の作業後に、dockerを入れたけど、dnfでこけてしまった。
pingでIP直打ちは外に通るのに、DNSで名前解決ができない。が解決したので、記録として残します。
コンテナ起動時に --net=host をやりたくなかったので情報をさがしてみました。

解決方法だけ記載

ホスト側で、NAPTの設定したら動いた。

# firewall-cmd --add-masquerade --permanent
# firewall-cmd --reload

前提条件

最小構成でインストールしたからでしょうか?nftablesは起動しておらず、firewalldが動作し、裏でiptablesが動いている状態でした。

nftablesは停止している。

# systemctl status nftables
● nftables.service - Netfilter Tables
   Loaded: loaded (/usr/lib/systemd/system/nftables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:nft(8)

firewalldが動いている。

# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-02-08 16:28:47 JST; 27min ago
     Docs: man:firewalld(1)
 Main PID: 1182 (firewalld)
    Tasks: 2 (limit: 23585)
   Memory: 38.9M
   CGroup: /system.slice/firewalld.service
           mq1182 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

iptablesが動いている。

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

dockerを使えるようにする

パッケージのインストール

インストール時のパッケージのバージョンではねられるので、--nobestをつけてインストールする。

# dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# dnf install --nobest docker-ce docker-ce-cli containerd.io

ついでなので、docker-composeもいれておく

curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

サービス設定しておく

# systemctl enable docker
# systemctl start docker

テストで hello-world を立ち上げてみる。
うまくいけば、下のように、 Hello from Docker! が表示される

# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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/

CentOS8のイメージで起動してみる

systemctl を動作させるために、 /sbin/init で走らせないとダメぽい。

# docker pull centos:centos8
# docker run --privileged -it -d --name centos8_check centos:centos8 /sbin/init
# docker exec -it centos8_check /bin/bash
[root@fced0781866f /]#

ログインできた。

トラブル発生

名前解決できない!

コンテナ内でepelパッケージのインストールしてみようとしたら、できない。

[root@fced0781866f /]# dnf install epel-release
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                                           0.0  B/s |   0  B     00:05
Failed to download metadata for repo 'AppStream'
Error: Failed to download metadata for repo 'AppStream'

コンテナ→GooglePublicDNSはIP直打ちで到達できる。

[root@fced0781866f /]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=5.60 ms

コンテナのDNSはLAN内のサーバを参照している

[root@fced0781866f /]# cat /etc/resolv.conf
# Generated by NetworkManager
search prosper2.net
nameserver 10.254.10.241

コンテナ→DNSへのpingは通る

[root@fced0781866f /]# ping 10.254.10.241
PING 10.254.10.241 (10.254.10.241) 56(84) bytes of data.
64 bytes from 10.254.10.241: icmp_seq=1 ttl=127 time=0.467 ms

のに、名前解決できない

[root@fced0781866f /]# ping dns.google
ping: dns.google: Name or service not known

なぜだ。。。

解消できた

ホスト側で、NAPTの設定したら動いた。

# firewall-cmd --add-masquerade --permanent
# firewall-cmd --reload

ちゃんと dnf install epel-release できた。

# dnf install epel-release
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                                           4.7 MB/s | 6.4 MB     00:01
CentOS-8 - Base                                                4.8 MB/s | 5.0 MB     00:01
CentOS-8 - Extras                                              6.2 kB/s | 2.1 kB     00:00
Dependencies resolved.
===============================================================================================
 Package                   Architecture        Version               Repository           Size
===============================================================================================
Installing:
 epel-release              noarch              8-5.el8               extras               22 k

Transaction Summary
===============================================================================================
Install  1 Package

Total download size: 22 k
Installed size: 30 k
Is this ok [y/N]: y
Downloading Packages:
epel-release-8-5.el8.noarch.rpm                                915 kB/s |  22 kB     00:00
-----------------------------------------------------------------------------------------------
Total                                                           36 kB/s |  22 kB     00:00
warning: /var/cache/dnf/extras-cbfb2f07b0021b7e/packages/epel-release-8-5.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - Extras                                              1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                       1/1
  Installing       : epel-release-8-5.el8.noarch                                           1/1
  Running scriptlet: epel-release-8-5.el8.noarch                                           1/1
  Verifying        : epel-release-8-5.el8.noarch                                           1/1

Installed:
  epel-release-8-5.el8.noarch

Complete!

追記

インストール時に --nobest でとりあえずは大丈夫だったけど、依存関係のエラーが出続けてしまっていた。

# dnf update
メタデータの期限切れの最終確認: 0:53:51 時間前の 2020年02月08日 16時38分36秒 に実施しました。
エラー:
 問題: package docker-ce-3:19.03.5-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
  - cannot install the best update candidate for package docker-ce-3:18.09.1-3.el7.x86_64
  - package containerd.io-1.2.10-3.2.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.3.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.el7.x86_64 is excluded
  - package containerd.io-1.2.4-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.5-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.6-3.3.el7.x86_64 is excluded
(インストール不可のパッケージをスキップするには、'--skip-broken' を追加してみてください または、'--nobest' を追加して、最適候補のパッケージのみを使用しないでください)

これは嫌なので、無理やりRPMを入れてしまおう。まず、どこにあるか調べる。

# grep stable /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable

そうか、そもそもCentOS7がターゲットなのか。。。
ここから拾ってこよう。

# dnf update https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.10-3.2.el7.x86_64.rpm
メタデータの期限切れの最終確認: 1:00:47 時間前の 2020年02月08日 16時38分36秒 に実施しました。
containerd.io-1.2.10-3.2.el7.x86_64.rpm                                                                                                 7.5 MB/s |  23 MB     00:03
依存関係が解決しました。
========================================================================================================================================================================
 パッケージ                                アーキテクチャー                   バージョン                                 リポジトリー                             サイズ
========================================================================================================================================================================
アップグレード:
 containerd.io                             x86_64                             1.2.10-3.2.el7                             @commandline                              23 M

トランザクションの概要
========================================================================================================================================================================
アップグレード  1 パッケージ

合計サイズ: 23 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                                                1/1
  scriptletの実行中: containerd.io-1.2.10-3.2.el7.x86_64                                                                                                            1/1
  アップグレード中 : containerd.io-1.2.10-3.2.el7.x86_64                                                                                                            1/2
  scriptletの実行中: containerd.io-1.2.10-3.2.el7.x86_64                                                                                                            1/2
  scriptletの実行中: containerd.io-1.2.0-3.el7.x86_64                                                                                                               2/2
  整理             : containerd.io-1.2.0-3.el7.x86_64                                                                                                               2/2
  scriptletの実行中: containerd.io-1.2.0-3.el7.x86_64                                                                                                               2/2
  検証             : containerd.io-1.2.10-3.2.el7.x86_64                                                                                                            1/2
  検証             : containerd.io-1.2.0-3.el7.x86_64                                                                                                               2/2

アップグレード済み:
  containerd.io-1.2.10-3.2.el7.x86_64

完了しました!

よしよし、もっかいupdateしよ。

# dnf update
メタデータの期限切れの最終確認: 1:01:08 時間前の 2020年02月08日 16時38分36秒 に実施しました。
依存関係が解決しました。
========================================================================================================================================================================
 パッケージ                           アーキテクチャー                  バージョン                                    リポジトリー                                サイズ
========================================================================================================================================================================
アップグレード:
 docker-ce                            x86_64                            3:19.03.5-3.el7                               docker-ce-stable                             24 M

トランザクションの概要
========================================================================================================================================================================
アップグレード  1 パッケージ

ダウンロードサイズの合計: 24 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
docker-ce-19.03.5-3.el7.x86_64.rpm                                                                                                      7.2 MB/s |  24 MB     00:03
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                                    7.2 MB/s |  24 MB     00:03
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                                                1/1
  scriptletの実行中: docker-ce-3:19.03.5-3.el7.x86_64                                                                                                               1/1
  アップグレード中 : docker-ce-3:19.03.5-3.el7.x86_64                                                                                                               1/2
  scriptletの実行中: docker-ce-3:19.03.5-3.el7.x86_64                                                                                                               1/2
  scriptletの実行中: docker-ce-3:18.09.1-3.el7.x86_64                                                                                                               2/2
/usr/bin/dockerd は dockerd の為の互換用として設定されていません。

  整理             : docker-ce-3:18.09.1-3.el7.x86_64                                                                                                               2/2
  scriptletの実行中: docker-ce-3:18.09.1-3.el7.x86_64                                                                                                               2/2
  検証             : docker-ce-3:19.03.5-3.el7.x86_64                                                                                                               1/2
  検証             : docker-ce-3:18.09.1-3.el7.x86_64                                                                                                               2/2

アップグレード済み:
  docker-ce-3:19.03.5-3.el7.x86_64

完了しました!

うん、大丈夫そう。

# dnf update
メタデータの期限切れの最終確認: 1:01:38 時間前の 2020年02月08日 16時38分36秒 に実施しました。
依存関係が解決しました。
行うべきことはありません。
完了しました!

よかった。

ELK

公式イメージで試してみる。

git clone https://github.com/elastic/stack-docker.git
cd stack-docker
docker-compose -f setup.yml up
docker-compose up -d

パスワードはセットアップ途中で表示されるものを利用。

setup_1  | setup_elasticsearch    | Elastic password is: [password]

出典

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
15