2014年11月26日水曜日

dockerのコマンド

コマンド一覧の表示

コマンド一覧を表示するときは「docker」だけ入力します。
$ docker

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code


イメージの入手

dockerのサーバからイメージをローカルに持ってきます。
$ docker pull NAME[:TAG]
実行例
$ sudo docker pull centos:latest


イメージの一覧表示

イメージ一覧の表示コマンドです。
$ docker images


イメージの削除

イメージIDを指定して削除します。
$ docker rmi IMAGE [IMAGE...]


コンテナの起動

入手したイメージを起動します。
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 実行例
ubuntuを立ち上げてCLIにログインしたいときの実行例です。
$ docker run --name ubuntu -p 80:80 -i -t ubuntu:14.04 /bin/bash
CLIからデタッチするときは「 Ctrl-p Ctrl-q 」を入力します。

コンテナの確認

起動しているコンテナを確認する
$ docker ps

全てのコンテナを確認する
$ docker ps -a


コンテナの削除

停止しているコンテナを削除する
$ docker rm コンテナID

2014年11月18日火曜日

dockerでCentOSを使ってみる

Dockerの公開コンテナを見てみるとCentOSがあるようだ。
https://registry.hub.docker.com

最新のCentOSイメージをダウンロードする

$ sudo docker pull centos:latest
Pulling repository centos
ae0c2d0bdc10: Download complete
511136ea3c5a: Download complete
5b12ef8fd570: Download complete

イメージを確認してみる

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              ae0c2d0bdc10        13 days ago         224 MB
無事にダウンロードできたようだ

早速コンテナを作成しログインしてみる

$ sudo docker run -it --name mycentos centos:latest /bin/bash
[root@0e53837cbff4 /]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)

2014年11月14日金曜日

さくらのVPSにCoreOSをインストールする

Immutable Infrastructureが面白そうだったので、そのための環境としてさくらのVPSにCoreOSをインストールしてみました。

まずはCoreOSのイメージをダウンロード

ダウンロード先 https://coreos.com


CoreOSのイメージをさくらのVPSに転送し、イメージから立ち上げる

  1. 転送先確認:VPSコントロールパネル➡OS再インストール➡ISOイメージインストールへ
  2. ホスト名とユーザ名、パスワードを確認してWinSCP等で転送
  3. 転送後、イメージが正しければ「確認」をクリック
  4. ISOイメージから仮想サーバの起動を実行する
  5. 表示されるインストール情報をメモする
  6. VNCコンソールから「HTML5モードで開く」をクリック

これでCoreOSイメージから立ち上がった状態となります。

インストール前の準備を行う

  • 立ち上げたイメージへネットワーク設定を行う

$ sudo vi /etc/systemd/network/static.network
設定内容:
[Match]
Name=eth0

[Network]
Address=IPアドレス/サブネットマスク
Gateway=IPアドレス
DNS=IPアドレス ←8.8.8.8とかにしました


  • sshアクセスするためcoreユーザにパスワードを設定する

$ sudo passwd core
これでsshでアクセス可能となります。

  • RSA暗号鍵を作成する

インストール後のCoreOS環境にアクセスする準備としてRSA暗号方式の鍵を作成する
$ ssh-keygen -t rsa
作成された「~/.ssh/id_rsa」の内容をアクセス元となる環境に送る。
(PuTTYがアクセス元ならputtygen.exeを使ってprivate keyを作っておく)
作成された「~/.ssh/id_rsa.pub」は次のコンフィグファイルに記載する。

  • インストール用コンフィグファイルの作成

$ sudo vi cloud-config
設定内容:
#cloud-config

write_files:
  - path: /etc/systemd/network/static.network
    permissions: 0644
    content: |
      [Match]
      Name=eth0

      [Network]
      Address=IPアドレス/サブネットマスク
      Gateway=IPアドレス
      DNS=8.8.8.8

ssh_authorized_keys:
  - ssh-rsa AAA・・・ core@localhost


CoreOSインストール開始

  • インストールコマンドの実行

$ sudo coreos-install -d /dev/vda -c cloud-config -C stable

Downloading the signature for http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2...
2014-11-09 01:59:44 URL:http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2.sig [543/543] -> "/tmp/coreos-install.dDd9kaQr2C/coreos_production_image.bin.bz2.sig" [1]
Downloading, writing and verifying coreos_production_image.bin.bz2...
2014-11-09 02:00:48 URL:http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2 [181896522/181896522] -> "-" [1]
gpg: Signature made Thu Oct 16 01:28:13 2014 UTC using RSA key ID E5676EFC
gpg: key 93D2DCB4 marked as ultimately trusted
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: Good signature from "CoreOS Buildbot (Offical Builds) <buildbot@coreos.com>" [ultimate]
Installing cloud-config...
Success! CoreOS stable 444.5.0 is installed on /dev/vda

インストールが完了したら再起動を行う。

