DebianにDockerをインストールするメモ

最近、Dockerを使用してサーバを構築する機会が多くなってきているため、メモする。

なお、他のディストリビューションへのDockerのインストールや使用方法について詳しく知りたい場合はこちらを参照すると良い。

Dockerのインストール

リポジトリを追加するための前処理

$ sudo apt update
$ # aptリポジトリをHTTPS経由で使用できるようにするものなどをインストール
$ sudo apt -y install apt-transport-https ca-certificates curl gnupg lsb-release
$ # リポジトリのGPGキーをインポートする
$ curl -fsSL https://download.docker.com/linux/debian/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/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ # キャッシュを更新し、docker関連のパッケージをインストールする
$ sudo apt update
$ sudo apt -y install docker-ce docker-ce-cli containerd.io

これでDockerのインストールは完了

後は動作確認

$ sudo docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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/

動作OK

docker-composeをインストール

以下のコマンドでdocker-composeをインストールすることができる。

ついでにコマンドの補完機能も追加する。

$ # docker-composeをインストール
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ # 以下は必要に応じて実行
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ # docker-composeのコマンドの補完機能をインストール (Bash)
$ sudo curl -L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

dockerグループにユーザを追加

dockerグループにユーザを追加すると、そのユーザの権限でDockerを操作することができるようになる

(sudo不要でdockerコマンドが実行できるようになる)

$ sudo usermod -aG docker <ユーザ名>

以上

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です