CentOS7 - 最新版のNginxをインストール

目次

1.Yumリポジトリを作成
2.Nginxをインストール
3.Nginxの起動と自動起動設定

作業環境

# hostnamectl status
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
~
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.2.3.el7.x86_64
      Architecture: x86-64

1.Yumリポジトリを作成

Nginx用のリポジトリを作成する

# cat << EOF > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
EOF

2.Nginxをインストール

YumコマンドでNginxをインストール

# yum install -y nginx

バージョン確認

# nginx -V
nginx version: nginx/1.15.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

3.Nginxの起動と自動起動設定

systemctlコマンドでNginxの起動と自動起動の設定

# systemctl enable nginx
# systemctl start nginx

CentOS7 - Golangをインストールする

Golang開発環境構築

作業環境


# hostnamectl status
   Static hostname: golang.local
         Icon name: computer-vm
           Chassis: vm
~~~
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.2.2.el7.x86_64
      Architecture: x86-64

Cコンパイラをインストール


# yum install -y gcc

Golangをインストール


Golangのバイナリをダウンロード(2017/8/9 最新版は1.8.3)

# wget -P /usr/local/src https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz

展開

# tar xzf /usr/local/src/go1.8.linux-amd64.tar.gz -C /usr/local/

PATH登録と読み込み

# vim ~/.bash_profile
~
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin

環境変数を読み込み

# source ~/.bash_profile

Hello Worldスクリプトを作成


スクリプト作成

# cat << EOF > hello.go
package main

import "fmt"

func main() {
    fmt.Printf("Hello World\n")
}
EOF

コンパイルせずに実行

# go run hello.go
Hello World

コンパイル

# go build hello.go

バイナリを実行

# ./hello
Hello World

CentOS 7 - Gem Load Error is: Could not find a JavaScript runtime.

Gem Load Error is: Could not find a JavaScript runtime.

エラーメッセージ


