LoginSignup
10

More than 5 years have passed since last update.

CentOS 7にDocker Community Edition, Docker Composeをインストールする

Last updated at Posted at 2017-06-25

CentOS環境のDockerが今ではDocker CE(Community Edition) とDocker EE(Enterprise Edition)に分かれ、インストールの仕方も変更になったのでこの記事で纏めてみる。
原則公式ドキュメント準拠。

環境

  • さくらVPS
  • CentOS7

Docker CEのインストール

過去にインストールしたDocker関連のyumパッケージがあれば削除する

Docker CEやDocker EEになる前にDocker環境を構築していた場合には、過去にインストールしたパッケージを削除する。

$ sudo yum remove docker \
                  docker-common \
                  container-selinux \
                  docker-selinux \
                  docker-engine \
                  docker-engine-selinux

インストールしたかどうか自信がない場合、実行した結果何もアンインストールされなければ問題ない。

yumパッケージを追加する

Docker CEをyumリポジトリ追加するにあたり、事前に必要なyumパッケージを追加する。
yum-utilsはyum-config-managerを使う為に必要で、
device-mapper-persistent-dataとlvm2はdevicemapperを利用するのに必要。

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

yumリポジトリを追加する

yumリポジトリにDocker CEリポジトリを追加する。この操作をすることで、yum installでDocker CEパッケージがインストール可能になる。

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

補足: stable版でなくedge版を利用するには

デフォルトだとstable(安定)版がインストールされる設定になっている。
edge(最新)版をインストールしたい場合には、以下の操作で変更可能。

# edge版を利用したい場合
$ sudo yum-config-manager --enable docker-ce-edge

# stable版に戻したい場合
$ sudo yum-config-manager --disable docker-ce-edge

yum installでDocker CEをインストールする

晴れてyum installする環境が整ったのでインストールする。

# yumリポジトリのキャッシュ再生成
$ sudo yum makecache fast

# docker-ceのインストール
$ sudo yum install docker-ce

サービス起動

無事インストール完了したら、サービスの起動と自動起動の設定をする

# Dockerの起動
$ sudo systemctl start docker

# 自動起動するならenableする
$ sudo systemctl enable docker

試しにhello-worldしてみる

$ docker run 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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Docker Composeのインストール

あらかじめ、Releases · docker/compose から最新のVer.を確認しておく

# 一時的にrootになる
$ sudo -i

# 最新のバージョン番号を指定してcurlでダウンロードする(2017-06-25現在は1.14.0)
$ curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

# rootを抜ける
$ exit

# 実行権限を付与する
$ sudo chmod +x /usr/local/bin/docker-compose

# コマンドが実行できたら成功
$ docker-compose --version
docker-compose version 1.14.0, build c7bdf9e

これでDockerとDocker Composeが利用可能になった。

参考

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
10