CoreOSで遊ぶ(その2:特に意味もなくPukiwiki環境を作る)
この記事はぴょこりんクラスタ:Re Advent Calendar 2016 - Adventarのために書いたものです。
サマリ
はじめに
コンテナ連携ごっこがしたかったので、pukiwikiを動かすという名目で httpサーバ用コンテナとphp実行用コンテナを分けて動かしてみた*1。
単に動かしてみるところまで
使ったイメージはもちろんalpine linuxをベースにしている公式イメージで、とにかくサイズが小さいのがジャスティス。 楽をしたいならphp:apacheというDebianベースのイメージを使ったほうがよい。何よりはまらないしね。
ディレクトリのバインドはnginxコンテナとfpm-php両方にやる必要がある(当然ぽいけど僕ははまった)。 *.phpの要求が来る→fpm-phpコンテナに向かって要求を飛ばす→fpm-phpコンテナが要求を実行して結果を返す→nginxがレスポンスを返す、という流れですね。
$ docker run -v /home/bisco/pukiwiki:/var/www/html/wiki --name fpm-php -h fpm-php -d php:fpm-alpine $ docker run -v /home/bisco/pukiwiki:/var/www/html/wiki --name nginx -h nginx --link fpm-php:php -p 80:80 -d nginx:alpine
docker runするだけじゃダメで、nginx側に設定が追加で必要がある。
# alpine linuxのシェルは/bin/ash $ docker exec -it nginx /bin/ash $ cd /etc/nginx.conf/conf.d/ $ vi server.conf
server.confはこれ。fastcgi_pass php:9000
のphp
のところは、--linkで指定した名前を書く。
server { listen 80 default; server_name _; root /var/www/html; index index.php index.html index.htm; charset utf-8; access_log off; error_log off; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
設定書いたら一応リスタートさせておく。
$ docker restart nginx
手作業でphpinfo.phpを放り込んでから、ブラウザでアクセスしてみると、うまく行ってればいつもの画面が表示されるはず。
$ docker exec -it fpm-php /bin/ash $ echo "<?php phpinfo(); ?>" > /var/www/html/index.php
phpの動作が確認できたら、pukiwiki.ini.phpの修正とcacheファイルの権限を CoreOSから変えておく。
# autoだとちゃんと認識できないのでURLを入れる。 $ vi pukiwiki.ini.php // Specify PukiWiki URL (default: auto) $script = 'http://xxx.xxx.xxx.xxx/wiki/'; // cache以下のファイルがライトできなかったので777にしておく $ sudo chmod 777 cache/*
この面倒な作業を全部勝手にやってほしい
docker-composeを使おう!!!
手順はこう。
- docker-composeをインストールする
- Dockerfileを作る
- docker-compose.yamlを作る
- docker-compose up -d
docker-composeのインストール
CoreOSにdocker-composeを導入 - Qiita に全部書いてあるのでコマンド履歴だけ。
$ mkdir -p /opt/bin $ curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /opt/bin/docker-compose $ chmod +x /opt/bin/docker-compose $ docker-compose version docker-compose version docker-compose version 1.9.0, build 2585387 docker-py version: 1.10.6 CPython version: 2.7.9 OpenSSL version: OpenSSL 1.0.1t 3 May 2016
Dockerfileとかdocker-compose.yamlを作る
ディレクトリ構成をこうしてファイルを作る。
- ./
- docker-compose.yaml
docker-compose.yaml。imageを指定すると、buildしたイメージに名前がつけられる。
linkに指定する
version: '2' services: fpm-php: build: ./fpm-php image: fpm-php-sandbox container_name: fpm-php volumes: - /home/bisco/pukiwiki:/var/www/html/wiki nginx: build: ./nginx image: nginx-sandbox container_name: nginx ports: - 80:80 links: - fpm-php:php volumes: - /home/bisco/pukiwiki:/var/www/html/wiki
fpm-php/Dockerfile
FROM php:fpm-alpine COPY index.php /var/www/html
<?php phpinfo();?>
nginx/Dockerfile
FROM nginx:alpine ADD server.conf /etc/nginx/conf.d/server.conf
nginx/server.conf
server { listen 80 default; server_name _; root /var/www/html; index index.php index.html index.htm; charset utf-8; access_log off; error_log off; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
docker-compose up -d
あとは何も考えずコマンドを実行すれば動いてくれる。
$ docker-compose up -d