# © Copyright IBM Corporation 2019, 2022
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# This Dockerfile has two separate stages.
#
# The first stage is builds the Node program, where we need tools like the C compiler for the ref-napi capability.
# It also downloads the MQ client.
# The second stage is a runtime-only container that holds just the things we need to
# execute the compiled program.
#
# Files and directories are copied from the builder container to the runtime container as needed.
# I've used two different base images, trying to get the runtime image as small
# as possible. Generally, the builder needs a C/C++ compiler to deal with native code during
# the npm install phase. But the runtime doesn't need that.
#
# The base images are taken from the Red Hat Universal Base Images repository

###########################################################
# This starts the BUILD phase
###########################################################
FROM registry.access.redhat.com/ubi8/nodejs-16 AS builder

# Set some parameters.
ENV APP_DIR    /opt/app-root/src/nodejs/mqput

WORKDIR ${APP_DIR}

USER 0

# Create the application directory so we can put stuff in there immediately. Change
# ownership to the default non-root id that is part of this container. Then switch to it being
# the active id.
RUN mkdir -p ${APP_DIR} && chown -R 1001 ${APP_DIR}
USER 1001

# Copy project files into the container. In the source directory, the
# package file has been given a different name, to avoid any attempt to
# reference it with the rest of the samples. But it gets renamed to the
# correct name during this copy.
COPY amqsput.js     ${APP_DIR}/
COPY package.docker ${APP_DIR}/package.json

# Now get all the prereq packages installed. This will also download the
# MQ client code and compile any native libraries from the dependency chain.
RUN npm install --omit=dev 

###########################################################
# This starts the RUNTIME phase
###########################################################
# Now that there is a container with the compiled program we can build a smaller
# runtime image.
FROM  registry.access.redhat.com/ubi8/nodejs-16-minimal

# This is a queue predefined in the MQ Developer Edition server container. Though
# the values can be overridden by setting environment variables during the 'docker run' phase.
ENV DOCKER_Q    DEV.QUEUE.1
ENV DOCKER_QMGR QM1
ENV MQSERVER SYSTEM.DEF.SVRCONN/TCP/localhost

ENV APP_DIR    /opt/app-root/src/nodejs/mqput
WORKDIR ${APP_DIR}

# Copy over the tree containing the program and all its dependencies
# including the ref-napi compiled code and the MQ client libraries which
# have been installed under the node_modules directory
COPY --from=builder ${APP_DIR}/ ${APP_DIR}

# We are now ready to run the amqsput program.
CMD node amqsput ${DOCKER_Q} ${DOCKER_QMGR}
