Docker入門:インストールから基本コマンドまで

Dockerはコンテナ仮想化技術を利用して、アプリケーションの開発・デプロイ・実行を効率化するためのツールです。Dockerを使用することで、簡単にアプリケーションの環境構築や配布が行えるようになり、開発者やシステムエンジニアにとって欠かせないツールの一つとなっています。

本記事では、UbuntuにDockerをインストールする方法と、基本的な使い方を解説します。

実行環境
  • Ubuntu 20.04.3 LTS (WSL2)

Dockerのインストール

  1. 必要なパッケージをインストールする
    sudo apt update
    sudo apt install ca-certificates curl gnupg lsb-release
  2. Docker の GPG 公開鍵をインストールする
    sudo mkdir -m 0755 -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  3. Dockerのrepositoryを追加する
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  4. apt を再度更新する
    sudo apt update
  5. Dockerをインストールする
    sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

参考

Dockerの起動と停止

Dockerの起動

sudo service docker start

Dockerの停止

sudo service docker stop

Dockerのサービス稼働情報を取得する

sudo service docker status

ユーザにDocker実行権限を追加する

Dockerコマンド実行時にsudoを入力しなくてよくなるよう、設定します。

sudo usermod -aG docker <username>

コマンド実行後、設定が反映されていない場合はUbuntuを再起動します。

イメージとコンテナについて

Dockerにはイメージコンテナという2つの概念があります。

イメージ

アプリケーションやその実行環境を定義する静的なファイルの集合です。Dockerfileというファイルを用いてイメージをビルドすることができます。イメージはレジストリに保存され、公開レジストリの「Docker Hub」では様々なイメージを取得できます。

コンテナ

イメージを実行するランタイム環境です。コンテナは、Dockerエンジンによって作成・管理されます。コンテナは、ホストシステムとは別の独立した環境を提供します。そして、複数のコンテナを組み合わせることで、マイクロサービスアーキテクチャを実現することもできます。

コンテナを動かしてみる

docker run hello-worldとコマンドを入力し、公開されているhello-worldイメージを使ったコンテナの起動を確認します。 以下は、コマンドの実行とその結果です。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
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 image ls
# または (旧コマンド)
docker images

# 出力例
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB

実行中のコンテナの確認

docker container ls
# または (旧コマンド)
docker ps

# 出力例
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

停止中も含めたコンテナの確認

docker container ls -a
# または (旧コマンド)
docker ps -a

# 出力例
CONTAINER ID   IMAGE          COMMAND     CREATED          STATUS                      PORTS    NAMES
cf48d522fb65   hello-world    "/hello"    3 minutes ago    Exited (0) 3 minutes ago             confident_heisenberg
コンテナの物理的な場所は/var/lib/docker/containers

イメージとコンテナの削除

イメージを利用しているコンテナ→イメージの順で削除を行います。

コンテナの削除

docker container rm [コンテナ名]|[コンテナID]
# または (旧コマンド)
docker rm [コンテナ名]|[コンテナID]
[コンテナID]は前方一致で特定できればOKなので、先頭数文字でも構いません。

イメージの削除

docker image rm [イメージ名]|[イメージID]
# または (旧コマンド)
docker rmi [イメージ名]|[イメージID]
このエントリーをはてなブックマークに追加
にほんブログ村 IT技術ブログへ

スポンサードリンク

関連コンテンツ

コメント

メールアドレスが公開されることはありません。 が付いている欄は必須項目です