NGINX with syslog support in Docker

If you haven’t heard about Docker, this post will be of little interest, but if you also have started dockering-all-the-things you might have run into the limited log handling capabilities for Docker containers (Docker v0.8.0 is the current version at the time of writing), too. I especially noticed this when trying to run NGINX inside a container.

Being able to attach /dev/log of the host system to /dev/log inside a container (by adding a -v /dev/log:/dev/log parameter to docker run) made it seem like a quick fix - but only until you notice this hint in the NGINX docs:

Logging to syslog is available as part of our commercial subscription only.

Luckily there is an open source syslog patch for NGINX on GitHub. Below is a quick and dirty Dockerfile for an Ubuntu 13.10 container with a patched NGINX version.

There are a few things missing (e.g. NGINX php-fpm config, adding your application, actually configuring NGINX to use syslog), but you’ll probably figure that out.


# VERSION 0.0.1
#
# BUILD: docker build -rm -t dominik/nginx-syslog .
# RUN:   docker run -p $PORT:80 dominik/nginx-syslog
#        (logging via: -v /dev/log:/dev/log)

FROM ubuntu:13.10

RUN export DEBIAN_FRONTEND=noninteractive

RUN apt-get -y -qq update
RUN apt-get -y -qq upgrade
RUN apt-get -y -qq dist-upgrade

RUN apt-get -y --force-yes -qq install nginx \
                                       php5-dev \
                                       php5-fpm \
                                       php5-cli \
                                       php5-curl \
                                       php5-json \
                                       php-apc

# Patch Syslog support into nginx
RUN echo "deb-src http://archive.ubuntu.com/ubuntu saucy main universe" >> /etc/apt/sources.list
RUN echo "deb-src http://archive.ubuntu.com/ubuntu saucy-updates main universe" >> /etc/apt/sources.list
RUN echo "deb-src http://archive.ubuntu.com/ubuntu saucy-security main universe" >> /etc/apt/sources.list
RUN apt-get -y -qq update
RUN apt-get -y --force-yes -qq install build-essential dpkg-dev
RUN apt-get -y --force-yes -qq build-dep nginx
RUN mkdir /tmp/nginx; \
    mkdir /tmp/nginx/mod; \
    cd /tmp/nginx; \
    apt-get -y --force-yes -qq source nginx
ADD https://raw2.github.com/yaoweibin/nginx_syslog_patch/master/syslog_1.4.0.patch /tmp/nginx/mod/syslog_1.4.0.patch
ADD https://raw2.github.com/yaoweibin/nginx_syslog_patch/master/config /tmp/nginx/mod/config
RUN cd /tmp/nginx/nginx-1.4.1; \
    patch -p1 < ../mod/syslog_1.4.0.patch; \
    mv ./debian/rules ./debian/rules.original; \
    awk '/\$\(CONFIGURE_OPTS\)/{print "--add-module=/tmp/nginx/mod \\"}1' ./debian/rules.original > ./debian/rules; \
    dpkg-buildpackage -us -uc -nc; \
    cd ..; \
    dpkg -i nginx-common_1.4.1-3ubuntu1.1_all.deb nginx-full_1.4.1-3ubuntu1.1_amd64.deb; \
    rm -rf /tmp/nginx

# Add App
RUN mkdir /opt/app

# Configure php
RUN echo "cgi.fix_pathinfo = 1" >> /etc/php5/fpm/php.ini
RUN echo "output_buffering off" >> /etc/php5/fpm/php.ini
RUN echo "always_populate_raw_post_data off" >> /etc/php5/fpm/php.ini
RUN echo "magic_quotes_gpc = Off" >> /etc/php5/fpm/php.ini
RUN echo "mbstring.func_overload off" >> /etc/php5/fpm/php.ini
RUN echo "expose_php = Off" >> /etc/php5/fpm/php.ini
RUN echo "date.timezone = 'UTC'" >> /etc/php5/fpm/php.ini
RUN echo "date.timezone = 'UTC'" >> /etc/php5/cli/php.ini

EXPOSE 80

CMD service php5-fpm start && nginx

Any comments? Ping me on Twitter. 👉🏻 Get my newsletter for occasional updates. ✌🏻