FROM alpine:3.7
LABEL maintainer="robert.gauto@gmail.com"

ENV USER=pg CONFIG_PATH=/etc/pgbouncer LOG_PATH=/var/log/pgbouncer PID_PATH=/var/run/pgbouncer

RUN set -x \
	apk update && \
        apk upgrade && \
	apk add --no-cache --virtual \
        pgbouncer && \
	addgroup -S -g 1000 $USER && \
  	adduser -S -H -D -h /home/$USER -s /bin/sh -u 1000 -G $USER $USER && \
	echo "$USER:$(dd if=/dev/urandom bs=24 count=1 status=none | base64)" | chpasswd && \
     	mkdir -p $CONFIG_PATH && \
        mkdir -p $LOG_PATH && \
        mkdir -p $PID_PATH && \
	chmod -R 755 $LOG_PATH && \
	chmod -R 755 $PID_PATH && \ 
	chown -R $USER:$USER $LOG_PATH && \
	chown -R $USER:$USER $PID_PATH

VOLUME ["$CONFIG_PATH", "$LOG_PATH"]

COPY pgbouncer.ini "$CONFIG_PATH"
COPY userlist.txt "$CONFIG_PATH"

ADD entrypoint.sh ./

EXPOSE 6432

ENTRYPOINT ["./entrypoint.sh"]
CMD ["pgbouncer"]