# bundle exec rails s
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:85:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `<top (required)>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:82:in `require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:82:in `block (2 levels) in require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `each'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `block in require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `each'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler.rb:108:in `require'
/root/projects/e-shop/config/application.rb:7:in `<top (required)>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/root/projects/e-shop/bin/rails:9:in `require'
/root/projects/e-shop/bin/rails:9:in `<top (required)>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/root/projects/e-shop/bin/spring:15:in `require'
/root/projects/e-shop/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Bundler Error Backtrace:
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `each'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `block in require'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `each'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `require'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler.rb:108:in `require'
        from /root/projects/e-shop/config/application.rb:7:in `<top (required)>'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
        from /root/projects/e-shop/bin/rails:9:in `require'
        from /root/projects/e-shop/bin/rails:9:in `<top (required)>'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
        from /root/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
        from /root/projects/e-shop/bin/spring:15:in `require'
        from /root/projects/e-shop/bin/spring:15:in `<top (required)>'
        from bin/rails:3:in `load'
        from bin/rails:3:in `<main>'

解決策


# yum install -y epel-release && yum update && yum install --enablerepo=epel nodejs

Ubuntu 16.04 - LXD 2.0をインストールする

Ubuntu 16.04 - LXD 2.0をインストールする

参考サイト


作業環境


詳細

$ hostnamectl status
   Static hostname: lxd.local
         Icon name: computer-vm
           Chassis: vm
~
    Virtualization: microsoft
  Operating System: Ubuntu 16.04.3 LTS
            Kernel: Linux 4.4.0-96-generic
      Architecture: x86-64

事前準備


パッケージリストとパッケージを更新

$ sudo apt update && sudo apt list --upgradable && sudo apt upgrade -y

LXDインストール


aptを使い、LXD構築に必要なパッケージをインストール

$ sudo apt install -y lxd lxd-client zfsutils-linux bridge-utils

フューチャーブランチを利用する場合は、下記コマンドでインストール

$ sudo apt install -t xenial-backports lxd lxd-client zfsutils-linux bridge-utils

LXD/LXCのバージョンを確認

lxd --version/lxc --versionコマンドを実行

$ lxd --version
2.0.10
$ lxc --version
2.0.10

ブリッジインターフェースを作成


「/etc/network/interfaces」にブリッジインターフェースの以下設定を追加

$ sudo vim /etc/network/interfaces
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
  address 0.0.0.0

auto br0
iface br0 inet static
  address 172.16.2.100
  netmask 255.240.0.0
  network 172.16.0.0
  broadcast 172.31.255.255
  gateway 172.16.0.1
  dns-nameservers 8.8.8.8 8.8.4.4
  bridge_ports eth0
  bridge_stp off
  bridge_fd 0
  bridge_maxwait 0

ホスト機のリソース制限編集

以下設定を「limits.conf」の末尾に追記

$ sudo vim /etc/security/limits.conf
~~~
*               soft    nofile          1048576
*               hard    nofile          1048576
root            soft    nofile          1048576
root            hard    nofile          1048576
*               soft    memlock         unlimited
*               hard    memlock         unlimited

以下設定を「sysctl.conf」の末尾に追記

$ sudo vim /etc/sysctl.conf
~~~
fs.inotify.max_queued_events = 1048576
fs.inotify.max_user_instances = 1048576
fs.inotify.max_user_watches = 1048576
vm.max_map_count = 262144

再起動

$ sudo reboot

LXDセットアップ


セットアップ

lxd initコマンドを実行

$ sudo lxd init
Name of the storage backend to use (dir or zfs) [default=zfs]: zfs
Create a new ZFS pool (yes/no) [default=yes]? yes
Name of the new ZFS pool [default=lxd]: lxd
Would you like to use an existing block device (yes/no) [default=no]? yes
Path to the existing block device: /dev/vdb1
Would you like LXD to be available over the network (yes/no) [default=no]? yes
Address to bind LXD to (not including port) [default=all]:
Port to bind LXD to [default=8443]:
Trust password for new clients:
Again:
Do you want to configure the LXD bridge (yes/no) [default=yes]? yes

f:id:sabakan1204:20171008234714p:plain
ブリッジインターフェースのセットアップは不要なため「いいえ」を選択

f:id:sabakan1204:20171008234718p:plain
既存のブリッジインターフェースを利用するため「はい」を選択

f:id:sabakan1204:20171008234723p:plain
既存のブリッジインターフェース名を入力(本環境の場合、br0と入力)

シングルディスクの場合は、以下のようにzfsで利用する容量を指定

$ sudo lxd init
[sudo] ubuntu のパスワード:
Name of the storage backend to use (dir or zfs) [default=zfs]: zfs
Create a new ZFS pool (yes/no) [default=yes]? yes
Name of the new ZFS pool [default=lxd]: lxd
Would you like to use an existing block device (yes/no) [default=no]? no
Size in GB of the new loop device (1GB minimum) [default=17]: 60
Would you like LXD to be available over the network (yes/no) [default=no]? yes
Address to bind LXD to (not including port) [default=all]: all
Port to bind LXD to [default=8443]: 8443
Trust password for new clients:
Again:
Do you want to configure the LXD bridge (yes/no) [default=yes]? yes
Warning: Stopping lxd.service, but it can still be activated by:
  lxd.socket
LXD has been successfully configured.

コンテナイメージ取得


ローカルのコンテナイメージ一覧を取得

lxc image listコマンドを実行 はじめは、コンテナイメージは表示されないはず

$ lxc image list
クライアント証明書を生成します。1分ぐらいかかります...
初めて LXD を使う場合、sudo lxd init と実行する必要があります
初めてコンテナを起動するには、"lxc launch ubuntu:16.04" と実行してみてくだ
さい。

+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+

リモートのコンテナイメージを検索

lxc image list images: ディストリビューション名コマンドを実行 (下記では、リモートのCentOSを検索した)

$ lxc image list images: centos
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
|         ALIAS          | FINGERPRINT  | PUBLIC |           DESCRIPTION           |  ARCH  |  SIZE   |          UPLOAD DATE          |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/6 (3 more)      | f2cdaec19821 | yes    | Centos 6 amd64 (20170919_02:16) | x86_64 | 75.51MB | Sep 19, 2017 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/6/i386 (1 more) | 3721b3e53702 | yes    | Centos 6 i386 (20170919_02:16)  | i686   | 75.72MB | Sep 19, 2017 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/7 (3 more)      | 35d54c491da1 | yes    | Centos 7 amd64 (20170919_02:29) | x86_64 | 82.22MB | Sep 19, 2017 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+

コンテナ作成・一覧取得


コンテナ作成

lxc launch images:イメージ名 コンテナ名を実行 以下では、CentOS7 64bitのイメージを選択してvm01を作成する場合 ※初回はリモートからコンテナイメージを取得するのに時間がかかる

$ lxc launch images:centos/7/amd64 vm01
vm01 を作成中
vm01 を起動中

【注意点】本作業環境では、仮想化にHyper-Vを利用している。コンテナ作成時にDHCPIPアドレスが取得できない場合は、Hyper-Vマネージャーから該当VMネットワークアダプターで「MACアドレスのスプーフィングを有効にする」にチェックすること。

コンテナ一覧を取得

lxc listを実行

$ lxc list
+------+---------+----------------------+-----------------------------------------------+------------+-----------+
| NAME |  STATE  |         IPV4         |                     IPV6                      |    TYPE    | SNAPSHOTS |
+------+---------+----------------------+-----------------------------------------------+------------+-----------+
| vm01 | RUNNING | 10.199.90.135 (eth0) | fde1:6be4:be7c:2a5c:216:3eff:fef4:21c2 (eth0) | PERSISTENT | 0         |
+------+---------+----------------------+-----------------------------------------------+------------+-----------+

コンテナ操作


コンテナ停止

lxc stop コンテナ名で該当コンテナを停止

$ lxc stop vm01

lxc listコマンドで、コンテナ停止を確認

$ lxc list
+------+---------+------+------+------------+-----------+
| NAME |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+------+---------+------+------+------------+-----------+
| vm01 | STOPPED |      |      | PERSISTENT | 0         |
+------+---------+------+------+------------+-----------+

コンテナ起動

lxc start コンテナ名で該当コンテナを起動

$ lxc start vm01

コンテナにログイン

lxc exec コンテナ名 bashコマンドを実行

$ lxc exec vm01 bash
[root@vm01 ~]#

コンテナ削除

lxc delete コンテナ名コマンドを実行

$ lxc delete vm01

コンテナイメージの作成

$ lxc publish vm01 --alias CentOS7
コンテナは以下のフィンガープリントで publish されます: 7b25b19d8b75fff2dea225bcd6d9580579b0e033f889147405afaf2f810c5d5b

作成したコンテナイメージを確認

$ lxc image list
+---------+--------------+--------+---------------------------------+--------+----------+-------------------------------+
|  ALIAS  | FINGERPRINT  | PUBLIC |           DESCRIPTION           |  ARCH  |   SIZE   |          UPLOAD DATE          |
+---------+--------------+--------+---------------------------------+--------+----------+-------------------------------+
| CentOS7 | 7b25b19d8b75 | no     |                                 | x86_64 | 167.98MB | Sep 19, 2017 at 12:56pm (UTC) |
+---------+--------------+--------+---------------------------------+--------+----------+-------------------------------+
|         | 35d54c491da1 | no     | Centos 7 amd64 (20170919_02:29) | x86_64 | 82.22MB  | Sep 19, 2017 at 6:59am (UTC)  |
+---------+--------------+--------+---------------------------------+--------+----------+-------------------------------+

CentOS7 - Dockerイメージを作る(Dockerfile)

環境

OS: CentOS Linux 7 (Core)
VCPU: 2
MEM: 2GB
Docker API Ver: 1.29

Dockerのインストールがまだの場合は、下記を参考にインストール。
DockerをCentOS7に30分でインストールする

手順

Dockerfileを作成
Dockerfileをビルドしてイメージ作成
Dockerイメージからコンテナ起動
DockerコンテナのNginxにアクセス

Dockerfileを作成

1.「Dockerfile」と「hello.html」を作成します。
下記では、CentOS7のDockerイメージを元にNginxをインストールしてプロセス起動する内容を記述しています。

# vim Dockerfile
FROM centos:centos7
RUN yum update -y && \
    yum install -y epel-release && \
    yum upgrade -y && \
    yum install -y nginx

ADD hello.html /usr/share/nginx/html/

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

2.HTMLファイルを作成

# echo 'Hello!!' > hello.html

3.ディレクトリ構造を確認

# tree
.
|-- Dockerfile
`-- hello.html

