9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
< 前の記事

MariaDB のインストール

次の記事 >

Docker で Nginx のコンテナの使用

Linux Docker

[Linux]Docker のインストール

公開日:2025-11-30
更新日:2025-11-30

1. 概要

Docker をインストールします。

2. Docker のインストール

パッケージの更新
コマンド
sudo dnf update

Docker をインストールできるようにするため、Docker 用のリポジトリを追加します。
コマンド
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Docker のインストール
コマンド
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
docker-ce : Docker 本体
docker-ce-cli : コマンドラインインターフェース
containerd.io : コンテナランタイム。Docker の実行に必要
docker-compose-plugin : 「docker-compose」ではなく「docker compose」を使えるようにする。

3. 現在のユーザを Docker グループに追加

sudo を付けずに Docker を操作できるようにするため、ユーザを docker グループに追加します。

コマンド
sudo usermod -aG docker $USER  # ログイン中のユーザをグループに追加する
または
sudo usermod -aG docker admin  # admin をグループに追加する
「-aG」は、セカンダリグループを維持したままグループに追加します。
「-G」にすると、セカンダリグループが指定したグループだけになるため、注意してください。

追加後、所属しているグループを確認します。
コマンド
id $USER  # ログイン中のユーザ
または
id admin  # admin ユーザ
実績結果
uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel),994(docker)
ユーザ名が admin のユーザ ID は 1000、
グループ名が admin のグループ ID は 1000、
所属しているグループは、admin、wheel、docker、
であることを示しています。

一度、ターミナルからログアウトしてログインすると、sudo なしで docker を操作できるようになります。

3. Docker の起動・停止・自動起動

コマンド
     systemctl status  docker  # ステータスの確認
sudo systemctl start   docker  # 起動
sudo systemctl stop    docker  # 停止
sudo systemctl restart docker  # 再起動

sudo systemctl enable  docker  # 自動起動
sudo systemctl disable docker  # 自動起動の解除

4. Docker の基本的なコマンド

コマンド
docker images  # コンテナイメージ一覧の表示

docker pull { イメージ名 }               # Docker Hub からコンテナイメージの取得
docker rmi  { イメージ名 | イメージID }  # コンテナイメージの削除

docker ps      # コンテナ一覧の表示
docker ps -a   # コンテナ一覧の表示(停止中のコンテナも含む)
docker ps -a --no-trunc   # コンテナ一覧の表示(コンテナIDを省略しない)

docker run      { イメージ名 }                   # イメージからコンテナの作成と起動
docker run -d   { イメージ名 }                   # コンテナがバックグラウンドで起動するため、ターミナルの操作が可能
docker run --name { コンテナ名} { イメージ名 }   # コンテナに名前を付けて起動
docker rm       { コンテナID | コンテナ名 }      # コンテナの削除

docker start    { コンテナID | コンテナ名 }  # コンテナの起動
docker start -a { コンテナID | コンテナ名 }  # コンテナの起動(標準出力あり)
docker stop     { コンテナID | コンテナ名 }  # コンテナの停止

docker logs     { コンテナID | コンテナ名 }

5. hello-world を使った例

hello-world はメッセージを表示するだけのコンテナイメージです。

5.1 イメージの取得

コマンド
docker pull hello-world
実行結果
Using default tag: latest
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:54e66cc1dd1fcb1c3c58bd8017914dbed8701e2d8c74d9262e26bd9cc1642d31
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

5.2 イメージ一覧の表示

コマンド
docker images
コマンド
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    1b44b5a3e06a   2 months ago   10.1kB
イメージ一覧に hello-world が追加されています。

5.3 コンテナの実行

コマンド
docker run hello-world
実行結果
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/

5.4 コンテナ一覧の表示

コマンド
docker ps
実行結果
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
hello-world は実行すると停止するため、「docker ps」では表示されません。

停止中のコンテナを表示する場合は、-a を付けます。
コマンド
docker ps -a
実行結果
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
e618726d769c   hello-world   "/hello"   10 minutes ago   Exited (0) 10 minutes ago             determined_heyrovsky
determined_heyrovsky は、コンテナに自動的に付けられた名前です。
docker run の時に「--name」で名前を付けることも可能。
コンテナID は省略されて表示されています。
「sudo docker ps -a --no-trunc」で省略していないコンテナIDを確認できます。

5.5 停止中のコンテナの実行

コマンド
docker start -a e618726d769c          # コンテナ ID(一部)を指定して実行
または
docker start -a determined_heyrovsky  # コンテナ名を指定して実行
実行結果
Hello from Docker!
(省略)
-a を付けるとログが画面に表示されます。

5.6 コンテナの削除

コマンド
docker rm e618726d769c
「sudo docker ps -a」で表示されなくなります。

5.7 イメージの削除

コマンド
docker rmi hello-world   # イメージ名を指定して実行
または
docker rmi 1b44b5a3e06a  # イメージ ID(一部)を指定して実行
< 前の記事

MariaDB のインストール

次の記事 >

Docker で Nginx のコンテナの使用

YouTube X

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