LoginSignup
30
31

More than 3 years have passed since last update.

CentOS7 Docker + Docker Compose のインストール

Last updated at Posted at 2020-01-25

この記事に書かれている事

CentOS7 への Docker 及び Docker-compose のインストール

  • SeLinuxの無効化
    いろいろと制限が出るから無効化しちゃいましょう。
  • Dockerのインストール
  • 動作確認
  • Docker Composeのインストール

しばらく触らないと忘れちゃうので備忘録として、、、

SeLinuxの無効化

  • コマンドで監視モードに変更
  • 再起動後は無効化されるように設定ファイルを変更
[user01@cent7 ~] getenforce
Enforcing
[user01@cent7 ~] sudo setenforce 0
[user01@cent7 ~] getenforce
Permissive
[user01@cent7 ~] sudo vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
##SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[user01@cent7 ~]

Dcokerのインストール

参考)
Get Docker Engine - Community for CentOS

古いモジュールの削除

以前インストールしていたら古いモジュールを削除しましょう。

[user01@centos7 ~]$ sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine \
> docker-engine-selinux
[user01@centos7 ~]$ sudo rm /etc/yum.repos.d/docker.repo

必要な関連パッケージをインストール

[user01@centos7 ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Dockerのインストール

  • Docker リポジトリを追加
  • Docker インストール
[user01@centos7 ~]$ sudo yum-config-manager \
--add-repo https://download.docker.com/linux/centos/docker-ce.repo
[user01@centos7 ~]$ sudo yum install -y docker-ce docker-ce-cli containerd.io

Docker管理者をdockerグループに追加

参考)
Post-installation steps for Linux

グループ docker に参加させることで sudo せずに dockerコマンドを実行できるようになります。
この設定をしなくてもsudoすれば良いだけなので必要に応じて任意で実行してください。
以下の設定例では $USER (ログイン中のユーザ) をDocker管理者に追加しています。
※ログイン中のユーザにはgroupの変更は反映されません。docker の操作前に再ログインしてください。

[user01@centos7 ~]$ LANG=C man usermod
[user01@centos7 ~]$ sudo usermod -aG docker $USER
 [sudo] user01 のパスワード:
[user01@centos7 ~]$ id $USER
uid=1000(user01) gid=1000(user01) groups=1000(user01),10(wheel),980(docker)
[user01@centos7 ~]$

usermod -a の説明は LANG=ja_JP.UTF-8 では出てきません。
LANG=Cで確認してください。

動作確認

  • Docker の起動
  • Docker 自動起動設定
  • 動作確認
[user01@centos7 ~]$ sudo systemctl start docker
[user01@centos7 ~]$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[user01@centos7 ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 土 2019-03-09 12:16:43 JST; 17s ago
     Docs: https://docs.docker.com
 Main PID: 12648 (dockerd)
   CGroup: /system.slice/docker.service
           mq12648 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

 3月 09 12:16:42 centos7 dockerd[12648]: time="2019-03-09T12:16:42.470344031+09:00" level=info msg="[graphdriver] using prior storage driver: overlay"
 3月 09 12:16:42 centos7 dockerd[12648]: time="2019-03-09T12:16:42.470393144+09:00" level=warning msg="[graphdriver] WARNING: the overlay st... release"
 3月 09 12:16:42 centos7 dockerd[12648]: time="2019-03-09T12:16:42.474175363+09:00" level=info msg="Graph migration to content-addressabilit... seconds"
 3月 09 12:16:42 centos7 dockerd[12648]: time="2019-03-09T12:16:42.475348040+09:00" level=info msg="Loading containers: start."
 3月 09 12:16:43 centos7 dockerd[12648]: time="2019-03-09T12:16:43.076457928+09:00" level=info msg="Default bridge (docker0) is assigned wit... address"
 3月 09 12:16:43 centos7 dockerd[12648]: time="2019-03-09T12:16:43.342253505+09:00" level=info msg="Loading containers: done."
 3月 09 12:16:43 centos7 dockerd[12648]: time="2019-03-09T12:16:43.372317576+09:00" level=info msg="Docker daemon" commit=774a1f4 graphdrive...n=18.09.3
 3月 09 12:16:43 centos7 dockerd[12648]: time="2019-03-09T12:16:43.372423919+09:00" level=info msg="Daemon has completed initialization"
 3月 09 12:16:43 centos7 dockerd[12648]: time="2019-03-09T12:16:43.398960579+09:00" level=info msg="API listen on /var/run/docker.sock"
 3月 09 12:16:43 centos7 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[user01@centos7 ~]$ 
[user01@centos7 ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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:

Docker Composeのインストール

参考)
Install Docker Compose

とりあえず手順通りにインストール。変数の扱いに注意。
ただ、最新版をインストールしたい場合 github を参照し最新版のversionを確認して curl で指定する URLのバージョン番号を調整しましょう。

依存パッケージの導入

alpine Linux の場合しか書いてないのでとりあえずスルー

Docker Compose のインストール

すごく簡単です。

  • github のファイルをダウンロード
  • 実行権限を付与
  • 必要なら Link を作成
Install Docker Compose に書いてるコマンドサンプル(失敗する場合下のコマンドを試してください)
[user01@cent7 ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

上のコマンドが失敗した場合、こっちのコマンドを試してください
[user01@cent7 ~]$ sudo curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0   1354      0 --:--:-- --:--:-- --:--:--  1356
100 16.4M  100 16.4M    0     0  1328k      0  0:00:12  0:00:12 --:--:-- 1676k
[user01@cent7 ~]$ sudo chmod +x /usr/local/bin/docker-compose
[user01@cent7 ~]$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

上記のコマンドで指定している"1.27.4"は最新のリリースでは無い事が考えられます。
最新バージョンなどサンプルと異なるバージョンを導入する場合、github.com/docker/compose/releases でバージョンを確認して、導入したいバージョンに書き換えてください。

30
31
1

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
30
31