/
Dockerfile
67 lines (48 loc) · 2.22 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Stage 0: Install alpine Linux (for a smaller size) + node
# Parent/Base image to use as a starting point for our own image
# Use a larger base image for installing dependencies
FROM node:16.17.0@sha256:a5d9200d3b8c17f0f3d7717034a9c215015b7aae70cb2a9d5e5dae7ff8aa6ca8 AS dependencies
RUN apt-get update && apt-get install -y --no-install-recommends dumb-init=1.2.* \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Image Metadata
LABEL maintainer="Jessica Krishtul <jkrishtul@myseneca.ca>" \
description="Fragments node.js microservice"
# Optimize Node.js apps for production
ENV NODE_ENV=production \
# We default to use port 8080 in our service
PORT=8080 \
# Reduce npm spam when installing within Docker
# https://docs.npmjs.com/cli/v8/using-npm/config#loglevel
NPM_CONFIG_LOGLEVEL=warn \
# Disable colour when run inside Docker
# https://docs.npmjs.com/cli/v8/using-npm/config#color
NPM_CONFIG_COLOR=false
# Use /app as our working directory
WORKDIR /app
# Copy the package.json and package-lock.json files into /app
COPY package*.json ./
# Install only production node dependencies defined in package-lock.json
RUN npm ci --only=production
#######################################################################
# Stage 1: use dependencies to build server
# Use officially supported and deterministic image tags
FROM node:16.17.0-bullseye-slim@sha256:59812c19504546fc66b0b26722bf0754ee48b74f9abc5ed9c3f251fc45d86099 AS build
RUN apt-get update && apt-get install --no-install-recommends curl=7.* -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=dependencies /usr/bin/dumb-init /usr/bin/dumb-init
# Run container with least privileged user
USER node
WORKDIR /app
# Copy cached dependencies from previous stage so we don't have to download (node modules)
COPY --chown=node:node --from=dependencies /app /app
# Copy all our source code
COPY --chown=node:node . .
# Start the container by running our server
# Properly handle events to safely terminate a Node.js application
CMD ["dumb-init", "npm", "start"]
# We run our service on port 8080
EXPOSE ${PORT}
HEALTHCHECK --interval=15s --timeout=30s --start-period=5s --retries=3 \
CMD curl --fail localhost:${PORT} || exit 1