LoginSignup
10
12

More than 5 years have passed since last update.

CentOS 7.3でDockerを使う

Posted at

CentOS7にDockerをインストールして、hello worldまでやってみました。
その際、少し引っかかったところがありましたので、メモとして残しておきます。

環境

  • OS: CentOS7

Dockerインストール

CentOS7ではリポジトリを追加せずに、そのままyumでインストールできます。
$ sudo yum -y install docker

Docker起動

$ sudo systemctl start docker
自動起動設定をする場合は、以下のコマンドも。
$ sudo systemctl enable docker

Docker実行

起動できたら動作確認をします。以下のコマンドを打つと、
$ docker ps

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

というエラーが出ます。

$ sudo docker ps
のように、sudoをつけてスーパーユーザ権限で毎回実行すれば問題ないのですが、面倒なので一般ユーザーでもdockerコマンドを実行できるようにしていきます。

一般ユーザ用の設定

  1. Dockerのグループ(dockerroot)にユーザを追加
    $ sudo gpasswd -a $(whoami) dockerroot

  2. /var/run/docker.sock のグループをdockerrootに変更
    $ sudo chgrp dockerroot /var/run/docker.sock


    【変更前】

    srw-rw----. 1 root root 0 6月 4 14:35 /var/run/docker.sock

    【変更後】

    srw-rw----. 1 root dockerroot 0 6月 4 14:35 /var/run/docker.sock


    これでsudoなしでdockerコマンドを使えるようになったのですが、dockerを再起動したりすると、グループの変更を再度行わなければなりません。
    それはちょっと面倒なので、dockerの設定ファイルを修正します。
  3. /etc/sysconfig/docker 修正
    3行目あたりの記述を修正します。
    $ sudo vi /etc/sysconfig/docker


    【修正前】

    OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false

    【修正後】

    OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false -G dockerroot'

    これでdockerを再起動しても、グループの変更をせずに一般ユーザでdockerコマンドを使えるようになります。

    準備が整ったので、あとは Hello world するのみです。

Docker で Hello World

以下コマンドで、hello worldしてみます。
$ docker run hello-world

Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
...
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
...
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

問題なく動きました!

参考

以下のサイトを参考にさせていただきました。
http://exlair.hatenablog.com/entry/install-docker-on-centos7

10
12
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
10
12