LoginSignup
33
26

More than 5 years have passed since last update.

DockerfileでNginxの起動とログのローテーションまで

Posted at

はじめに

Docker触り初めですが、
やるからには本番環境に投入すると奮起しております。
Nginxで簡単な静的ページの公開まで備忘録として残しておきます。

環境

$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
$ docker -v
Docker version 1.12.3-cs4, build 65c6c4

適当な仮想OSです。

準備からコンテナ起動まで

Dockerのインストールは完了している前提です。

作業ディレクトリを作成

$ mkdir docker-test
$ cd docker-test/

Dockerfieを作成する

$ vi Dockerfile
FROM centos:centos7.1.1503
ENV container docker
RUN export http_proxy=http://<Proxy-server>:8080 && \
    export https_proxy=http://<Proxy-server>:8080 && \
    yum swap -y fakesystemd systemd && \
    yum update -y && \
    yum install -y epel-release && \
    yum install -y nginx && \
    yum clean all && \
    ln -sf  /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
ADD nginx.conf /etc/nginx/
ADD default.conf /etc/nginx/conf.d/
RUN systemctl enable nginx
EXPOSE 80

何もしないとタイムゾーンがUTCなのでJSTにしてあげています。
基本的にコンテナを起動したらそのコンテナにはログインも変更しない状態が宜しいかと。

nginx.confの作成

コンテナに設定したいnginx.confを作成します。

$ vi nginx.conf
user  nginx;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    multi_accept on;
    use epoll;
}
http {
    server_tokens off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for $request_time"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip_static  on;
    gzip  on;
    gzip_types image/png image/gif image/jpeg text/javascript text/css;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_comp_level 9;
    gunzip on;
    include /etc/nginx/conf.d/*.conf;
}

default.confの作成

こちらもコンテナに設定したいを作成します。

$ vi default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page 403 404 500 502 503 504   /404.html;
    location = /404.html {
        return 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL $request_uri was not found on this server.</p>\n</body></html>";
        internal;
    }
}

error_page部分はApache風にする設定の情報があったのでそちらをマネております。
ここまででファイル構成は下記となります。

$ ls -1
Dockerfile
default.conf
nginx.conf

ビルド

ビルドしてDockerイメージを作成します。

$ sudo docker build -f ./Dockerfile -t centos:nginx-test --no-cache .
Sending build context to Docker daemon  5.12 kB
Step 1 : FROM centos:centos7.1.1503
 ---> 285396d0a019
~省略~
Step 8 : EXPOSE 80
 ---> Running in 9f66d7de2d51
 ---> d903a01a7bd3
Removing intermediate container 9f66d7de2d51
Successfully built d903a01a7bd3

イメージの確認

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              nginx-test          d903a01a7bd3        3 minutes ago       588.7 MB

起動

作成したdockerイメージからコンテナを起動します。

$ sudo docker run -itd --privileged -p8888:80 \
-v /home/test/html:/usr/share/nginx/html:ro \
-v /home/test/logs:/var/log/nginx \
--name nginx-test --hostname nginx-test centos:nginx-test /sbin/init

  • -vでホストOS側の領域をコンテナ側にマウントしてあげます。
    • /home/test/html:/usr/share/nginx/html:roがNginxで公開するコンテンツ領域。事前に何かしらの静的ファイルを置いておきます。
    • /home/test/logs:/var/log/nginxがNginxのログ領域。
    • /home/test/logsは事前に作成しなくてもコンテナ起動時に勝手に作成されます。
  • --privilegedを入れないとNginxが自動起動してくれなかったです。

起動したことを確認します。

$ sudo docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                  NAMES
0745709f54a2        centos:nginx-test       "/sbin/init"             58 seconds ago      Up 57 seconds       0.0.0.0:8888->80/tcp   nginx-test

確認してみる

タイムゾーンがJSTになっていること。

$ sudo docker exec -it nginx-test timedatectl status
      Local time: Thu 2017-05-04 16:40:25 JST
  Universal time: Thu 2017-05-04 07:40:25 UTC
        RTC time: Thu 2017-05-04 07:40:25
       Time zone: Asia/Tokyo (JST, +0900)/
     NTP enabled: n/a
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

Nginxが起動していて、コンテンツが取得出来ること。

sudo docker exec -it nginx-test systemctl status nginx
~省略~
curl http://localhost:8888

ログが出力されていること。

$ cd /home/test/logs
$ ls -1
access.log
error.log

無事に起動出来ました。
この後commitしてもう一つ起動しようとしましたが、何故か固まりスキップ・・・。

ログローテート

色々やり方あるみたいですが、ホストOS側の設定でログをローテートしてみました。
ホストOSの下記領域にコンテナで起動しているNginxのログが出力されています。

$ ls -1 /home/test/logs/
access.log
error.log

ログローテート設定をします。

$ sudo vi /etc/logrotate.d/test
/home/test/logs/*log {
    rotate 90
    missingok
    ifempty
    sharedscripts
    compress
    postrotate
        docker exec -it nginx-test systemctl reload nginx > /dev/null 2>/dev/null || true
    endscript
}

下記を実行

logrotate -fv /etc/logrotate.d/test

ログローテートされたことを確認

$ ls -1
access.log
access.log.1.gz
error.log
error.log.1.gz

まとめ

Dockerfileを使ってNginxの起動とログローテートまで実施してみました。
まだまだ不明な点があるが、本番環境への導入に向けてこれから触っていきたいと思います。

もう少し慣れたらdocker-composeも触っていきたい。

33
26
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
33
26