Dockerfileをビルドしてイメージ作成

# docker build -t nginx:centos7 .

Dockerイメージからコンテナ起動

1.イメージの一覧を取得

# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
nginx                                           centos7             e9c983020f72        6 seconds ago       400MB

2.Dockerコンテナを起動

# docker run -t -p 80:80 -d nginx:centos7
11fb0be9e4360e5092754b85ba4d1301a3210b5627dc54163596df21d3cc1341

3.Dockerコンテナの状態を確認

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
11fb0be9e436        nginx:centos7       "nginx -g 'daemon ..."   5 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp

DockerコンテナのNginxにアクセス

CurlコマンドでNginxにアクセスしてHello!!と表示があれば終わり。

# curl -s 127.0.0.1/hello.html
Hello!!

Drone.io オンプレ版をCentOS7に30分でインストールする

環境

OS: CentOS Linux 7 (Core)
VCPU: 2
MEM: 2GB
Docker API Ver: 1.29

手順

1.Dockerをインストール
2.Drone.io Ver 0.7 のDockerイメージを取得
3.GitHubと連携
4.Drone.ioを起動
5.GitLabと連携
6.GitHub Enterpriseと連携

1.Dockerをインストール

Yumでインストール可能なDockerバージョンでは、古いので下記を参考Dockerをインストールする。

