-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathDockerfile
More file actions
105 lines (74 loc) · 2.71 KB
/
Dockerfile
File metadata and controls
105 lines (74 loc) · 2.71 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## Stage 1 (production base)
# This gets our prod dependencies installed and out of the way
FROM node:10-alpine as base
EXPOSE 3000
ENV NODE_ENV=production
WORKDIR /opt
COPY package*.json ./
# we use npm ci here so only the package-lock.json file is used
RUN npm config list \
&& npm ci \
&& npm cache clean --force
## Stage 2 (development)
# we don't COPY in this stage because for dev you'll bind-mount anyway
# this saves time when building locally for dev via docker-compose
FROM base as dev
ENV NODE_ENV=development
ENV PATH=/opt/node_modules/.bin:$PATH
WORKDIR /opt
RUN npm install
WORKDIR /opt/app
CMD ["nodemon", "./bin/www", "--inspect=0.0.0.0:9229"]
## Stage 3 (copy in source)
# This gets our source code into builder for use in next two stages
# It gets its own stage so we don't have to copy twice
# this stage starts from the first one and skips the last two
FROM base as source
WORKDIR /opt/app
COPY . .
## Stage 4 (testing)
# use this in automated CI
# it has prod and dev npm dependencies
# In 18.09 or older builder, this will always run
# In BuildKit, this will be skipped by default
FROM source as test
ENV NODE_ENV=development
ENV PATH=/opt/node_modules/.bin:$PATH
# this copies all dependencies (prod+dev)
COPY --from=dev /opt/node_modules /opt/node_modules
# run linters as part of build
# be sure they are installed with devDependencies
RUN eslint .
# run unit tests as part of build
RUN npm test
# run integration testing with docker-compose later
CMD ["npm", "run", "int-test"]
## Stage 5 (security scanning and audit)
FROM test as audit
RUN npm audit
# Trivy security scanner (replaces deprecated microscanner)
# https://github.com/aquasecurity/trivy
ENV TRIVY_VERSION=0.35.0
# Use BuildKit to help translate architecture names
ARG TARGETPLATFORM
RUN case ${TARGETPLATFORM} in \
"linux/amd64") ARCH=amd64 ;; \
"linux/arm64") ARCH=arm64 ;; \
"linux/arm64/v8") ARCH=arm64 ;; \
"linux/arm/v7") ARCH=arm ;; \
*) ARCH=amd64 ;; \
esac \
&& apk add --no-cache wget ca-certificates \
&& update-ca-certificates \
&& wget --progress=dot:giga https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-${ARCH}.tar.gz \
&& tar zxf trivy_${TRIVY_VERSION}_Linux-${ARCH}.tar.gz \
&& mv trivy /usr/local/bin/ \
&& rm trivy_${TRIVY_VERSION}_Linux-${ARCH}.tar.gz
COPY . .
RUN trivy fs --severity "HIGH,CRITICAL" --no-progress --security-checks vuln .
## Stage 6 (default, production)
# this will run by default if you don't include a target
# it has prod-only dependencies
# In BuildKit, this is skipped for dev and test stages
FROM source as prod
CMD ["node", "./bin/www"]