MENU
  • VPS(レンタルサーバ)比較
  • ITエンジニアの転職
  • IT技術入門(Engineers.LOG)
カテゴリー
  • IT関連 (623)
    • Bootstrap (2)
    • クラウド (4)
    • サーバー技術 (185)
    • ストレージ (7)
      • NetApp (7)
    • ネットワーク技術 (91)
      • Cisco (36)
      • Juniper (18)
      • 負荷分散装置 (17)
    • パソコン関連 (92)
      • MAC (25)
      • Windows (54)
    • プログラミング (154)
      • Ansible (11)
      • C言語 (23)
      • Python (111)
        • Django (20)
    • 仮想化 (88)
      • Docker (63)
      • VMware (23)
  • VPS (200)
    • ABLENET VPS (16)
    • Amazon Lightsail(VPS) (2)
    • ConoHaVPS (37)
    • mixhostVPS (4)
    • WebARENAIndigo (16)
    • XServerVPS (34)
    • お名前.com VPS (12)
    • さくらのVPS (24)
    • カゴヤクラウドVPS (13)
    • シンVPS (7)
  • レンタルサーバー (56)
    • エックスサーバー (38)
  • 光回線 (8)
  • 技術スキルアップ開発・学習 (7)
    • Envader (2)
    • RareTECH (2)
    • Udemy (3)
もう現役20年超えましたー。経験値ならだれにも負けないエンジニア技術ブログ
インフラエンジニアの技術LOG
  • VPS(レンタルサーバ)比較
  • ITエンジニアの転職
  • IT技術入門(Engineers.LOG)
インフラエンジニアの技術LOG
  • VPS(レンタルサーバ)比較
  • ITエンジニアの転職
  • IT技術入門(Engineers.LOG)
  1. ホーム
  2. IT関連
  3. 仮想化
  4. Docker
  5. 【CentOS8】Dockerインストールと起動・SSHアクセスまで(無償版CE)

【CentOS8】Dockerインストールと起動・SSHアクセスまで(無償版CE)

2021 9/30
Docker
2020年11月15日2021年9月30日

CentOS8でDockerインストールを実施し、CentOS8のコンテナを構築・sshアクセスまでを構築したので手順を公開します。

CentOSのバージョンは以下となります。
2020年11月現在での最新バージョンです。

$ cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

※本構築環境はConoHaVPSを使用しています。

目次
安くて高速!高性能VPSで快適開発!
最速5分でサーバー構築
>>>こちらをチェック

エンジニア必見! 技術力を伸ばすVPS徹底比較
>>>VPS比較

CentOS8最新版でDockerインストールとDockerコンテナ(CentOS8:latest)を構築

Dockerインストール前に事前にアップデートを実施します。
※ここは「dnf -y update」、「dnf -y upgrade」でも問題ないです。

$ yum -y update
メタデータの期限切れの最終確認: 2:10:26 時間前の 2020年11月12日 00時18分44秒 に実施しました。
依存関係が解決しました。
行うべきことはありません。
完了しました!
$ yum -y upgrade
メタデータの期限切れの最終確認: 2:10:34 時間前の 2020年11月12日 00時18分44秒 に実施しました。
依存関係が解決しました。
行うべきことはありません。
完了しました!

公式リポジトリのインストール

Docker公式ページにて参照するリポジトリを設定します。

$ yum install -y yum-utils device-mapper-persistent-data lvm2
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

DockerCEインストール

以下、コマンドでDocker(CE・無償版)のインストールを実施します。

$ yum install -y docker-ce docker-ce-cli containerd.io

------中略------

インストール済み:
  container-selinux-2:2.124.0-1.module_el8.2.0+305+5e198a41.noarch     containerd.io-1.3.7-3.1.el8.x86_64
  docker-ce-3:19.03.13-3.el8.x86_64                                    docker-ce-cli-1:19.03.13-3.el8.x86_64
  libcgroup-0.41-19.el8.x86_64

完了しました!

Docker起動

上記で正常にインストールが成功した後は、Dockerサービスを起動します。

$ systemctl start docker
$ systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

以下コマンドでDockerが起動(active)していることを確認します。

$  systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-11-12 02:33:24 EST; 1min 17s ago
     Docs: https://docs.docker.com
 Main PID: 6288 (dockerd)
    Tasks: 10
   Memory: 41.4M
   CGroup: /system.slice/docker.service
           mq6288 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

11月 12 02:33:23 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:23.861959905-05:00" level=error msg="F>
11月 12 02:33:23 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:23.894579691-05:00" level=warning msg=>
11月 12 02:33:23 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:23.894826633-05:00" level=warning msg=>
11月 12 02:33:23 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:23.895081591-05:00" level=info msg="Lo>
11月 12 02:33:24 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:24.359352683-05:00" level=info msg="De>
11月 12 02:33:24 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:24.672645341-05:00" level=info msg="Lo>
11月 12 02:33:24 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:24.701736323-05:00" level=info msg="Do>
11月 12 02:33:24 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:24.702022626-05:00" level=info msg="Da>
11月 12 02:33:24 phpIPAM.fko.local dockerd[6288]: time="2020-11-12T02:33:24.733850712-05:00" level=info msg="AP>
11月 12 02:33:24 phpIPAM.fko.local systemd[1]: Started Docker Application Container Engine.

Dockerバージョンが最新版になっていることを確認します。

$ docker --version
Docker version 19.03.13, build 4484c46d9d

Docker動作確認

以下では、Dockerが正常にインストールをされているか確認をします。
デフォルトで設置されている「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:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
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/

