FROM python:3.6-slim
MAINTAINER Dorian Pula <dorian.pula@amber-penguin-software.ca>
ENV DOCKERIZE_VERSION v0.3.0
ENV DUMB_INIT_VERSION 1.2.0

# Add Docker utilities
RUN apt-get update \
    && apt-get install -y wget curl gcc \
    && wget https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64.deb \
    && dpkg -i dumb-init_*.deb \
    && rm dumb-init_*.deb \
    && wget https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \
    && rm dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz

# Install Node and Yarn
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
    && apt-get update \
    && apt-get install -y nodejs yarn

# Install Python dependencies
RUN pip install pipenv
COPY ["Pipfile", "Pipfile.lock", "/app/rookeries/"]
RUN cd /app/rookeries && pipenv install --system --dev

# Install Node dependencies
COPY ["package.json", "yarn.lock", "/app/rookeries/"]
RUN cd /app/rookeries && yarn install --silent

# Copy in Rookeries server code
COPY . /app/rookeries/
RUN mkdir -p /app/rookeries/dist/ \
    && mkdir -p /app/rookeries/static/js/ \
    && mkdir -p /var/www
WORKDIR /app/rookeries/

# Fix permissions
RUN chown www-data . --recursive \
    && chgrp www-data . --recursive \
    && chmod g+rw . --recursive \
    && chown www-data /var/www/ --recursive \
    && chgrp www-data /var/www/ --recursive \
    && chmod g+rw /var/www/ --recursive

USER www-data
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["inv", "server.run"]
EXPOSE 5000
