🐳

Dockerをコマンド1発インストール

2024/08/16に公開

一言で言うと

Linux系OSなら以下のコマンド1発でDockerをインストールできます。

curl https://get.docker.com | sudo sh

https://get.docker.com で配布されているDockerインストール用shellスクリプトが、OSを判断してそのOSに合った手順でインストールを実行してくれます。
でもDisributionによっては使えないものもあります(後述)。

Ubuntuの例

Ubuntuで手順を踏んでDockerをインストールしようとすると以下の流れになると思います。

  1. 事前準備で使うパッケージのインストール
  2. apt用キーの取得と登録
  3. aptリポジトリ追加
  4. apt update
  5. docker関連パッケージのインストール

前述のコマンドを使うとこれらを全自動で行ってくれます。Ubuntu 24.04で実行した結果が以下です。

$ curl https://get.docker.com | sudo sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21596  100 21596    0     0   382k      0 --:--:-- --:--:-- --:--:--  390k
# Executing docker install script, commit: 0d6f72e671ba87f7aa4c6991646a1a5b9f9dae84
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq ca-certificates curl >/dev/null
+ sh -c install -m 0755 -d /etc/apt/keyrings
+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" -o /etc/apt/keyrings/docker.asc
+ sh -c chmod a+r /etc/apt/keyrings/docker.asc
+ sh -c echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu noble 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 docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin >/dev/null
Scanning processes...
Scanning candidates...
Scanning linux images...
+ sh -c docker version
Client: Docker Engine - Community
 Version:           27.1.2
 API version:       1.46
 Go version:        go1.21.13
 Git commit:        d01f264
 Built:             Mon Aug 12 11:53:45 2024
 OS/Arch:           linux/arm64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.1.2
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.13
  Git commit:       f9522e5
  Built:            Mon Aug 12 11:53:45 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.7.20
  GitCommit:        8fc6bcff51318944179630522a095cc9dbf9f353
 runc:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

================================================================================

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/

================================================================================

簡単にできました。動くかどうか確かめてみます。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
478afc919002: Pull complete
Digest: sha256:53cc4d415d839c98be39331c948609b659ed725170ad2ca8eb36951288f81b75
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.
    (arm64v8)
 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/

動きました。

対応しているディストリビューション

インストールスクリプトを覗いてみると以下のディストリビューションに対応しているようです。

  • Ubuntu
  • RHEL
  • CentOS
  • Raspberry Pi OS
  • Debian
  • Fedora

対応していない場合

Alma Linuxで実行してみると以下のような結果になりました。

$ curl https://get.docker.com | sudo sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21596  100 21596    0     0   229k      0 --:--:-- --:--:-- --:--:--  226k
# Executing docker install script, commit: 0d6f72e671ba87f7aa4c6991646a1a5b9f9dae84

ERROR: Unsupported distribution 'almalinux'

こう言う場合は仕方がないので手順を踏んでインストールしましょう。

Discussion