fragments / Dockerfile
Dockerfile
Raw
 ## Docker file for microservice
# # Use node version 16.13.2
# FROM node:16.13.2

# # Adds key=value pairs with arbitrary metadata about your image.
# LABEL maintainer="Duc Phong Ma <mphong-duc@myseneca.ca>"
# LABEL description="Fragments node.js microservice"

# # We default to use port 8080 in our service
# ENV PORT=8080

# # Reduce npm spam when installing within Docker
# # https://docs.npmjs.com/cli/v8/using-npm/config#loglevel
# ENV NPM_CONFIG_LOGLEVEL=warn

# # Disable colour when run inside Docker
# # https://docs.npmjs.com/cli/v8/using-npm/config#color
# ENV NPM_CONFIG_COLOR=false

# # Use /app as our working directory
# WORKDIR /app

# # Option 1: explicit path - Copy the package.json and package-lock.json
# # files into /app. NOTE: the trailing `/` on `/app/`, which tells Docker
# # that `app` is a directory and not a file.
# COPY package*.json /app/

# # Install node dependencies defined in package-lock.json
# RUN npm install

# # Copy src to /app/src/
# COPY ./src ./src

# # Copy our HTPASSWD file
# COPY ./tests/.htpasswd ./tests/.htpasswd

# # Start the container by running our server
# CMD npm start

# # We run our service on port 8080
# EXPOSE 8080

#========================================

# Stage 0: Install node:18-alpine3.17 + node + dependencies
FROM node:18-alpine3.17@sha256:b45e71e98bd0eecd4b694c7fb0281e08e06a384de26a986d241d348926692318 as dependencies

# Adds key=value pairs with arbitrary metadata about your image.
LABEL maintainer="Duc Phong Ma <mphong-duc@myseneca.ca>"
LABEL description="Fragments node.js microservice"

# We default to use port 8080 in our service
ENV PORT=8080

# Reduce npm spam when installing within Docker
# https://docs.npmjs.com/cli/v8/using-npm/config#loglevel
ENV NPM_CONFIG_LOGLEVEL=warn

# Disable colour when run inside Docker
# https://docs.npmjs.com/cli/v8/using-npm/config#color
ENV NPM_CONFIG_COLOR=false

ENV NODE_ENV=production

# Use /app as our working directory
WORKDIR /app

# Option 1: explicit path - Copy the package.json and package-lock.json
# files into /app. NOTE: the trailing `/` on `/app/`, which tells Docker
# that `app` is a directory and not a file.
COPY package*.json /app/

# Install node dependencies defined in package-lock.json
RUN npm install

#========================================
# Stage 1: use dependencies to build the site
FROM node:18-alpine3.17@sha256:b45e71e98bd0eecd4b694c7fb0281e08e06a384de26a986d241d348926692318 as production

WORKDIR /app

#copy folder and change the owner to node user
COPY --chown=node:node --from=dependencies /app /app

# Copy src to /app/src/
COPY --chown=node:node ./src ./src

# Copy our HTPASSWD file
COPY --chown=node:node ./tests/.htpasswd ./tests/.htpasswd

# Switch user to node before runing the app to prevent any attacks form hackers
USER node

# Start the container by running our server
CMD ["npm", "start"]

# We run our service on port 8080
EXPOSE 8080

# Define a command to run in order to monitor the health of our container's process
HEALTHCHECK --interval=10s --timeout=30s --start-period=5s --retries=3 \
CMD curl --fail localhost:8080 || exit 1