LoginSignup
3
3

More than 5 years have passed since last update.

NGINX Unit 公式Dockerイメージをためしてみた

Last updated at Posted at 2018-03-21

NGINX Unit 0.6 Betaがリリースされてた

公式のブログで書いてあったのは以下の4点。

  • Perl/PSGIのサポート
  • Amazon Linux でコンパイル済みのものがインストール可能になった
  • 公式Dockerイメージが公開
  • プロセスのより高度な設定が可能に

いよいよAmazon Linuxのサポートも入って、実用に近づいてきたなというところ。

とりあえず、公式のDockerイメージを試してみた。

ほんとはdocker-composeでぱっとやりたかったのだけれども、unitの設定を行うタイミングがうまくいかなくて一旦置いておいた。
(nginx-unit起動 -> unitのsocketへjsonをPUTでリクエスト の流れをうまく入れられなかった)

ファイル構成

.
├── nginx
│   ├── Dockerfile
│   └── nginx
│       ├── conf.d
│       │   └── default.conf
│       └── nginx.conf
└── nginx-unit-php
    ├── Dockerfile
    ├── index.php
    └── unit.conf.json

nginx/Dockerfile

From nginx

COPY nginx /etc/nginx

nginx/nginx/conf.d/default.conf

基本デフォルトのままなので変更箇所だけ抜粋。

# :
    location / {
        proxy_pass http://nginx-unit:8888/;
        proxy_set_header Host $host;
    }
# :

nginx/nginx.conf

は変更なしなので省略

nginx-unit-php/Dockerfile

From nginx/unit:0.6-php7.0

RUN mkdir -p /var/www/test

COPY index.php /var/www/test/

COPY unit.conf.json /tmp/

nginx-unit-php/unit.conf.json

{
    "listeners": {
        "*:8888": {
            "application": "test"
        }
    },
    "applications": {
        "test": {
            "type": "php",
            "processes": {
                "max": 10,
                "spare": 5,
                "idle_timeout": 20
            },
            "root": "/var/www/test",
            "index": "index.php"
        }
    }
}

nginx-unit-php/index.php

<?php

echo 'hi', PHP_EOL;

Docker

なにはなくともイメージをpull

local$ docker pull nginx/unit:0.6-php7.0
local$ docker pull nginx

(nginx/unit:latestはサポートしている言語全部入り)

build

local$ docker build -t yokotano/test/nginx/unit:php ./nginx-unit-php
local$ docker build -t yokotano/test/nginx ./nginx

run

local$ docker run --rm -d --name nginx-unit-php -p 8888:8888 yokotano/test/nginx/unit:php
local$ docker run --rm -d --name nginx -p 8080:80 --link nginx-unit-php:nginx-unit yokotano/test/nginx

unitのコンテナに入って設定

local$ docker exec -it nginx-unit-php /bin/bash
container# curl -X PUT -d @/tmp/unit.conf.json --unix-socket /var/run/control.unit.sock http://localhost/

結果

スクリーンショット 2018-03-21 17.13.20.png

とりあえずできた。

2018/03/26 追記

とりあえずtail吐かせてやればcomposeでもいけた。

#!/bin/sh
# run.sh

unitd --control unix:/var/run/control.unit.sock --log /var/log/unit.log

curl -X PUT -d @/tmp/unit.conf.json --unix-socket /var/run/control.unit.sock http://localhost/

tail -f /var/log/unit.log
# Dockerfile
From nginx/unit:0.6-php7.0

RUN mkdir -p /var/www/test

COPY index.php /var/www/test/

COPY unit.conf.json /tmp/
COPY run.sh /tmp/run.sh

CMD ["sh", "/tmp/run.sh"]
# docker-compose.yml
version: '3'

services:
  nginx:
    build: ./nginx
    ports:
      - '8080:80'
    links:
      - nginx-unit-php:nginx-unit


  nginx-unit-php:
    build: ./nginx-unit-php
    ports:
      - '8888:8888'

でもtail吐かせるのってなんか違う気がするので、これでいいのかなあという思いがある。
雰囲気以下の感じでやれないかなあと。

set -m

RUN='unitd --no-daemon --control unix:/var/run/control.unit.sock &'

eval "${RUN}"

sleep 3

curl -X PUT -d @/tmp/unit.conf.json --unix-socket /var/run/control.unit.sock http://localhost/

jobs

JOB=`jobs | grep "${RUN}" | cut -d ' ' -f1 | sed -e 's/\[\([0-9]\{1,\}\)\]+/\1/'`

fg ${JOB}

一応docker自体をフォアグラウンドで動かした時はこれでも期待通りに動く。
が、裏で動かそうとするとなんでか終了してしまう。
dockerのそもそもの仕組み的なところでダメなのかなという気がするけど、調べるためのキーワードがわからない。。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3