CoreOSで遊ぶ(その2:特に意味もなくPukiwiki環境を作る)

この記事はぴょこりんクラスタ:Re Advent Calendar 2016 - Adventarのために書いたものです。

サマリ

  • php:fpm-alpine + nginx:alpineでpukiwiki環境を作った
  • 楽がしたい場合はphp:apacheを使え
  • docker-composeが使えるとさらに楽

はじめに

コンテナ連携ごっこがしたかったので、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:9000phpのところは、--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を使おう!!!

手順はこう。

  1. docker-composeをインストールする
  2. Dockerfileを作る
  3. docker-compose.yamlを作る
  4. 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
    • ./fpm-php/Dockerfile
    • ./fpm-php/index.php
    • ./nginx/Dockerfile
    • ./nginx/server.conf

docker-compose.yaml。imageを指定すると、buildしたイメージに名前がつけられる。 linkに指定する:aliasのは、services直下のタグ?を指定する。container_nameを指定するのは間違い(僕ははまった)。

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

fpm-php/index.php

<?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

*1:wikiがほしいわけではない