起動後にsshでアクセスが出来れば成功
表示例
login as: core
Authenticating with public key "imported-openssh-key"
Last login: XXX
CoreOS (stable)
core@localhost ~ $

はまったトラブル

  • ネットワークの設定

当初はコンフィグファイルで「Name=ens3」とかにしていたが、アクセスできなかったので「Name=eth0」とした。

  • インストール時に失敗する

インストールを開始すると以下のメッセージで失敗した。
gpg: Signature made Thu Oct 16 01:28:13 2014 UTC using RSA key ID E5676EFC
gpg: key 93D2DCB4 marked as ultimately trusted
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: Good signature from "CoreOS Buildbot (Offical Builds) <buildbot@coreos.com>" [ultimate]
BLKRRPART: Device or resource busy

どうやら、もともと入っていたOSが消せなかったようなのでfdiskで削除してから実行した。

  • インストール後、アクセス出来ない

これが一番困った。
しかし、インストール後にもう一度再起動するとアクセス出来るようになった。
なんらかのタイミングの問題か。


2014年11月3日月曜日

Ubuntu12.04にRedmine2.6.0をインストールする

環境はこんな感じ
ソフトウェアバージョン
Ubuntu12.04
redmine2.6.0
ruby1.9.3p448
mysql3.2

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

  • gitのインストール

# apt-get install git
# git --version
git version 1.7.9.5

せっかくなのでバージョンアップ
# add-apt-repository ppa:git-core/ppa
# apt-get update
# apt-get install git
# git --version
git version 2.1.1

  • rubyのインストール

各種パッケージのインストール
apt-get install curl g++
apt-get install zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev
apt-get install sqlite3 libsqlite3-dev

rbenv のインストール
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

ruby-build のインストール
mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone git://github.com/sstephenson/ruby-build.git

ruby のインストール
rbenv install 1.9.3-p448
rbenv rehash
rbenv global 1.9.3-p448

.gemrcの設定
以下の内容で作成する
# cat  ~/.gemrc
install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

Ryby on Railsのインストール
gem install rails --version="~> 3.2"
rbenv rehash

作業フォルダの作成
cd
mkdir work

動作確認用の新規アプリケーションの作成
cd work
rails new foo --skip-bundle
cd foo

Gemfileの編集
/work/foo/Gemfileを編集し「gem 'therubyracer', :platforms」のコメントアウトを外す

依存パッケージのインストール
# bundle install

  • MySQLのインストール

# apt-get install mysql-server
途中でパスワードを決める

  • redmineのインストールと設定

redmineの入手
# wget http://www.redmine.org/releases/redmine-2.6.0.tar.gz

解凍とインストールディレクトリへ配置
# tar zxvf redmine-2.6.0.tar.gz
# mv redmine-2.6.0 /var/redmine

# cd /var/redmine

データベースへの接続設定
# cd /var/redmine
設定例をコピーする
# cp config/database.yml.example config/database.yml
/var/redmine/config/database.ymlのproductionを以下のように編集する
production:
adapter: mysql2
database: redmine
host: localhost
username: redmine
password: パスワード
encoding: utf8

MySQLへrootログイン
# mysql -u root -p

データベース名と文字コード設定
create database redmine character set utf8;

redmineが使うユーザ名とパスワードを設定
create user 'redmine'@'localhost' identified by 'パスワード';

権限の設定
grant all privileges on redmine.* to 'redmine'@'localhost';

Gemパッケージのインストール
# bundle install --without development test
しかしエラーが発生
Gem files will remain installed in /root/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/rmagick-2.13.3 for inspection.
Results logged to /root/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/rmagick-2.13.3/ext/RMagick/gem_make.out
An error occurred while installing rmagick (2.13.3), and Bundler cannot
continue.
Make sure that `gem install rmagick -v '2.13.3'` succeeds before bundling.

原因がわからないが以下を試して成功
# bundle install --without development test rmagick

セッションストアの秘密鍵を生成
# rake generate_secret_token
しかしエラーが発生
Could not find gem 'mysql2 (~> 0.3.11) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.

mysql2のインストールを試みる
# gem install mysql2
しかしエラーが発生
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

検索すると以下が必要らしい
#apt-get install libmysqld-dev

インストールして再度試すると成功
# gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.16
1 gem installed

# rake generate_secret_token
こちらも成功

データベースの初期設定を行う
# RAILS_ENV=production rake db:migrate

デフォルトデータを設定
言語を聞かれるので、ja と入力
# RAILS_ENV=production rake redmine:load_default_data

redmineユーザを作成
# useradd redmine
# passwd redmine

パーミッションの設定

# mkdir tmp public/plugin_assets 
chown -R redmine:redmine files log tmp public/plugin_assets

chmod -R 755 files log tmp public/plugin_assets

WEBrickによるwebサーバを起動して、インストールができたかテスト
# ruby script/rails server webrick -e production












ユーザ:admin パスワード:admin でログイン出来ることを確認した。