CentOS7にdockerとdocker-composeの最新版をインストールする

2.Drone.io Ver 0.7 のDockerイメージを取得

Dockerイメージを取得。

# docker pull drone/drone:0.7
0.7: Pulling from drone/drone
a3ed95caeb02: Pull complete
802d894958a2: Pull complete
64d43428256a: Pull complete
Digest: sha256:625a4f8db1c9c344517dc1de4519302f8be33b90b38b6dda58a796b8741e3fab
Status: Downloaded newer image for drone/drone:0.7

Dockerイメージの一覧を表示

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
drone/drone         0.7                 c58de76939e9        3 weeks ago         25.7MB

3.GitHubと連携

まず、Developer applicationsで[Register a new application]を選択。

下記項目を設定。
x.x.x.xの部分は、Drone.ioをインストールしたサーバーIPアドレスまたはドメイン名を指定する。
登録完了後、[Client ID]と[Client Secret]が取得できる。

【Application name】Drone.io
【Homepage URL】http://x.x.x.x
【Authorization callback URL】http://x.x.x.x/authorize

.envを作成する。

# vim .env

内容

DRONE_SECRET=hogehgoe
DRONE_GITHUB_CLIENT=xx3ce1f35xxx0
DRONE_GITHUB_SECRET=45eaaae5103xxxxxxxxxxxxxx3d9b3eab3a79

docker-compose.ymlを作成する。

# vim docker-compose.yml

内容

version: '2'

services:
  drone-server:
    image: drone/drone:0.7
    ports:
      - 80:8000
    volumes:
      - ./drone:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_GITHUB=true
      - DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT}
      - DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET}
      - DRONE_SECRET=${DRONE_SECRET}

  drone-agent:
    image: drone/drone:0.7
    command: agent
    restart: always
    depends_on:
      - drone-server
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=ws://drone-server:8000/ws/broker
      - DRONE_SECRET=${DRONE_SECRET}

4.Drone.ioを起動

カレントディレクトリに.envdocker-compose.ymlがあるか確認します。

