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

スポンサーリンク

便利なオープンソースなツールをインストールしようとしたとき、Dockerが前提になっていることが多くなってきました。ということでDockerとDocker ComposeをUbuntu20.04にインストールします。
といっても公式手順をコピペするだけです。

Linux への Docker Engine 導入ガイド — Docker-docs-ja 1.12.RC ドキュメント

Dockerをインストールするために必要なパッケージをインストールします。

sudo apt-get update
sudo apt-get install ca-certificates curl

公式のDockerリポジトリのGPGキーを追加します。

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

安定版のリポジトリに設定します。

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

システムを更新してDockerをインストールします。

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

更新によりDockerのリポジトリが追加されました。

root@ip-172-31-7-75:~# apt-get update
Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://packages.grafana.com/oss/deb stable InRelease
Get:5 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [15.5 kB]
Fetched 187 kB in 1s (306 kB/s)
Reading package lists… Done

root@ip-172-31-7-75:~# apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns
Suggested packages:
aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras
docker-scan-plugin pigz slirp4netns
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 96.4 MB of archives.
After this operation, 405 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 slirp4netns amd64 0.4.3-1 [74.3 kB]
Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.5.11-1 [22.9 MB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:20.10.14~3-0~ubuntu-focal [41.0 MB]
Get:5 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:20.10.14~3-0~ubuntu-focal [20.9 MB]
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:20.10.14~3-0~ubuntu-focal [7932 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-scan-plugin amd64 0.17.0~ubuntu-focal [3521 kB]
Fetched 96.4 MB in 2s (40.1 MB/s)
Selecting previously unselected package pigz.
(Reading database … 127271 files and directories currently installed.)
Preparing to unpack …/0-pigz_2.4-1_amd64.deb …
Unpacking pigz (2.4-1) …
Selecting previously unselected package containerd.io.
Preparing to unpack …/1-containerd.io_1.5.11-1_amd64.deb …
Unpacking containerd.io (1.5.11-1) …
Selecting previously unselected package docker-ce-cli.
Preparing to unpack …/2-docker-ce-cli_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce-cli (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-ce.
Preparing to unpack …/3-docker-ce_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack …/4-docker-ce-rootless-extras_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce-rootless-extras (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-scan-plugin.
Preparing to unpack …/5-docker-scan-plugin_0.17.0~ubuntu-focal_amd64.deb …
Unpacking docker-scan-plugin (0.17.0~ubuntu-focal) …
Selecting previously unselected package slirp4netns.
Preparing to unpack …/6-slirp4netns_0.4.3-1_amd64.deb …
Unpacking slirp4netns (0.4.3-1) …
Setting up slirp4netns (0.4.3-1) …
Setting up docker-scan-plugin (0.17.0~ubuntu-focal) …
Setting up containerd.io (1.5.11-1) …
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-ce-cli (5:20.10.14~3-0~ubuntu-focal) …
Setting up pigz (2.4-1) …
Setting up docker-ce-rootless-extras (5:20.10.14~3-0~ubuntu-focal) …
Setting up docker-ce (5:20.10.14~3-0~ubuntu-focal) …
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for man-db (2.9.1-1) …
Processing triggers for systemd (245.4-4ubuntu3.15) …

Dockerをroot以外のユーザーとして管理したいときはグループを作成してユーザーを追加する必要がありますが、rootで実行しているので省略します。
システム起動時にDockerが自動起動するようにします。

systemctl enable docker.service
systemctl enable containerd.service

次はDocker Composeをインストールします。

Compose のインストール — Docker-docs-ja 24.0 ドキュメント

同様にしてコピペします。
Docker Composeの最新版を指定したディレクトリにダウンロードします。

curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

root@ip-172-31-7-75:~# curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-uname -suname -m -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 5398 0 –:–:– –:–:– –:–:– 5398
100 25.2M 100 25.2M 0 0 41.4M 0 –:–:– –:–:– –:–:– 41.4M

最新バージョンはGitHubにあるのでリンクを確認して、バージョンの所を書き換えます。

Releases · docker/compose
Define and run multi-container applications with Docker - docker/compose

バイナリに対して実行権限を付与します。

chmod +x /usr/local/bin/docker-compose

バージョンを表示します。

docker-compose --version

root@ip-172-31-7-75:~# docker-compose –version
Docker Compose version v2.4.1

タイトルとURLをコピーしました