IMAGE decidim-nginx_1.18.0-210106
FROM alpine3.12-ruby2.7_2.7.2-210106

RUN EOF
    # Install runtime dependencies
    apk --no-cache add curl pcre

    # Install build dependencies
    apk --no-cache add --virtual .deps build-base curl-dev libressl-dev linux-headers pcre-dev

    # Install passenger
    gem install passenger --no-document
    ln -s /usr/local/lib/ruby/gems/2.7.0/gems/passenger-* /usr/local/lib/passenger

    # Create OS user
    addgroup -S -g 8080 decidim
    adduser -S -u 8080 -h /srv/decidim-app -s /sbin/nologin -G decidim -g decidim decidim

    # Compile nginx
    # Minimized version of /usr/local/bin/passenger-install-nginx-module
    NGINX_VERSION=$(grep '  PREFERRED_NGINX_VERSION' /usr/local/lib/passenger/src/ruby_supportlib/phusion_passenger.rb | grep -Eo '([0-9\.]+)')
    cd /tmp
    wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
    tar xf nginx-*.tar.gz
    cd nginx-*
    ./configure \
        --prefix=/var/lib/nginx \
        --sbin-path=/usr/sbin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --pid-path=/run/nginx.pid \
        --lock-path=/run/nginx.lock \
        --user=decidim \
        --group=decidim \
        --with-threads \
        --with-file-aio \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-http_realip_module \
        --with-http_gzip_static_module \
        --with-http_stub_status_module \
        --with-http_addition_module \
        --without-http_fastcgi_module \
        --without-http_memcached_module \
        --without-http_scgi_module \
        --without-http_uwsgi_module \
        --with-cc-opt=-Wno-error \
        --add-module=/usr/local/lib/passenger/src/nginx_module
    make -j $(getconf _NPROCESSORS_ONLN)
    make install
    rm -f /etc/nginx/*.default

    # Cleanup
    apk --no-cache del .deps
    rm -rf /tmp/*
    rm -rf /.gem
EOF

COPY decidim-nginx.image.d

CMD /bin/s6-svscan /etc/services.d
