Ubuntu ServerにDockerを導入する

前回インストールした Ubuntu Server に Docker をインストールします。
以前、「Debian 11にDockerをインストール」にてDebian 環境に Docker をインストールしましたが基本的な作業は一緒です。

前提条件

インストール手順は Docker Engine インストール(Ubuntu 向け)にあるので、その通り行えば簡単です。
今回のインストールもGUI環境ではないため Docker Desktop for Linux はインストールしません。

Docker Engine をインストールするには、以下に示す Ubuntu の 64 ビットバージョンのいずれかが必要です。
日本語の Docs は記述が古く、最新のドキュメントでは下記のようになっています。

・Ubuntu Mantic 23.10
・Ubuntu Jammy 22.04 (LTS)
・Ubuntu Focal 20.04 (LTS)

古いバージョンがインストールされている場合は下記のコマンドを実行します。

$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

インストール手順

今回もDockerのリポジトリを用いたセットアップを行います。

リポジトリのセットアップ

最初に Docker の 公式GPG鍵をインストールします。

$ sudo apt-get update
$ sudo apt-get install ca-certificates curl
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc

次に リポジトリを apt ソースに追加します。

$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update

Docker Engine のインストール

最新版の Docker をインストールします。

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

自ユーザをグループ docker に追加して sudo を使用しないで済むようにします。

$ sudo gpasswd -a $USER docker

上記、グループ追加後は再ログインしてください。

動作確認

Docker Engine が正しくインストールされていることを確認します。

手始めにバージョンの確認。

$ docker version
Client: Docker Engine - Community
 Version:           25.0.2
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        29cf629
 Built:             Thu Feb  1 00:22:57 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          25.0.2
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       fce6e0c
  Built:            Thu Feb  1 00:22:57 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

お決まりの hello-world を動作させます。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:4bd78111b6914a99dbc560e6a20eab57ff6655aea4a80c50b0c5491968cbc2e6
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 images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   9 months ago   13.3kB

動作確認が出来たので、要らないので削除します。
説明は省略しますね。

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
276668f38c80   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             vigorous_solomon

$ docker rm 276668f38c80
276668f38c80

$ docker rmi d2c94e258dcb
Untagged: hello-world:latest
Untagged: hello-world@sha256:4bd78111b6914a99dbc560e6a20eab57ff6655aea4a80c50b0c5491968cbc2e6
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
Deleted: sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e

$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

本日はここまで…

Linux

前の記事

Ubuntu Serverの日本語化