# tree -a
.
|-- .env
`-- docker-compose.yml

Docker ComposeでDrone.ioを起動する。

# docker-compose up -d
Creating droneio_drone-server_1 ...
Creating droneio_drone-server_1 ... done
Creating droneio_drone-agent_1 ...
Creating droneio_drone-agent_1 ... done

ブラウザからGitHubに登録した【Homepage URL】アクセスする。

http://x.x.x.x

5.GitLabと連携

まず、GitLabにSgin inして[User Settings]->[Applications]を選択。

下記項目を設定。
y.y.y.yの部分は、Drone.ioをインストールしたサーバーIPアドレスまたはドメイン名を指定する。
登録完了後、[Application Id]と[Secret]が取得できる。

【Name】Drone.io
【Redirect URI】http://y.y.y.y/authorize
【Scopes】api,read_userを選択

Applications

.envを作成する。
x.x.x.xは、GitLabをインストールしたサーバを指定する。

# vim .env

内容

DRONE_SECRET=hogehoge
DRONE_GITLAB_URL=http://x.x.x.x
DRONE_GITLAB_CLIENT=xxx69012cbfdae1c9e5e5050bfexxxxxxxxxxxxxxd87dadca1efade9cf54xxxx
DRONE_GITLAB_SECRET=xxxxx351601bxxxxxxxxxxxxxxxxx52b2339ed5608ee8ac3e4d57688xxxxxxxx

docker-compose.ymlを作成する。

# vim docker-compose.yml
version: '2'

services:
  drone-server:
    image: drone/drone:0.7
    ports:
      - 80:8000
    volumes:
      - ./drone:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_GITLAB=true
      - DRONE_GITLAB_URL=${DRONE_GITLAB_URL}
      - DRONE_GITLAB_CLIENT=${DRONE_GITLAB_CLIENT}
      - DRONE_GITLAB_SECRET=${DRONE_GITLAB_SECRET}
      - DRONE_SECRET=${DRONE_SECRET}

  drone-agent:
    image: drone/drone:0.7
    command: agent
    restart: always
    depends_on:
      - drone-server
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=ws://drone-server:8000/ws/broker
      - DRONE_SECRET=${DRONE_SECRET}

6.GitHub Enterpriseと連携

まず、GitLabにSgin inして[User Settings]->[Applications]を選択。

下記項目を設定。
y.y.y.yの部分は、Drone.ioをインストールしたサーバーIPアドレスまたはドメイン名を指定する。
登録完了後、[Application Id]と[Secret]が取得できる。

[Settings]->[OAuth applications]と進み、[Register a new application]を選択。

【Application name】Drone.io
【Homepage URL】http://y.y.y.y
【Authorization callback URL】http://y.y.y.y/authorize

.envを作成する。 x.x.x.xは、GitLabをインストールしたサーバを指定する。

# vim .env

内容

DRONE_SECRET=hogehoge
DRONE_GITHUB_URL=https://x.x.x.x
DRONE_GITHUB_CLIENT=xx1edeaa9c5xxxxd0x
DRONE_GITHUB_SECRET=xxxe2605exxxxxxxx0d551c6b8f1bbaxxxxx

docker-compose.ymlを作成する。

# vim docker-compose.yml
version: '2'

services:
  drone-server:
    image: drone/drone:0.7
    ports:
      - 80:8000
    volumes:
      - ./drone:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_GITHUB=true
      - DRONE_GITHUB_SKIP_VERIFY=true
      - DRONE_GITHUB_PRIVATE_MODE=false
      - DRONE_GITHUB_URL=${DRONE_GITHUB_URL}
      - DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT}
      - DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET}
      - DRONE_SECRET=${DRONE_SECRET}

  drone-agent:
    image: drone/drone:0.7
    command: agent
    restart: always
    depends_on:
      - drone-server
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=ws://drone-server:8000/ws/broker
      - DRONE_SECRET=${DRONE_SECRET}

CentOS7 - Dockerイメージを作る(Dockerfileなし)

環境

OS: CentOS Linux 7 (Core)
VCPU: 2
MEM: 2GB
Docker API Ver: 1.29  

Dockerのインストールがまだの場合は、コチラをみてください。 DockerをCentOS7に30分でインストールする

手順

1.Dockerイメージをダウンロード
2.Dockerコンテナを起動
3.Dockerイメージに接続
4.Dockerイメージを編集
5.Dockerイメージを保存

1.Dockerイメージをダウンロード

ベースとなるDockerイメージをダウンロード。

[root@localhost ~]# docker pull centos:centos7

Dockerイメージを確認。

[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        centos7             3bee3060bfc8        5 days ago          192.5 MB

2.Dockerコンテナを起動

[root@localhost ~]# docker run --rm -itd centos:centos7 /bin/bash

起動中のDockerコンテナを確認。

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7906cb8f9bb1        centos:centos7      "/bin/bash"         11 seconds ago      Up 10 seconds                           prickly_pare

3.Dockerイメージに接続

[root@localhost ~]# docker attach 7906cb8f9bb1
[root@7906cb8f9bb1 /]#

4.Dockerイメージを編集

dockerイメージにRubyとGitをインストール。

[root@7906cb8f9bb1 /]# yum -y update
[root@7906cb8f9bb1 /]# yum install -y git ruby ruby-devel

Dockerイメージから抜けるときは、Ctrl+P,Ctrl+Qを入力。

5.Dockerイメージを保存

[root@localhost ~]# docker commit -m 'Ruby 2.0' 7906cb8f9bb1 centos7:Ruby2.0

Dockerイメージを確認。

[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
git                     latest              31db81031059        12 seconds ago      338.2 MB