-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDockerfile
75 lines (60 loc) · 2.43 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
68
69
70
71
72
73
74
75
# ------------------------------------------------------------------------------
# Multi-Stage Node Build.
# This Dockerfile is meant for production builds, but can also be used for dev
# ------------------------------------------------------------------------------
# Final Directory Structure:
# /
# |_app/
# |_node_modules/ <-- All node_modules
# |_src/ <-- Empty folder to bind the source files into
# |_package.json
# |_package-lock.json
# |_prodapp
# |_node_modules/ <-- Only the production node_modules
# |_src/ <-- Production ready application.
# |_resources/
# |_/views
# |_server.js
# ------------------------------------------------------------------------------
# pin images for reproducability
# ------------------------------------------------------------------------------
FROM node@sha256:0ae1a6a3a8a61e2bcf7f826b2562eb865f9a3095acf41bc6f184773ab66f3007 AS base
WORKDIR /app
# ------------------------------------------------------------------------------
# Setup prod dependencies first.
# ------------------------------------------------------------------------------
FROM base AS prod_dependencies
COPY package*.json ./
RUN npm install --production --silent
# ------------------------------------------------------------------------------
# Install remaining dev dependencies.
# ------------------------------------------------------------------------------
FROM prod_dependencies AS dev_dependencies
RUN npm install
# ------------------------------------------------------------------------------
# Setup dev target.
# ------------------------------------------------------------------------------
FROM dev_dependencies AS devapp
RUN mkdir /app/src
USER node
WORKDIR /app
EXPOSE 3000
CMD ["npm", "run", "dev"]
# ------------------------------------------------------------------------------
# Setup production target (/prodapp)
# ------------------------------------------------------------------------------
FROM node:12-alpine AS prodapp
# Create prodapp directory
WORKDIR /prodapp
COPY --from=prod_dependencies /app ./
COPY src /prodapp/src
# ------------------------------------------------------------------------------
# Run node as non-root user for production.
# run as non-root user
# ------------------------------------------------------------------------------
USER node
WORKDIR /prodapp
EXPOSE 3000
ENV \
NODE_ENV='production'
CMD ["npm", "start"]