docker

【簡単】Ubuntuにdockerをインストールする方法

どうも、katです。

今回は、Ubuntuにdockerをインストールする方法について書いていこうと思います。

インストール手順

古いdockerを削除

古いdockerが残っているとインストールがうまくいかない場合があるようなので、削除しておきます。(root以外のユーザーで実行する場合は適宜「sudo」をコマンドの前につけてください)

$ apt-get remove docker docker-engine docker.io containerd runc

ここで、これらのパッケージがインストールされていないといった警告が出ても特に問題ないです。

リポジトリ設定

次にリポジトリの設定を行なっていきます。

下記のコマンドを実行します。

$ apt-get update

$ apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

$ mkdir -p /etc/apt/keyrings

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

$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

dockerインストール

最後に、下記のコマンドでdockerをインストールします。

$ apt-get update

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

特定のバージョンのdockerに切り替え

dockerを特定のバージョンに変更したい場合は、まず以下のコマンドでバージョン一覧を表示させます。

$ apt-cache madison docker-ce

↓出力例
docker-ce | 5:20.10.17~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.16~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.15~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.14~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.13~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

上記の「5:20.10.17~3-0~ubuntu-jammy」のような部分がバージョンになります。

次に、上記の出力をもとに、変更したいバージョンを指定して変更します。

$ apt-get install docker-ce={バージョン} docker-ce-cli={バージョン} containerd.io docker-compose-plugin

動作確認

正常にインストールできたか、下記のコマンドで確認できます。

$ docker run hello-world

実行後、下記のような出力がされれば、問題なくインストールできています。

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

最後に

いかがだったでしょうか?

当初は、コマンド1つでインストールできるのかな?と思っていましたが、意外といくつかのコマンドを打つ必要がありました。

とはいえ、ほぼコピペで打ち込むだけで良いので、数分あればインストールできるかと思います。

今回は公式サイトに記載の内容とほぼ同じにはなりましたが、備忘録としても記事にしておきました。

以上、「Ubuntuにdockerをインストールする方法」でした〜

参考

https://docs.docker.com/engine/install/ubuntu/

ABOUT ME
kat
プログラマー歴7年、2歳の子供を持つパパです。 興味のあることはプログラミングや今後のIT技術などです。 趣味でオンラインカードゲームのサイトを運営しております。 プログラミングを通して社会に貢献していきたいです。