📚
【Ubuntu】Dockerをインストールする
UbuntuでDockerが使いたいのでインストールしてみます。
調べてみると、Docker の公式ドキュメント内に手順があったので、それ通りにやってみます
こちらのapt リポジトリを使用してインストールする方法をやっていきます
バージョン
バージョン | |
---|---|
Ubuntu | 22.04 |
apt リポジトリのセットアップ
インストールに必要なパッケージを準備
// パッケージの更新
$ sudo apt-get update
...
Fetched 6186 kB in 10s (630 kB/s)
Reading package lists... Done
// インストール
$ sudo apt-get install ca-certificates curl gnupg
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20230311ubuntu0.22.04.1).
ca-certificates set to manually installed.
curl is already the newest version (7.81.0-1ubuntu1.13).
curl set to manually installed.
gnupg is already the newest version (2.2.27-3ubuntu2.1).
gnupg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 37 not upgraded.
(すべてインストール済みでした。。。)
Docker の公式 GPG キーを追加
// GPGキーの格納先を作成
$ sudo install -m 0755 -d /etc/apt/keyrings
// GPGキーのダウンロードしてバイナリに変換して格納
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
// ダウンロードされたか確認
$ ll /etc/apt/keyrings/docker.gpg
-rw-r--r-- 1 root root 2760 Aug 27 20:10 /etc/apt/keyrings/docker.gpg
// 読み取り権限がなかったら、権限を追加
// 今回は権限があるため、未実行
$ sudo chmod a+r /etc/apt/keyrings/docker.gpg
リポジトリをセットアップ
// パッケージリポジトリの情報を追加
$ echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
// 実行結果を確認
$ ll /etc/apt/sources.list.d/docker.list
-rw-r--r-- 1 root root 112 Aug 27 20:18 /etc/apt/sources.list.d/docker.list
$ cat /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable
パッケージインデックスを更新
$ sudo apt-get update
...
Get:7 https://download.docker.com/linux/ubuntu jammy InRelease [48.9 kB]
Get:8 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages [21.4 kB]
Fetched 70.3 kB in 6s (12.6 kB/s)
Reading package lists... Done
Docker エンジンをインストール
Docker Engine、containerd、Docker Compose をインストール
今回は最新バージョンをインストールしていきます
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
...
Do you want to continue? [Y/n] Y
Get:1 https://download.docker.com/linux/ubuntu jammy/stable amd64 containerd.io amd64 1.6.22-1 [28.3 MB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libslirp0 amd64 4.6.1-1build1 [61.5 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/universe amd64 slirp4netns amd64 1.0.1-2 [28.2 kB]
...
No VM guests are running outdated hypervisor (qemu) binaries on this host.
// インストール確認
// バージョンが表示されればOK
$ docker -v
Docker version 24.0.5, build ced0996
コンテナを起動して動作確認
// docker daemon が起動しているか確認
$ sudo service docker status
* Docker is not running
---起動していなければ---------------------------
$ sudo service docker start
* Starting Docker: docker [ OK ]
$ sudo service docker status
* Docker is running
------------------------------------------------
// 動作確認
$ docker run --rm hello-world
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
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 9c7a54a9a43c 3 months ago 13.3kB
// 引数にIMAGE IDを指定してください
$ docker rmi 9c7a54a9a43c
Untagged: hello-world:latest
Untagged: hello-world@sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1
// 削除されていればOK
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
これで終わりです
参考文献
Discussion