先日このサイトを移行した際にタイトルの構成でWebサーバーを構築しました。
ちまたに記事はやまほどあるのですが、せっかくですので。
また、公式のnginxのDocker imageは1.11.6のものではブラウザがChromeの場合、http2で通信できませんでした。なんかopen-sslのバージョンの問題っぽいです。なので、Chromeの場合はhttp2で通信できません。いちおう、対応する予定っぽい(というかもうやったのか?)
というのもあったのでせっかくなので自作しました。なお、今回作ったものはここに置いてます。
Dockerfile
まず、Dockefileを作ります。
ソースからビルドします。解説は以下の記事が参考になります。
Nginxのインストールと基本設定 (1/4)
ちゅ~わけで、以下が実際に作成したDockerfileです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| FROM alpine:3.4
MAINTAINER YoshinoriN
ENV NGINX_VERSION=1.11.8
RUN apk --no-cache update \ && apk --no-cache add --virtual build-dependencies curl build-base openssl-dev \ && apk --no-cache add pcre-dev libxslt-dev gd-dev geoip-dev \ && curl -O -s http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \ && tar xvfz nginx-${NGINX_VERSION}.tar.gz \ && addgroup -S nginx \ && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \ && cd nginx-${NGINX_VERSION} \ && ./configure \ --prefix=/usr/share/nginx \ --conf-path=/etc/nginx/nginx.conf \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log \ --lock-path=/var/lock/nginx.lock \ --pid-path=/run/nginx.pid \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --http-scgi-temp-path=/var/lib/nginx/scgi \ --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ --with-debug \ --with-pcre-jit \ --with-ipv6 \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_auth_request_module \ --with-http_addition_module \ --with-http_dav_module \ --with-http_geoip_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_image_filter_module \ --with-http_v2_module \ --with-http_sub_module \ --with-http_xslt_module \ --with-stream \ --with-stream_ssl_module \ --with-mail \ --with-mail_ssl_module \ --with-threads \ && make \ && make install \ && ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log \ && cd / \ && mkdir /var/lib/nginx \ && apk del --purge build-dependencies \ && rm nginx-${NGINX_VERSION}.tar.gz \ && rm -rf nginx-${NGINX_VERSION} \ && rm -rf /var/cache/apk/*
EXPOSE 80 443
CMD ["usr/share/nginx/sbin/nginx", "-g", "daemon off;"]
|
Docker Composeを作る
と、まあ、後はビルドして動かせばよい…のですが、個人的にコンテナが一つだったとしてもDocker Composeを使用するようにしています。docker-compose.yml
内にマウントのパスやポートを記述できるのと、docker-composeの方が覚えるコマンドの量が少ないです。要するに「起動時に長ったらしくコマンド打たなくてよい」からです。私のようなアルコール漬けの脳ミソには少しでも楽な方がいいです。
というわけで、Docker Composeを作っていきます。
ディレクトリ構成
まず、ディレクトリの構成を決めます。
1 2 3 4 5 6 7 8 9 10 11 12
| . |-- docker-compose.yml |-- logs | `-- nginx | |-- access.log | `-- error.log |-- nginx | |-- config | | |-- nginx.conf | | `-- nginx.conf.default | `-- Dockerfile `-- README.md
|
なんか、ディレクトリの構成に違和感があるかもしれませんが、今後別のアプリも同一のdocker-compose.yml
で動かす時のことを考えるとこれでいいかな…?と思って上記にしています。
docker-compose.yml
次にdocker-compose.yml
を作ります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| version: '2' services: nginx: environment: TZ: Asia/Tokyo build: ./nginx volumes: - ./logs/nginx/:/var/log/nginx/ - ./nginx/config/nginx.conf:/etc/nginx/nginx.conf - /home/example/www/:/www/ - /etc/letsencrypt/live/exapmle.com/cert.pem:/etc/nginx/cert.pem - /etc/letsencrypt/live/exapmle.com/privkey.pem:/etc/nginx/private.pem hostname: nginx ports: - "80:80" - "443:443"
|
解説
まあ、だいたい見たらわかりますが、ざっとした解説です。
- docker-compose.ymlから見たnginxのDockerfileを
build
に指定します volumes
にコンテナにマウントするディレクトリやファイルを指定します- ログ、nginxの設定ファイル、証明書、Webサイトのコードなど
environment
のTZ
でタイムゾーンを指定ports
でポートを指定
nginx.conf
こちらに書きました
起動
docker-compose up -d
で起動できます。
以上。
簡単ですな。