Ubuntu に Docker と Docker Compose をインストールする

メモ:  Category:ubuntu

Superset に続き Redash や Metabase の準備として Docker をインストールします。

導入環境

  • Ubuntu 24.04 LTS

docker の apt リポジトリを登録する

最初にパッケージを更新しておきます。

$ sudo apt update
$ sudo apt upgrade

Docker のドキュメントに沿って、 インストールに必要なパッケージをインストールします。

$ sudo apt install ca-certificates curl

私の環境では、どちらもインストール済みでした。

ドキュメントでは、次のコマンドを使って GPG キー(公開鍵)を保管するディレクトリを作成するようですが、私の環境では適切な状態で作成済みでした。

存在しない場合は、次の install コマンドで指定したパーミッションで「/etc/apt/keyrings」ディレクトリが作成されます。

$ sudo install -m 0755 -d /etc/apt/keyrings

GPG キー(公開鍵)を取得します。

$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

Docker のリポジトリを登録します。

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

リポジトリの登録が完了たので、 apt update を実行して docker が増えていることを見ておきます。

$ sudo apt update
Get:1 https://download.docker.com/linux/ubuntu noble InRelease [48.8 kB]
Get:2 https://download.docker.com/linux/ubuntu noble/stable amd64 Packages [29.3 kB]
Hit:3 http://archive.ubuntu.com/ubuntu noble InRelease
・・・

Docker Engine をインストールする

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

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

インストールが完了したら動作確認を行ってみます。

$ sudo docker run hello-world

正常に動作していると、次のように表示されます。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:ec153840d1e635ac434fab5e377081f17e0e15afab27beb3f726c3265039cfff
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/

Proxy 環境で Docker を使うには

プロキシ環境で「 docker run hello-world 」を実行すると次のようなエラーが発生しました。

Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awa
iting headers)

Run 'docker run --help' for more information

Proxy サーバーを経由しないようでしたので、環境変数の設定を行います。

$ sudo systemctl edit docker

ユニットファイルへプロキシ設定を追加します。

[Service]
Environment="HTTP_PROXY=http://Proxy_server:port"
Environment="HTTPS_PROXY=http://Proxy_server:port"
Environment="NO_PROXY=localhost,127.0.0.1"

作成したユニットファイルを有効にして、実行します。

$ sudo systemctl daemon-reload
$ sudo systemctl show docker --property Environment
$ sudo systemctl restart docker
$ sudo systemctl status docker

Docker の再起動後動作確認を実施します。

$ sudo docker run hello-world

正しく表示されたら設定は完了です。

bluenote by BBB