Docker:【Docker Engineインストール】実行手順
目次
はじめに
選択できるインストール手順
- 3つある
- ①aptのパッケージリストにDockerリポジトリを追加してインストールする
- 機能を色々追加する場合
- ②dpkgを使ってインストール
- アーキテクチャを指定できる
- 選べるアーキテクチャ
- amd64, armhf, arm64, s390x
- ③convenience script(shell)を使ってインストール
- DRY_RUN(お試し実行)してみたい場合
※通常インストールももちろんできる
- DRY_RUN(お試し実行)してみたい場合
- このページで紹介する手順は③
前提
- WSL2インストール
- Docker Enginが何なのか?
- Dockerのリリースチャネルについて
事前手順
要件確認
lsb_release -a
No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04 LTS Release: 20.04 Codename: focal
- 要件確認
- 次のいずれか
・Ubuntu Impish 21.10
・Ubuntu Hirsute 21.04
・Ubuntu Focal 20.04 (LTS)
・Ubuntu Bionic 18.04 (LTS)
- 次のいずれか
- 補足
- lsb_releaseがインストールされていない場合
-
【E: Unable to locate package】対処方法
※「ssh」を「lsb-release」に読み替え(「_」ではなくて「-」に注意)
古いバージョンがある場合の対処
アンインストール
sudo apt-get remove docker docker-engine docker.io containerd runc
- イメージやコンテナなどコンテンツ削除する場合は次項のコマンド
完全削除
sudo apt-get purge docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
- イメージやコンテナなどもすべて削除
- 削除が完了すればクリーンインストールが可能
Docker Engineインストール
curlのインストール
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB] Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1238 kB] Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [970 kB] Get:7 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [217 kB] Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [9560 B] Get:9 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [748 kB] Get:10 http://archive.ubuntu.com/ubuntu focal/main Translation-en [506 kB] Get:11 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [107 kB] Get:12 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [504 B] Get:13 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [676 kB] Get:14 http://archive.ubuntu.com/ubuntu focal/main amd64 c-n-f Metadata [29.5 kB] Get:15 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB] Get:16 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [115 kB] Get:17 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [13.1 kB] Get:18 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [20.7 kB] Get:19 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5196 B] Get:20 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [500 B] Get:21 http://archive.ubuntu.com/ubuntu focal/universe Translation-en [5124 kB] Get:22 http://archive.ubuntu.com/ubuntu focal/universe amd64 c-n-f Metadata [265 kB] Get:23 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [144 kB] Get:24 http://archive.ubuntu.com/ubuntu focal/multiverse Translation-en [104 kB] Get:25 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 c-n-f Metadata [9136 B] Get:26 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1572 kB] Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [302 kB] Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [14.7 kB] Get:29 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [801 kB] Get:30 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [114 kB] Get:31 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [500 B] Get:32 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [902 kB] Get:33 http://archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [200 kB] Get:34 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [20.1 kB] Get:35 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [23.7 kB] Get:36 http://archive.ubuntu.com/ubuntu focal-updates/multiverse Translation-en [7312 B] Get:37 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [580 B] Get:38 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [42.0 kB] Get:39 http://archive.ubuntu.com/ubuntu focal-backports/main Translation-en [10.0 kB] Get:40 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 c-n-f Metadata [864 B] Get:41 http://archive.ubuntu.com/ubuntu focal-backports/restricted amd64 c-n-f Metadata [116 B] Get:42 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [21.6 kB] Get:43 http://archive.ubuntu.com/ubuntu focal-backports/universe Translation-en [15.0 kB] Get:44 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [716 B] Get:45 http://archive.ubuntu.com/ubuntu focal-backports/multiverse amd64 c-n-f Metadata [116 B] Fetched 23.6 MB in 8s (2869 kB/s)
- aptのパッケージリスト更新
- パッケージインストール
- 次項でcurlを使用
convenience scriptのダウンロード
curl -fsSL https://get.docker.com -o get-docker.sh
ls -l get-docker.sh
-rw-rw-r-- 1 futa futa 18617 Feb 14 16:05 get-docker.sh
- リリーチャネルをテスト版にする場合
- 「get-docker.sh」を「test-docker.sh」に読み替え
- リリースチャネルについて
インストール実行
sudo sh get-docker.sh
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be
WSL DETECTED: We recommend using Docker Desktop for Windows.
Please get Docker Desktop from https://www.docker.com/products/docker-desktop
You may press Ctrl+C now to abort this script.
+ sleep 20
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | gpg --dearmor --yes -o /usr/share/keyrings/docker-archive-keyring.gpg
+ sh -c echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable" > /etc/apt/sources.list.d/docker.list
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends docker-ce-cli docker-scan-plugin docker-ce >/dev/null
+ version_gte 20.10
+ [ -z ]
+ return 0
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null
================================================================================
To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:
dockerd-rootless-setuptool.sh install
Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.
To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/
WARNING: Access to the remote API on a privileged Docker daemon is equivalent
to root access on the host. Refer to the 'Docker daemon attack surface'
documentation for details: https://docs.docker.com/go/attack-surface/
- インストールスクリプトが実行されるまで20秒sleepしている
- WARNINGについて
- Dockerを実行できるユーザを特定しておかないと、システムが脆弱になる
- 詳細
- 今回はお試しなので、そのまま進む
インストール後確認
Dockerサービス起動
プロセス確認
ps -ef|grep docker|grep -v grep
- 出力なし
- サービスは起動していないはずなので、出力なしでOK
サービス起動
sudo service docker start
* Starting Docker: docker [ OK ]
- サービス起動
- 上記WARNINGの指摘があるものの、今回はお試しなのでrootで起動した
- 通常はuserを指定するか、Docker専用userにsuして実行
サービス起動確認(プロセス確認)
ps -ef|grep docker|grep -v grep
root 10012 8 0 16:27 ? 00:00:00 /usr/bin/dockerd -p /var/run/docker.pid root 10022 10012 0 16:27 ? 00:00:01 containerd –config /var/run/docker/containerd/containerd.toml –log-level info
- サービス起動完了
- この例ではroot実行になっている
- 何度も書いてるけど、WARNINGの出力の通り、本来はrootじゃない方がいい
Docker実行
Hello World(サンプルコンテナ)が実行できるか確認
sudo 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/
- 結果確認
- 「Hello from Docker!」メッセージが出力されれば、成功
- Dockerが実行できる環境が整った
補足:初回に出るメッセージ
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a Status: Downloaded newer image for hello-world:latest
- Dockerイメージダウンロード
- 「hello-world」がなければ
- 「docker run」のタイミングで
次は、チュートリアルやってみよう!
- 参考記事の紹介
- Dockerできることを試すチュートリアル(基本編)
- 概要
- Docker HUBからダウンロードしたAlpine LinuxのDockerイメージを利用
- コンテナ実行した環境でLinuxのOSコマンドが実行できるか確認する
- また、実行が終わったコンテナがどうなるかも確認
リファレンス
- Install Docker Engine on Ubuntu | Docker Documentation
- Docker Engineインストール手順(Ubuntu)

