LoginSignup
3
8

More than 5 years have passed since last update.

ConoHa VPS(CentOS7.5)にDockerCEとDocker Composeをインストール〜Django導入までの道程〜

Posted at

前回はConoHaVPSの初期設定をやりました。
ConoHaのVPSを初期設定。sshログイン後ユーザ作成、rootログイン禁止、port変更まで

今回は、Django環境を作る前提としてにDockerで仮想環境を作ります。

CentOSのシステムを最新にアップデート

CentOSを最新の状態にしておきます。

// 最新にする
$ sudo yum -y update

Dockerのインストール準備

Get Docker for CentOS - Docker Documentation
日本語版

必要なパッケージのインストール

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

開発版の最新バージョンが使えるedge や testing リポジトリは無効のままにしておく。

DockerCE本体のインストール

$ sudo yum install docker-ce

Docker リポジトリを追加後、パッケージ一覧の更新が初めての場合は、 GPG 鍵を受け入れるかどうかの確認と、鍵のフィンガープリント(fingerprint;指紋)を表示します。フィンガープリントが正しいものであると確認したら、鍵を受け入れます。鍵のフィンガープリントが 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 と一致するのを確認します。

Running transaction
  Installing : 2:container-selinux-2.68-1.el7.noarch                        1/3 
setsebool:  SELinux is disabled.
  Installing : libtool-ltdl-2.4.2-22.el7_3.x86_64                           2/3 
  Installing : docker-ce-18.06.1.ce-3.el7.x86_64                            3/3 
  Verifying  : docker-ce-18.06.1.ce-3.el7.x86_64                            1/3 
  Verifying  : libtool-ltdl-2.4.2-22.el7_3.x86_64                           2/3 
  Verifying  : 2:container-selinux-2.68-1.el7.noarch                        3/3 

Installed:
  docker-ce.x86_64 0:18.06.1.ce-3.el7                                           

Dependency Installed:
  container-selinux.noarch 2:2.68-1.el7   libtool-ltdl.x86_64 0:2.4.2-22.el7_3  

Complete!

今回は、18.06.1がインストールされたようです。
指定バージョンがインストールしたければ、ドキュメント参照

Dockerの起動

$ sudo systemctl start docker

正常に動作するか確認のため、以下のコマンドで、テストイメージがプルされ、コンテナ内で走ります。

$ sudo 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.
    (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.

Dockerが自動で起動するように設定しておきます。

// 自動起動の設定
$ sudo systemctl enable docker

Docker Composeのインストール

docker-composeを使うと複数コンテナの管理が便利に
なるらしいので、Docker Composeも入れる。
Docker Compose 公式Docs

インストール手順はこちら

Docker Composeのインストール(※URLは変わるので注意)
以下のコマンドだと、Version1.22.0を
/usr/local/bin/docker-compose
にインストールしてる。

$ sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
//パーミッションの変更
$ sudo chmod +x /usr/local/bin/docker-compose

インストール後のオプションで、Dockerの使用を簡単にする。(root権限なしでの起動)

上記手順でインストールしただけの現状だと、

$ docker run hello-world

を走らせると、root権限がないので

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

と拒否される。
ここでは、公式のオプションインストールDocどおりにユーザグループを作ってroot権限なしでも起動できるようにする。

$ sudo groupadd docker

と思ったらすでにグループがあると拒否られたw

$ sudo groupadd docker
[sudo] password for $: 
groupadd: group 'docker' already exists
//確認
$ cat /etc/group | grep docker
docker:x:993:

ということで、このグループにユーザを追加することにする。

// newuserというユーザーをdockerグループに追加
$ sudo usermod -aG docker newuser

-aオプション(--appendオプション):副グループを追加する
補助グループをカンマ区切りのリストで追加するオプションで、必ず-Gオプションと併用する。
// 所属グループ確認
$ groups newuser
tetsuya : newuser wheel docker
// docker再起動
$ sudo systemctl restart docker

※SSH接続している場合、一度ログアウトが必要らしい。

//ログアウト
$ exit

再度ログインして以下実行

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

sudoなしで実行できました!

では引き続き本番、Djangoを入れていこうと思います。

3
8
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
3
8