FROM nginx:latest

MAINTAINER Markus Willman

ENV PYTHONPATH=$PYTHONPATH:/opt/services/smartbusstop/face_classification/src

SHELL ["/bin/bash", "-c"]

# update the system, install python3 and create symlinks and directories (supervisor forces python2.7 to also install)
RUN apt-get update -qqy --fix-missing \
	&& apt-get upgrade -qqy \
	&& apt-get install -qqy --no-install-recommends \
	build-essential \
	python3.5 \
	python3.5-dev \
	python3-pip \
	python3-setuptools \
	python3-tk \
	supervisor \
	git \
	libgtk2.0-dev \
	&& apt-get clean \
	&& rm -f /etc/nginx/nginx.conf \
	&& rm -f /etc/nginx/conf.d/default.conf \
	&& mkdir -p /opt/services/smartbusstop \
	&& ln -s /opt/services/smartbusstop/conf/nginx/nginx.conf /etc/nginx/nginx.conf \
	&& ln -s /opt/services/smartbusstop/conf/nginx/default.conf /etc/nginx/conf.d/default.conf \
	&& ln -s /opt/services/smartbusstop/conf/supervisor/nginx.conf /etc/supervisor/conf.d/01_nginx.conf \
	&& ln -s /opt/services/smartbusstop/conf/supervisor/smartbusstop.conf /etc/supervisor/conf.d/02_smartbusstop.conf \
	&& git clone --depth=1 --branch=master https://github.com/oarriaga/face_classification /opt/services/smartbusstop/face_classification \
	&& rm -rf /opt/services/smartbusstop/face_classification/.git \
	&& apt-get remove -qqy --purge git \
	&& apt-get autoremove -y

WORKDIR /opt/services/smartbusstop

COPY . /opt/services/smartbusstop

# install python dependencies opencv-python needs libgtk2.0-dev installed in the system
# copy files from contrib to src overwriting originals, if I wasn't lazy I'd do this better
RUN pip3 --no-cache-dir install -r /opt/services/smartbusstop/system-requirements.txt \
	&& rm -f /opt/services/smartbusstop/system-requirements.txt \
	&& virtualenv /opt/services/smartbusstop/.venv \
	&& source /opt/services/smartbusstop/.venv/bin/activate \
	&& pip --no-cache-dir install -r /opt/services/smartbusstop/requirements.txt \
	&& rm -f /opt/services/smartbusstop/requirements.txt \
	&& cp -fR /opt/services/smartbusstop/contrib/* /opt/services/smartbusstop/face_classification \
	&& rm -rf /opt/services/smartbusstop/contrib

EXPOSE 80
STOPSIGNAL SIGTERM

CMD ["/usr/bin/supervisord", "-n"]
