
FROM alpine:3.15

# vim and bash for debug
RUN apk update && apk add \
	nginx openssl \
	vim bash \
	&& \
	rm -rf /var/cache/apk*

# nginx conf
COPY ./conf/nginx.conf.alpine /etc/nginx/nginx.conf
COPY ./conf/inception_nginx.conf /etc/nginx/http.d/
# dir for logs
RUN mkdir -p /var/log/nginx/

# create user www-data and assign it to group www-data
RUN adduser -S www-data && \
	adduser www-data www-data

ARG WP_URL
ARG MAX_UPLOAD_SIZE
ARG WP_VOLUME_DIR
ARG WP_VOLUME_PLUGINS
ARG NG_VOLUME_CERTS

# create and empty volumes dir
RUN mkdir -p ${WP_VOLUME_DIR}   ${WP_VOLUME_PLUGINS}   ${NG_VOLUME_CERTS} && \
	rm -rf   ${WP_VOLUME_DIR}/* ${WP_VOLUME_PLUGINS}/* ${NG_VOLUME_CERTS}/*

# replace WP_URL
RUN sed -i "s/\${WP_URL}/${WP_URL}/g" /etc/nginx/http.d/inception_nginx.conf

# replace max file size upload
RUN sed -i "s/\(client_max_body_size \).*\(m;\)/\1${MAX_UPLOAD_SIZE}\2/g" /etc/nginx/nginx.conf

# create ssl certificate
COPY ./conf/ssl ${NG_VOLUME_CERTS}
RUN if [ -z "$(ls -A ${NG_VOLUME_CERTS} 2>/dev/null)" ]; then \
		mkdir ${NG_VOLUME_CERTS}/private ${NG_VOLUME_CERTS}/certs; \
		openssl req -newkey rsa:2048 -nodes -x509 -days 365 \
			-subj "/C=fr/ST=ile-de-france/L=paris/O=42/OU=inception/CN=${WP_URL}" \
			-keyout ${NG_VOLUME_CERTS}/private/${WP_URL}.key \
			-out ${NG_VOLUME_CERTS}/certs/${WP_URL}.crt; \
	fi

ENTRYPOINT [ "nginx", "-g", "daemon off;" ]

