LoginSignup
89
104

More than 1 year has passed since last update.

LinuxにDockerをインストールする

Last updated at Posted at 2018-08-04

まえがき

Ubuntu 18.04、lubuntu 18.04とCentOS 7.5にdockerをインストールする方法をメモしておきます。それぞれのディストリビューションのリポジトリからdockerパッケージをインストールする方法もあるみたいですが、ここではDockerの公式のリポジトリを登録してそこからdocker-ceパッケージをインストールする方法を取ります。基本的にDocker公式サイトを参考にしています。
Ubuntu22.04についてはこちらに書きました。

環境

  • Ubuntu 18.04 and lubuntu 18.04 and CentOS 7.5
  • Docker 18.06.1、18.09.2

古いバージョンの削除

もし今までにDockerをインストールしたことがあって削除したい場合は削除しておきます。

  • Ubuntu and lubuntu

    ~# apt-get remove docker docker-engine docker.io
    
  • centOS

    ~# yum remove docker \
                    docker-client \
                    docker-client-latest \
                    docker-common \
                    docker-latest \
                    docker-latest-logrotate \
                    docker-logrotate \
                    docker-selinux \
                    docker-engine-selinux \
                    docker-engine
    

dockerリポジトリの設定

準備

必要なパッケージをインストールします。

  • Ubuntu and lubuntu

    ~# apt install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
    
  • CentOS

    ~# yum install yum-utils device-mapper-persistent-data lvm2
    

GPG鍵の入手

  • Ubuntu

    ~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key  add -
    

リポジトリの登録

リポジトリを登録した後、リポジトリの設定ファイルが作成されたことを確認しています。

  • Ubuntu and lubuntu

    ~# add-apt-repository \
        "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
        $(lsb_release -cs) \
        stable"
    
    ~$ cat /etc/apt/sources.list | grep docker
    deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
    # deb-src [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
    
  • centOS

    ~# yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    
    ~$ ls /etc/yum.repos.d/docker-ce.repo 
    /etc/yum.repos.d/docker-ce.repo
    

Dockerのインストール

docker-ceパッケージをインストールすればよいのですが、インストールするバージョンを指定できるらしいので、寄り道してやってみました。

インストール可能なdocker-ceのバージョンを調べる。

  • Ubuntu and lubuntu

    ~$ apt-cache madison docker-ce
    docker-ce | 18.06.1~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
    docker-ce | 18.06.0~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
    docker-ce | 18.03.1~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
    
  • centOS

    ~$ yum list docker-ce --showduplicates | sort -r
    利用可能なパッケージ
    読み込んだプラグイン:fastestmirror
    docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
    docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
    docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
    docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
    

Dockerのインストール

バージョンを指定してインストールしています。インストール後dockerコマンドを実行してインストールできたか確認をしています。

  • Ubuntu and lubuntu

    ~# apt install docker-ce=18.06.1~ce~3-0~ubuntu
    
    ~$ docker --version
    Docker version 18.06.1-ce, build e68fc7a
    
  • CentOS

    ~# yum install docker-ce-18.06.1.ce-3.el7
    
    ~$ docker --version
    Docker version 18.06.1-ce, build e68fc7a
    

    インストール途中で、リポジトリの鍵をインポートするか聞かれるのでyと答えてインポートする。

Dockerの実行

Dockerデーモンの起動

  • Ubuntu and lubuntu

    デフォルトでDockerデーモンはactive(running)になっています。

    ~$ systemctl status docker
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
    Active: active (running) since Mon 2018-09-17 11:54:00 JST; 3min 40s ago
        Docs: https://docs.docker.com
    Main PID: 17466 (dockerd)
    ...
    
  • CentOS

    デフォルトでDockerデーモンはinactiveになっているので、Dockerデーモンを起動する。起動後にステータスを確認してactive(running)になっていればOK

    ~# systemctl start docker # Dockerデーモンを起動する
    
    ~$ systemctl status docker # Dockerデーモンの状態を確認する
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
    Active: active (running) since 月 2018-09-17 11:12:01 JST; 3s ago
        Docs: https://docs.docker.com
    Main PID: 1654 (dockerd)
        Tasks: 20
    Memory: 46.1M
    ...
    

一般ユーザでDockerを実行できるようにする

Dockerを実行するたびにsudoをしなくて済むように一般ユーザをdockerグループに所属させます。

  • Ubuntu、lubuntu、CentOS共通

    初期状態ではdockerグループには誰も所属していません。usermodコマンドでユーザyoshiをdockerグループに所属させると、dockerグループにyoshiが追加されたことが確認できます。一度ログアウトしなければ設定が反映されないためログアウトします。

    ~$ getent group | grep docker # dockerグループに所属するユーザを確認する
    docker:x:994:
    
    ~# usermod -aG docker yoshi # dockerグループにユーザyoshiを追加する
    
    ~$ getent group | grep docker # dockerグループに所属するユーザを確認する
    docker:x:994:yoshi
    
    ~$ exit
    

Dockerの実行

再ログイン後にdocker runコマンドで動作確認を行います。"Hello from Docker!"が表示されていればOK

  • Ubuntu、lubuntu、CentOS共通

    $ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    d1725b59e92d: Pull complete 
    Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
    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:
    https://docs.docker.com/get-started/
    
89
104
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
89
104