# Local Ambience image. The build helper pulls or reuses this fixed base image.
FROM amazoncorretto:21-alpine

ARG AMBIENCE_VERSION=2024.0

# Java AWT needs fontconfig and at least one system font to lay out text ("Fontconfig head is null").
RUN apk add --no-cache fontconfig ttf-dejavu

ENV AMBIENCE_HOME=/opt/elixir/Ambience \
    HEALTHCHECK_URL=http://127.0.0.1:1740/alive

WORKDIR ${AMBIENCE_HOME}/bin

# The build script normalises the selected archive to this directory name.
COPY --chown=1000:1000 Ambience/ ${AMBIENCE_HOME}/

RUN addgroup -S -g 1000 ambience \
    && adduser -S -D -u 1000 -G ambience ambience \
    && mkdir -p "${AMBIENCE_HOME}/log" \
      "${AMBIENCE_HOME}/data/in" "${AMBIENCE_HOME}/data/out" \
    && chown -R 1000:1000 "${AMBIENCE_HOME}/log" "${AMBIENCE_HOME}/data" \
    && chmod 0755 "${AMBIENCE_HOME}/bin/run-server" "${AMBIENCE_HOME}/bin/ambience-cli"

LABEL org.opencontainers.image.title="Ambience" \
      org.opencontainers.image.version="${AMBIENCE_VERSION}" \
      org.opencontainers.image.description="Locally built Ambience base image"

HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
  CMD ["sh", "-c", "wget -q -O /dev/null \"$HEALTHCHECK_URL\""]

USER 1000:1000
EXPOSE 1740
ENTRYPOINT ["./run-server"]
