Dockerとは
マシン上でシンプルで軽量な仮想環境(コンテナ)を構築できる。
OSレベルの仮想化なので、起動は一瞬。コンテナ間でイメージを共有できるので、コンテナの起動自体ではディスクは消費しない。
docker-comporseとは
複数のコンテナを利用して一つのサービスを提供する場合に yamlファイルで一括で構築出来るようにするツール。
環境
バージョン | |
---|---|
OS(CentOS) | 7.3.1611 (Core) |
Docker | 1.12.5 |
docker-compose | 1.9.0 |
Dockerインストール
epelリポジトリからyumでインストールできるけどバージョンが古い場合があるため、docker用のレポジトリを作成し、そこからインストールする。
レポジトリ作成
- OSのバージョン確認
# uname -r
3.10.0-514.2.2.el7.x86_64
※3.xx系であること、CentOS7は3.xxのため
- 最新版の状態へパッケージアップデートしておく
# yum update
- インストール状況確認
# yum list | grep docker
dockerがインストールされていないこと、最新版のdockerがインストールできないこと
- レポジトリ作成
# tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
- レポジトリ確認
# yum list | grep docker
※dockerrepoの出力があること
Docker パッケージをインストール
# yum install docker-engine
- Docker デーモンを開始
# systemctl start docker.service
- Dockerのバージョン確認
# docker -v
Docker version 1.12.5, build 7392c3b
- dockerが正常にインストールされたか確認するため、コンテナでテスト用イメージを実行
# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
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.
(Assuming it was not already locally available.)
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
For more examples and ideas, visit:
http://docs.docker.com/userguide/
dockerグループ作成
dockerコマンドはrootまたは、dockerグループに属するユーザでしか、実行できない。毎回sudoして実行してもよいが
面倒な場合はdockerグループを作成し、ユーザをグループに入れておく。
docker グループは root ユーザ相当。システム上のセキュリティに対する影響に注意。
rootまたはsudo特権のあるユーザでログインしておく。
- グループにdockerがないこと確認
# cat /etc/group | grep docker
※dockerがないこと
- dockerグループ作成
# groupadd docker
- 追加されたか確認
# cat /etc/group | grep docker
docker:x:988:
- ユーザをdockerグループに追加
$ sudo usermod -aG docker your_username
G:プライマリ以外に所属するグループを変更する
ログアウトしてから、再度ログイン。
sudo を使わずに docker が実行できることを確認します。
# docker run --rm hello-world
Dockerインストール時に「--skip-broken」に関するエラーが出力されたときの対処法
「--skip-broken」
に関するエラーが出力されてしまいインストールできないことがある。
「yum」はインストールやアップデート時などに自動的に依存性を解決するが依存性を解決できないと出力される。
原因として、他のレポジトリと衝突してしまうことと、以前どこかのタイミングでDockerをインストールしていることが原因のようです。
# yum list | grep docker | grep @
※インストールされているdockerに関するパッケージを表示
で、出力されたパッケージをremoveするとうまくいくはず
# yum remove hoge
※hogeパッケージを削除
docker-composeインストール
最新版を確認しておく
https://docs.docker.com/compose/install/
- curlにてインストール
# cd /usr/local/
※インストールパスに移動(どこにインストールしたいか各自判断)
# curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
コマンドを実行するために権限変更
# chmod +x /usr/local/bin/docker-compose
インストール確認のためのバージョン確認
# docker-compose --version
docker-compose version 1.9.0, build 2585387