安装 CentOS7 Docker + Docker Compose

这篇文章中提到的事情

在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 ~]

Docker的安装

下载适用于CentOS的Docker引擎-社区版。

删除旧的模块

如果之前已经安装了,请删除旧的模块。

[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组中。

Linux的安装后步骤

通过将用户添加到docker组中,可以在不使用sudo的情况下执行docker命令。
如果不进行此设置,只需根据需要自行执行sudo即可。
在以下示例设置中,我们将$USER(当前登录用户)添加为Docker管理员。
※更改组对当前登录用户不会生效。请在使用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 ~]$

请在LANG=C下确认,usermod -a的说明不会在LANG=ja_JP.UTF-8下显示。

确认动作

    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

安装Docker Compose

首先按照步骤进行安装。留意处理变量的方式。
然而,如果想要安装最新版本,可以参考 GitHub 网站确认最新版本的号码,然后用 curl 命令调整指定URL的版本号。

引入依赖包

只有阿尔派因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上确认版本,并将其更改为您想要的版本。

bannerAds