Dockerの簡単なインストールのやり方

Docker
スポンサーリンク

今回は、UbuntuにDockerをインストールする方法を紹介します。

スポンサーリンク

Dockerの古いバージョンをアンインストールする

Dockerをインストールする前に古いバージョンのDockerをアンインストールします。以下のコマンドを実行してアンインストールしてください。

$ sudo apt-get remove docker docker-engine docker.io container runc

これでアンインストールができました。

Dockerをインストールする

Dockerをインストールしていきます。

リポジトリを設定

必要なパッケージをインストールしていきます。まず、システムをアップデートして最新状態にしておきます。

$ sudo apt update

必要なパッケージをインストールします。

$ sudo apt install \
       apt-transport-https \
       ca-certificates \
       curl \
       gnupg \
       lsb-release

GPGキーを追加します。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

最後にリポジトリを設定します。

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

Docker Engineをインストールする

まず、アップデートをして最新状態にします。

$ sudo apt update

そして必要なパッケージをインストールします。

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

インストールが終われば完了です。正しく動作しているか確認するために「hello-worldイメージ」を実行して確認します。

$ sudo docker run hello-world

Docker をroot以外で使えるようにする

初期のままではDockerを利用するときrootでないと実行できません。それだと不便なので他のユーザーでも実行できるようにしていきます。
dockerのグループを作成してユーザーを追加します。
最初にDockerグループを作ります。

$ sudo groupadd docker

次にユーザーをDockerグループに追加します。

$ sudo usermod -aG docker $USER

そして以下のコマンドも実行します。

$ newgrp docker

これで完了です。実際にsudoなしで実行することができます。試しに以下のコマンドを実行してみて下さい。

$ docker run hello-world

実行できると思います。

まとめ

今回はDockerのインストールについて解説しました。基本的にDockerの公式サイトに記載されていることを自分なりにまとめました。参考にしたサイトを以下に貼っておきます。

Install Docker Engine on Ubuntu
Jumpstart your client-side server applications with Docker Engine on Ubuntu. This guide details prerequisites and multip...
Linux post-installation steps for Docker Engine
Find the recommended Docker Engine post-installation steps for Linux users, including how to run Docker as a non-root us...

コメント

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