2015年4月2日木曜日

dockerで開発環境を構築

dockerで開発環境を構築したのでメモを残しておきます。
久しぶりに日本語で記事を書きます。なぜなら言語がPHPだからさ!
フレームワークはこれまた日本人に人気のcakeを使います。

環境

  • Mac Yosemite
  • docker version 1.5.0(using boot2docker)
  • nginx
  • php-fpm
  • php
  • cake php
  • composor
  • suppervisor

dockerファイルの作成

まずはdockerファイルを作成します。dockerファイルを知らないって人は、公式サイトでチュートリアルを勉強してからまた来てください。チュートリアルは理解している前提で進みます。


        cd {projectfolder}
        // 環境構築用のbranchを作成
        git branch env_develop develop
        // プッシュ
        git push origin env_develop
        // Dockerfileを作成
        touch Dockerfile

Dockerfile


FROM centos:6.6
MAINTAINER edy

# yum update
RUN yum -y update

# install nginx
RUN rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
RUN yum -y install nginx

# nginx setting
ADD default.conf   /etc/nginx/conf.d/default.conf
ADD www.conf   /etc/php-fpm.d/www.conf

# install php
RUN rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN yum install -y http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
RUN yum install -y --enablerepo=remi-php55 php php-mbstring php-mcrypt php-devel php-fpm php-cli php-curl php-mysql

# php setting
ADD php.ini   /etc/php.ini

# supervisor
RUN yum install -y python-setuptools
RUN easy_install supervisor

# supervisor setting
ADD supervisord.conf /etc/supervisord.conf

# Expose ports.
EXPOSE 80

# launch nginx
# ENTRYPOINT ["nginx"]
# ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
CMD ["/usr/bin/supervisord"]

nginxとphp-fpmを同じコンテナで動作させるために、supervisorを利用しています。他は特に変わった点はありません。
まず最初にnginxの設定を行います。

default.conf


server {
    listen       80;
    server_name  localhost;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;
    root /var/www/html/app;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

fastcgi_passを設定しています。nginxとphp-fpm間をunix socketで通信します。続いて、php-fpmの設定です。

www.conf


listen = /var/run/php-fpm/php-fpm.sock

listen.owner = nginx
listen.group = nginx

上記に記載されているのは変更点のみです。本来は、全て記載する必要があります。listenはnginxのdefault.confで設定したpathと同じになります。listen.ownerとlisten.groupはnginxです。

php.ini


date.timezone = "Asia/Tokyo"

php.iniはタイムゾーンだけ変更します。この変更をしないとcake phpで警告エラーが表示されます。

supervisord.conf


[supervisord]
nodaemon=true

[program:php-fpm]
command=/usr/sbin/php-fpm --nodaemonize
autostart=true
autorestart=true

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true

nginxとphp-fpmのデーモンを切るのがポイントです。この設定はdocker特有です。

上記で準備は終了なので、ビルドします。

build Dockerfile


  // build Dockerfile
  docker build -t development001 .
  // docker launch by /bin/bash. and foldr sync
  docker run -i -t -v {プロジェクトフォルダ}/app:/var/www/html/app --name dev001 4b97a03d4b73 /bin/bash

上記でbashが起動するので、nginx、php等のバージョンを確認しましょう。cakephpでアプリのひな形を作成するので、-vでローカルのフォルダと同期をしておきます。

install composer and create cakephp


  // syncで指定したフォルダに移動
  cd /var/www/html/app
  // install composer
  curl -sS https://getcomposer.org/installer | php
  // touch 
  touch composer.json
  // crate project
  php composer.phar install

普通にcomposerを導入してcakephpのプロジェクトを作成します。syncしていれば、ローカルからソースコードが操作できるので、ソースコードの変更はSublime Textなどを利用して変更していきましょう。


  1. tmp配下フォルダの権限を777
  2. core.phpのCache::configでmask666を設定
  3. CAKE_CORE_INCLUDE_PATHに変更が必要なら変更する

build Dockerfile posting


  docker build -t development0001 .
  // docker launch by contanier. and foldr sync
  docker run -d -i -v /Users/msuzuki/Documents/nec/api/app:/var/www/html/app -p 80:80 -t development0001:latest
  // ブラウザで開く
  http://localhost:3000/

準備が完了したら再ビルドして、portを指定してdockerコンテナを立ち上げましょう。上記の場合は、boot2dockerでlocalhostを見れるようにポートフォワード設定しています。
上記の設定により、ブラウザから画面が確認できるようになります。

以上。

参考サイト

この記事がお役にたちましたらシェアをお願いします

このエントリーをはてなブックマークに追加

0 件のコメント:

コメントを投稿

Related Posts Plugin for WordPress, Blogger...