LoginSignup
0
0

More than 1 year has passed since last update.

Docker環境構築

Posted at

はじめに

Dockerのインストールについて忘却録として残しておきます。

概要

仮想マシンを作成しその上でDockerを動かします。
すべてroot権限で実行していきます。

環境構成

  • OS : CentOS Stream 8 (kernel 4.18.0-305.3.1.el8)
  • Docker : Community 20.10.7

手順

  • OSインストール (省略)
  • OS設定
  • Dockerインストール

OS設定

考慮するのが面倒なためfirwalldとSELinuxを無効化しておきます。
必要に応じて実施してくださ。

# systemctl disable firewalld --now
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

# setenforce 0
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

Dockerインストール

公式手順に従ってインストールしていきます。
インストールの動作確認は hello-world コンテナーを事項して確認します。
初めての実行となるためコンテナーイメージのダウンロードが行われますが、2回目以降はダウンロードは行われずローカルに保存されているイメージより実行されます。

リポジトリインストール

# dnf install -y yum-utils
(省略)
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
repo の追加: https://download.docker.com/linux/centos/docker-ce.repo

Dockerエンジンインストール

# dnf install docker-ce docker-ce-cli containerd.io
(省略)

Docker起動・自動起動設定

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

テスト用コンテナー起動

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

hello-worldイメージは Hello from Docker! を表示させるためだけのイメージのためこの表示で完了になります。

よく使うオプションと解説

オプション 解説 実行例
run Dockerイメージからコンテナーの生成と起動 docker run <イメージ名>
run [オプション] 起動と同時に実施する内容を指定
-it : コンソール出力
--name : コンテナー名指定
-d : バックグラウンド実行
-p <ホストポート>:<コンテナーポート> : ポート転送設定
--net : デフォルトのブリッジからホストIPを使用する設定等
-e <環境変数>=<設定値> : コンテナー内の環境変数を設定
-v <ホストディレクトリ>:<コンテナディレクトリ> ホストのディレクトリをコンテナー内にマウント
docker run -it <イメージ名>
stats コンテナーの動作状況確認 docker status <コンテナ名>
exec 実行中のコンテナー内で新たなコマンドを実行
-itオプションを使用してBashを起動すればDockerのコンソールに入れます
※attachオプションと違ってBashプロセスを新たに起動させて接続する
docker exec -it <コンテナー名> /bin/bash
container ls 実行中のコンテナー一覧を表示します
停止中のコンテナーも表示させる場合は -a オプションを付けてください
docker container ls
container start 停止中のコンテナーを起動
※containerは省略可能
docker container start <コンテナー名>
container restart 稼働中のコンテナーを再起動
※containerは省略可能
docker container restart <コンテナー名>
container stop 稼働中のコンテナーを停止
※containerは省略可能
docker container stop <コンテナー名>
container rm 停止中のコンテナーの削除
-fオプションを付けると稼働中のコンテナーも削除できます
※containerは省略可能
docker container rm <コンテナー名>
images ローカルにダウンロード済みのイメージ一覧を表示 docker images
rmi Dockerイメージを削除
※稼働中のコンテナーで使用されているイメージは削除できません
docker rmi <イメージ名>

引用

0
0
0

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
0
0