ARG BASE_REGISTRY=registry1.dso.mil
ARG BASE_IMAGE=redhat/ubi/ubi9
ARG BASE_TAG=9.6
ARG ELASTIC_VERSION=9.2.1
ARG ELASTIC_REGISTRY=docker.elastic.co
ARG ELASTIC_IMAGE=elastic-maps-service/elastic-maps-server

ARG NODEJS_VERSION=22

# EMS Server source image
FROM ${ELASTIC_REGISTRY}/${ELASTIC_IMAGE}:${ELASTIC_VERSION} AS source

# Main layer
FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG}

EXPOSE 8080

ARG NODEJS_VERSION

# Install node software and user
RUN dnf -y module enable nodejs:$NODEJS_VERSION && \
    dnf install -y nodejs && \
    rpm --verify nodejs && \
    # Test
    node --version && \
    # Cleanup
    dnf -y remove nodejs-docs nodejs-full-i18n && \
    dnf -y clean all --enablerepo='*' && \
    # Add user
    groupadd --gid 1000 node && \
    useradd --uid 1000 --gid node --shell /bin/bash --create-home node

# Copy the different components from EMS Server
COPY --from=source --chown=node:node /usr/src/app /usr/src/app

# Copy the license
COPY LICENSE /licenses/elastic-maps-server

# Advertise the platform to the app
ENV EMS_PLATFORM="ironbank"

# Create EMS log folder
RUN mkdir -p /var/log/elastic-maps-server \
    && chown -R node:node /var/log/elastic-maps-server

WORKDIR /usr/src/app/server

# Run a server status check against the /status endpoint)
HEALTHCHECK --interval=30s --start-period=15s --timeout=5s \
    CMD node --no-warnings "/usr/src/app/server/app/healthcheck.js"

USER 1000

ENTRYPOINT [  ]

CMD node "app/index.js"
