LoginSignup
17
14

More than 1 year has passed since last update.

Linuxサーバー環境のDockerインストールメモ

Last updated at Posted at 2022-01-05

背景

自分の各投稿でLinux環境でDockerをインストールする項目が毎回だと冗長なので切り分けて最新版に追随するメモ書きです。

Docker CEインストール

最新のDocker CEをインストールする場合はディストリビューションの標準パッケージではなく本家Dockerのリポジトリを利用します。

ubuntuの場合

[Install Docker Engine on Ubuntu]
(https://docs.docker.com/engine/install/ubuntu/)

$ sudo apt install ca-certificates curl gnupg lsb-release
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo \
  "deb [arch=$(dpkg --print-architecture) 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
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

CentOSの場合

[Install Docker Engine on CentOS]
(https://docs.docker.com/engine/install/centos/)

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum-config-manager --enable docker-ce-edge
$ sudo yum makecache fast
$ sudo yum install -y docker-ce
$ sudo systemctl start docker
$ sudo systemctl enable docker

Debianの場合

[Install Docker Engine on Debian]
(https://docs.docker.com/engine/install/debian/)

$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg-agent
$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce

Amazon Linux の場合

AWS EC2で利用されていてベースはRHEL/CentOSですが、Dockerは独自のExtrasからパッケージをインストールします。

$ sudo amazon-linux-extras install docker -y
$ sudo systemctl start docker
$ sudo systemctl enable docker
$ docker --version
Docker version 20.10.13, build a224086

ユーザーアカウントをdockerグループに追加

dockerコマンドを使用する為にdockerグループにユーザーアカウントを追加します。
有効化するのは再ログイン後なので一旦シェルをログアウトします。

$ sudo gpasswd -a [ユーザ名] docker
$ exit

Dockerの動作確認

$ docker --version
Docker version 20.10.17, build 100c701
$ 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/

Docker Composeのインストール

今日では「docker-compose-plugin」パッケージでインストールされるので個別に最新バージョンを使用するので無ければ別途不要。

Docker Composeには従来の1系と最新の2.0系があります。
インストールと動作に違いがあるので状況に応じて使用します。
一応インストールだけは両方で共存利用も出来ていますが、どちらかに合わせておかないとトラブルの元です。

docker compose 2.0

ユーザーディレクトリの直下に配置して実行権限を付与する形になりました。
セキュリティ的にもこの方が安全ですね。

$ mkdir -p ~/.docker/cli-plugins/
$ curl -SL https://github.com/docker/compose/releases/download/v2.8.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
$ chmod +x ~/.docker/cli-plugins/docker-compose

dockerコマンドのサブセットという形なので、以下のバージョン表示でもコマンドは「-(ハイフン)」がありません。

$ docker compose version
Docker Compose version v2.8.0

また自動付与されるコンテナ名等の区切り符号も「_(アンダースコア)」から「-(ハイフン)」に変わりました。

設定ファイル「docker-compose.yaml」の記述方法も変更されているので、以下のリンク先を参考にしています。

Docker Compose V2で変わったdocker-compose.ymlの書き方

docker-compose

従来の1系はPATHの通ったシステムディレクトリに実行ファイルをroot権限で配置しています。

$ sudo -i
# curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# exit

docker-composeコマンド実行例、バージョン表示

$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c
17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14