# syntax = docker/dockerfile:1-experimental

#FROM golang:alpine AS base
FROM golang:1.15.2-alpine AS base
WORKDIR /go/src/github.com/cmp/blockchain
ENV CGO_ENABLED=1


# Set up dependencies
ENV PACKAGES git build-base
# Install dependencies
RUN apk add --update $PACKAGES
# Add source files
COPY ./ ./
#ENV GOPROXY=https://mirrors.aliyun.com/goproxy/
ENV GOSUMDB=off
RUN --mount=type=cache,target=/go/pkg/mod \
    cd chain && go mod download


FROM base AS build


ARG ENABLE_PRO
ENV ENABLE_PRO=${ENABLE_PRO}


RUN --mount=target=. \
    --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
        cd chain && make build-docker-ouput

# Final image
FROM alpine

WORKDIR /root

# Install ca-certificates
RUN apk add --no-cache --update  
# supervisor

RUN apk add --update python3 py3-pip
RUN pip install supervisor


COPY --from=build /cmpd /usr/bin/cmpd
COPY --from=build /cmpcli /usr/bin/cmpcli
COPY --from=build /go/src/github.com/cmp/blockchain/chain/network/scripts/start.sh /usr/bin/start.sh

RUN mkdir -p /etc/supervisor/conf.d/
COPY --from=build /go/src/github.com/cmp/blockchain/chain/network/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY --from=build /go/src/github.com/cmp/blockchain/chain/network/supervisor/conf.d/* /etc/supervisor/conf.d/

EXPOSE 26656 26657 1317 8545 8546

# Add supervisor configuration files
COPY --from=build /go/src/github.com/cmp/blockchain/chain/network/scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod u+x /usr/local/bin/entrypoint.sh

ENTRYPOINT [ "/usr/local/bin/entrypoint.sh" ]
#ENTRYPOINT [ "supervisord --nodaemon --configuration /etc/supervisor/supervisord.conf" ]
STOPSIGNAL SIGINT
