IMAGE rabbitmq_3.8.9-210106
FROM alpine3.12_3.12.3-210106

RUN EOF
    # Install RabbitMQ runtime dependencies
    apk --no-cache add procps openssl

    # Install RabbitMQ build dependencies
    apk --no-cache add --virtual .deps autoconf gcc libc-dev linux-headers make ncurses-dev openssl-dev

    # Download and unpack Erlang/OTP
    cd /tmp
    wget https://github.com/erlang/otp/archive/OTP-23.1.1.tar.gz
    tar xf OTP-*

    # Compile and install Erlang/OTP
    cd /tmp/otp-*
    export ERL_TOP=$(pwd)
    export CFLAGS='-g -O2'
    ./otp_build autoconf
    ./configure \
        --host=x86_64-linux-musl \
        --build=x86_64-linux-musl \
        --disable-dynamic-ssl-lib \
        --disable-hipe \
        --disable-sctp \
        --disable-silent-rules \
        --enable-clock-gettime \
        --enable-hybrid-heap \
        --enable-kernel-poll \
        --enable-shared-zlib \
        --enable-smp-support \
        --enable-threads \
        --with-microstate-accounting=extra \
        --without-common_test \
        --without-debugger \
        --without-dialyzer \
        --without-diameter \
        --without-edoc \
        --without-erl_docgen \
        --without-erl_interface \
        --without-et \
        --without-eunit \
        --without-ftp \
        --without-hipe \
        --without-jinterface \
        --without-megaco \
        --without-observer \
        --without-odbc \
        --without-reltool \
        --without-ssh \
        --without-tftp \
        --without-wx
    make -j $(getconf _NPROCESSORS_ONLN) GEN_OPT_FLGS="-O2 -fno-strict-aliasing"
    make install

    # Download and install RabbitMQ
    cd /tmp
    wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-generic-unix-latest-toolchain-3.8.9.tar.xz
    tar xf rabbitmq-server-* -C /opt
    mv /opt/rabbitmq* /opt/rabbitmq
    sed -i 's/^SYS_PREFIX=.*$/SYS_PREFIX=/' /opt/rabbitmq/sbin/rabbitmq-defaults
    ln -s /opt/rabbitmq/sbin/* /usr/sbin/

    # Create RabbitMQ OS user
    addgroup -S -g 5672 rabbitmq
    adduser -S -u 5672 -h /var/lib/rabbitmq -s /bin/false -g rabbitmq -G rabbitmq rabbitmq

    # Create RabbitMQ directory structure
    mkdir -p /var/lib/rabbitmq /etc/rabbitmq/conf.d /var/log/rabbitmq
    chown -R rabbitmq:rabbitmq /opt/rabbitmq /var/lib/rabbitmq /etc/rabbitmq /var/log/rabbitmq
    chmod 755 /var/lib/rabbitmq /etc/rabbitmq /var/log/rabbitmq

    # Cleanup
    apk --no-cache del .deps
    rm -rf /usr/local/lib/erlang/lib/*/examples /usr/local/lib/erlang/lib/*/src /tmp/*
EOF

USER rabbitmq
ENV HOME /var/lib/rabbitmq
ENV RABBITMQ_LOGS -
CMD /usr/sbin/rabbitmq-server
READY /usr/sbin/rabbitmqctl await_startup
