【Debian 10】Docker + Docker Composeのインストール

Debian

最近、Dockerを使う機会が増えてきたので、Debian 10にインストールする手順を記事にまとめてみました。

インストール

この記事では、Dockerの公式ドキュメントを参考に以下を進めていきます。

この手順ではsudoを使用していますが、表示のコマンドからsudoを抜いてrootユーザで実行してもインストール可能です。(sudoのインストールはコチラ

Docker

以下の手順でDockerをインストールします。

リポジトリの設定

パッケージインデックスを更新し、必要なパッケージをインストールしてHTTPS経由でリポジトリを使用出来るようにします。

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

Docker公式のGPGキーを追加します。

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

fingerprintを確認します。

$ sudo apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [  不明  ] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

stableリポジトリを使用するように設定します。

$ 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 Engineとcontainerdの最新バージョンをインストールします。

$ sudo apt update
$ sudo apt -y install docker-ce docker-ce-cli containerd.io

動作確認

動作確認のため、Dockerでhello-worldを実行します。Hello from 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:776b0895d5e2fcd5e80bcdd607adc45461ba11143ef3df531174bf00679f43fe
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/

一般ユーザでも実行出来るように設定

現状、rootユーザかsudoでしか実行出来ないので、一般ユーザでも実行出来るようにします。

一般ユーザ権限で実行すると以下のように、権限が無い旨のメッセージが表示されて実行出来ません。

$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

これを解決するためには、dockerコマンドを実行したいユーザをdockerグループへ追加します。

以下を実行して、再ログインすると一般ユーザでも実行出来るようになります。

$ sudo gpasswd -a flan docker
ユーザ flan をグループ docker に追加

$ exit

再ログイン

$ docker run --rm hello-world

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/

DockerCompose

以下の手順でDocker Composeをインストールします。

インストール

以下を実行して、DockerComposeの安定版リリースをダウンロードします。

$ 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

動作確認

以下を実行してインストールされたことを確認します。

$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c

インストールに成功していればバージョンが表示されます。

コメント

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