「docker ps」コマンドで「hello-world」コンテナが起動していることが確認できます。

$  docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
915a533e3726        hello-world         "/hello"            41 seconds ago      Exited (0) 40 seconds ago                       compassionate_lumiere

Dockerコンテナ(CentOS8:最新版)を構築

コンテナの作成はこちらも参照してください。

あわせて読みたい
【Docker】CentOS7,8をコンテナ上で動作とSSHアクセスまで Dockerのコンテナ上でCentOS7を動かし、sshアクセスまでできる状態を構築します。 DockerのCentOSイメージでは、VMプレーヤーなどをインストールして動作するよりも、簡...

以下コマンドでCentOSの最新イメージをダウンロードします。

$ docker pull centos:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest             5e35e350aded        3 months ago        203MB

次に以下コマンドでCentOS最新版のコンテナの作成・起動を実行します。

$ docker run -itd --privileged -p 2222:22 --name centos8 --hostname centos8 centos:latest /sbin/init
5ef592aa81dd2e406fc5004335cbb83ced64e46a09cd3ec6fa1bc53011bd208a

以下コマンドで、Centosのコンテナが起動したことを確認します。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
5ef592aa81dd        centos:latest      "/bin/bash"         About a minute ago   Up About a minute                       centos8

CentOSのコンテナにSSHアクセスを実施する

上記で、CentOSの最新版のコンテナが作成できたら、以下コマンドでコンテナにアクセスします。

$ docker exec -it centos8 /bin/bash
[root@centos8 /]#

今回は「dnf」コマンドを使用して「openssh-server」をインストールします。

$ dnf -y update
$ dnf -y install openssh-server

正常にインストールが完了したら、sshdサービスを開始します。

[root@centos8 /]# systemctl start sshd.service
[root@centos8 /]# systemctl enable sshd.service

ステータスが起動状態になっていることを確認します。

[root@centos8 /]# 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 Sun 2020-11-15 05:12:15 UTC; 13s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 558 (sshd)
    Tasks: 1 (limit: 12530)
   Memory: 1.2M
   CGroup: /docker/e5f1dfc9eb80a6018d5dd1a9ad19c132173f3659aefaabeac3e2e0088c21c6a8/system.slice/sshd.service
           └─558 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes25>

Nov 15 05:12:15 centos8 systemd[1]: Starting OpenSSH server daemon...
Nov 15 05:12:15 centos8 sshd[558]: Server listening on 0.0.0.0 port 22.
Nov 15 05:12:15 centos8 sshd[558]: Server listening on :: port 22.
Nov 15 05:12:15 centos8 systemd[1]: Started OpenSSH server daemon.
lines 1-15/15 (END)

SSHアクセス用の「root」パスワード実施

SSHサービスを立ち上げてもアクセスはできますが、初期値はrootパスワードが設定されていないため、以下コマンドで「passwd」パッケージをインストールし、パスワードを設定します。

[root@centos8 /] dnf -y install passwd
[root@centos8 /]# passwd
Changing password for user root.
New password: 
/usr/share/cracklib/pw_dict.pwd.gz: No such file or directory
BAD PASSWORD: The password fails the dictionary check - error loading dictionary
Retype new password: 
passwd: all authentication tokens updated successfully.

ローカルPCからコンテナにSSHアクセス

最後に、ローカルから構築したコンテナへSSHアクセスができるか確認しましょう。
SSHアクセスのポートは、コンテナ作成時「2222」に設定しているため、sshでポート指定してアクセスします。

$ ssh -p 2222 root@localhost
root@localhost's password: 
Last login: Sun Nov 15 05:14:25 2020 from 172.17.0.1
[root@centos8 ~]# 

上記のようにコンテナアクセスができれば完了です。

エンジニアスキルをアップする勉強法

ITエンジニアの開発・検証・学習としてインターネット上で専用のサーバ(VPS)を利用しましょう!
実務経験はVPSで学べます。

あわせて読みたい
現役エンジニアがおすすめするVPSレンタルサーバ比較(高速SSD限定) 格安VPS比較(2025年最新版)

Docker
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする コメントをキャンセル

email confirm*

post date*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)



検索
カテゴリー
新着記事
  • XServerVPS アニメ風 男の子 プログラミングをしている VPSを利用している 楽しそうにしている
    XServerVPSで証明書(SSL)を取得する方法
    2025年3月24日
    XServerVPS
  • レンタルサーバ アニメ風 女の子 楽しんでパソコンをしている 目がキラキラ
    WordPressがレンタルサーバよりVPSで利用する方がおすすめの理由
    2025年3月19日
    VPS
  • VPS レンタルサーバー アニメ風 女の子 プログラミングをしている VPSを利用している 楽しそうにしている
    VPSとクラウドの違いとは?初心者向けにわかりやすく解説
    2025年3月5日
    VPS
  • シンVPS アニメ風 女の子 パソコン プログラミングをしている VPSを利用している 楽しそうにしている
    シンVPSはWindows serverを利用できない。その理由と利用できるOSは?
    2025年3月4日
    シンVPS
  • シンVPS アニメ風 女の子 パソコン プログラミングをしている VPSを利用している 楽しそうにしている
    シンVPSでゲームを利用する環境はあるのか
    2025年3月4日
    シンVPS
  • シンVPS アニメ風 女の子 パソコン プログラミングをしている VPSを利用している 楽しそうにしている
    シンVPSのサイトの表示速度は他社に比べて早いのか?
    2025年3月4日
    シンVPS
目次
目次