-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
76 lines (50 loc) · 1.98 KB
/
Dockerfile
File metadata and controls
76 lines (50 loc) · 1.98 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
## Dockerfile for creating a production image
FROM node:20-alpine as builder
#########################
# BACKEND NODE PACKAGES #
#########################
WORKDIR /backend
COPY [ "./backend/package.json", "./backend/package-lock.json", "./" ]
# Only the production packages are needed
ENV NODE_ENV production
RUN npm install
# Install and run node-prune. It deletes unnecessary files from node_modules, such as md files etc
RUN apk update && apk add curl bash && rm -rf /var/cache/apk/*
RUN curl -sf https://gobinaries.com/tj/node-prune | sh
RUN node-prune /backend/node_modules
##########################
# FRONTEND NODE PACKAGES #
##########################
# Generate the static files with vite
# Create a folder for frontend building and set current directory to it
WORKDIR /frontend
# Copy package files from frontend folder into our new working directory in our image
COPY [ "./frontend/package.json", "./frontend/package-lock.json", "./" ]
ENV NODE_ENV development
# Install all packages required for building the frontend. which includes dev-dependencies
RUN npm install
##################
# BUILD FRONTEND #
##################
# This is done separate from installing node pakcages
# Each COPY creates a new layer. And if that layer has changed from previous builds, all subsequent ones have to be rebuilt!
# Hence I start with the one which is changed least frequently
# Copy all frontend code into /frontend-building directory inside the container
COPY ./frontend .
# Build the frontent with webpack. All files will be built inside /build
RUN npm run build
###############
# FINAL BUILD #
###############
# Copy over static frontend files
# Copy over backend source code
# Copy over installed node packages for backend
## This FROM will start a new container
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /frontend/dist /app/dist
COPY ./backend/src /app/src
COPY --from=builder /backend/node_modules /app/node_modules
ENV NODE_ENV production
EXPOSE 3000
CMD [ "node", "./src/index.js" ]