Dockerをインストール

先月VirtualBoxのインストールの記事を載せましたが、VirtualBoxのようにホストOS上に仮想マシンを作るハイパーバイザ型の仮想マシンではホストOSとゲストOSを分離できる分、イメージサイズが大きくなりがちでオーバヘッドが大きくなり動作が遅くなる欠点がありました

実際仕事でもこのvirtualboxでwebアプリ開発を行っているのですが

環境配布は簡単ですがイメージファイルがでか過ぎてコピーに時間がかかったり

PCのメモリとかが少ないと起動や処理スピードが遅いなどで結構不便を強いられてます。

今回は軽量だと謳っているコンテナ型のDockerをcentosにインストールしたのでそのメモを残しておきます。

 

まずは、レポジトリを追加します。

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

yumのパッケージインデックスを更新

    yum makecache fast

Dockerをインストール

    yum install docker-ce-17.12.0.ce-1.el7.centos
yum install docker-ce

サービスを起動

    systemctl start docker

以下のコマンドを実行し動作確認します。

    docker run hello-world

すると以下のようなメッセージを出力します。

    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    ca4f61b1923c: Pull complete
    Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
    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://cloud.docker.com/

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

これはDocker Hubからhello-worldイメージをダウンロードし、ダウンロードしたイメージからコンテナが 起動し、コンテナが標準出力したのち終了していることを表示しているようです。 確認出来たら、通常使うユーザをdockerのグループに登録します。

    sudo usermod -aG docker $USER

コマンドの前にsudoをいれたのは一般ユーザに戻って子のコマンドを実行したためです。rootユーザで直接ユーザを 指定してもよいともいます。 最後に、dockerをOS起動時に起動するように自動起動を有効にします。

    systemctl enable docker

これでインストールが完了したので実際にOSのイメージを入れて使ってみたいと思います。