FROM alpine:latest

# install packages
RUN apk add --no-cache tini wget curl shadow
RUN rm -fr /var/cache/apk/*

# setup main user
RUN adduser -D postgrest
RUN groupmod --gid 1000 postgrest
RUN usermod --uid 1000 postgrest

# install postgrest
RUN wget "https://github.com/PostgREST/postgrest/releases/download/v12.2.3/postgrest-v12.2.3-linux-static-x64.tar.xz" -O /tmp/postgrest.tar.xz
RUN tar xJf /tmp/postgrest.tar.xz -C /usr/local/bin
RUN rm /tmp/postgrest.tar.xz

# copy scripts
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh

# remove build packages
RUN apk del shadow

# make the dirs
RUN mkdir -p /etc/postgrest.d && \
	chown postgrest:postgrest /etc/postgrest.d

# do the
USER postgrest
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/usr/local/bin/entrypoint.sh"]